GNUnet 0.22.2
pabc_helper.c File Reference
#include "platform.h"
#include "pabc_helper.h"
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for pabc_helper.c:

Go to the source code of this file.

Functions

static const char * get_homedir ()
 
static enum GNUNET_GenericReturnValue write_file (char const *const filename, const char *buffer)
 
static enum GNUNET_GenericReturnValue init_pabc_dir ()
 
static const char * get_pabcdir ()
 
enum GNUNET_GenericReturnValue read_file (char const *const filename, char **buffer)
 
struct pabc_public_parameters * PABC_read_issuer_ppfile (const char *f, struct pabc_context *const ctx)
 
enum GNUNET_GenericReturnValue PABC_load_public_parameters (struct pabc_context *const ctx, char const *const pp_name, struct pabc_public_parameters **pp)
 
enum GNUNET_GenericReturnValue PABC_write_public_parameters (char const *const pp_name, struct pabc_public_parameters *const pp)
 
enum GNUNET_GenericReturnValue PABC_write_usr_ctx (char const *const usr_name, char const *const pp_name, struct pabc_context const *const ctx, struct pabc_public_parameters const *const pp, struct pabc_user_context *const usr_ctx)
 
enum GNUNET_GenericReturnValue PABC_read_usr_ctx (char const *const usr_name, char const *const pp_name, struct pabc_context const *const ctx, struct pabc_public_parameters const *const pp, struct pabc_user_context **usr_ctx)
 

Variables

static char pabc_dir [4096+1]
 

Function Documentation

◆ get_homedir()

static const char * get_homedir ( )
static

Definition at line 16 of file pabc_helper.c.

17{
18 const char *homedir;
19 if ((homedir = getenv ("HOME")) == NULL)
20 {
21 homedir = getpwuid (getuid ())->pw_dir;
22 }
23 return homedir;
24}
char * getenv()

References getenv().

Referenced by init_pabc_dir().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_file()

static enum GNUNET_GenericReturnValue write_file ( char const *const  filename,
const char *  buffer 
)
static

Definition at line 28 of file pabc_helper.c.

29{
30 struct GNUNET_DISK_FileHandle *fh;
37 if (fh == NULL)
38 return GNUNET_SYSERR;
40 buffer, strlen (buffer) + 1))
41 goto fail;
43 return GNUNET_OK;
44
45fail:
47 return GNUNET_SYSERR;
48}
static char * filename
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1215
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:682
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1289
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_OK
@ GNUNET_SYSERR
Handle used to access files (and pipes).

References filename, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_OK, and GNUNET_SYSERR.

Referenced by PABC_write_public_parameters(), and PABC_write_usr_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_pabc_dir()

static enum GNUNET_GenericReturnValue init_pabc_dir ( )
static

Definition at line 52 of file pabc_helper.c.

53{
54 size_t filename_size = strlen (get_homedir ()) + 1 + strlen (".local") + 1
55 + strlen ("pabc-reclaim") + 1;
56 snprintf (pabc_dir, filename_size, "%s/%s/%s",
57 get_homedir (), ".local", "pabc-reclaim");
59}
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:520
static char pabc_dir[4096+1]
Definition: pabc_helper.c:13
static const char * get_homedir()
Definition: pabc_helper.c:16

References get_homedir(), GNUNET_DISK_directory_create(), and pabc_dir.

Referenced by get_pabcdir().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pabcdir()

static const char * get_pabcdir ( )
static

Definition at line 63 of file pabc_helper.c.

64{
66 return pabc_dir;
67}
static enum GNUNET_GenericReturnValue init_pabc_dir()
Definition: pabc_helper.c:52

References init_pabc_dir(), and pabc_dir.

Referenced by PABC_load_public_parameters(), PABC_read_usr_ctx(), PABC_write_public_parameters(), and PABC_write_usr_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_file()

enum GNUNET_GenericReturnValue read_file ( char const *const  filename,
char **  buffer 
)

Definition at line 71 of file pabc_helper.c.

72{
73 struct GNUNET_DISK_FileHandle *fh;
75 return GNUNET_SYSERR;
76
80 if (fh == NULL)
81 return GNUNET_SYSERR;
82 long lSize = GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_END);
83 if (lSize < 0)
84 goto fail;
86 *buffer = calloc ((size_t) lSize + 1, sizeof(char));
87 if (*buffer == NULL)
88 goto fail;
89
90 // copy the file into the buffer:
91 size_t r = GNUNET_DISK_file_read (fh, *buffer, (size_t) lSize);
92 if (r != (size_t) lSize)
93 goto fail;
94
96 return GNUNET_OK;
97
98fail:
100 GNUNET_free (*buffer);
101 return GNUNET_SYSERR;
102}
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:506
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:206
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:646
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
@ GNUNET_DISK_SEEK_END
Seek an absolute position from the end of the file.
@ GNUNET_YES
#define GNUNET_free(ptr)
Wrapper around free.

References filename, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_file_seek(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_SEEK_END, GNUNET_DISK_SEEK_SET, GNUNET_free, GNUNET_OK, GNUNET_SYSERR, and GNUNET_YES.

Referenced by PABC_read_issuer_ppfile(), and PABC_read_usr_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PABC_read_issuer_ppfile()

struct pabc_public_parameters * PABC_read_issuer_ppfile ( const char *  f,
struct pabc_context *const  ctx 
)

Definition at line 106 of file pabc_helper.c.

107{
108 if (NULL == ctx)
109 {
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No global context provided\n");
111 return NULL;
112 }
113 struct pabc_public_parameters *pp;
114 char *buffer;
115 int r;
116 r = read_file (f, &buffer);
117 if (GNUNET_OK != r)
118 {
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading file\n");
120 return NULL;
121 }
122 if (PABC_OK != pabc_decode_and_new_public_parameters (ctx, &pp, buffer))
123 {
125 "Failed to decode public parameters\n");
126 PABC_FREE_NULL (buffer);
127 return NULL;
128 }
129 PABC_FREE_NULL (buffer);
130 return pp;
131}
static struct GNUNET_FS_Handle * ctx
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_ERROR
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:71

References ctx, removetrailingwhitespace::f, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, and read_file().

Referenced by PABC_load_public_parameters().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PABC_load_public_parameters()

enum GNUNET_GenericReturnValue PABC_load_public_parameters ( struct pabc_context *const  ctx,
char const *const  pp_name,
struct pabc_public_parameters **  pp 
)

Definition at line 135 of file pabc_helper.c.

138{
139 char fname[PATH_MAX];
140 char *pp_filename;
141 const char *pdir = get_pabcdir ();
142
143 if (ctx == NULL)
144 return GNUNET_SYSERR;
145 if (pp_name == NULL)
146 return GNUNET_SYSERR;
147
148 GNUNET_STRINGS_urlencode (strlen (pp_name),
149 pp_name,
150 &pp_filename);
152 {
153 GNUNET_free (pp_filename);
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading %s\n", pdir);
155 return GNUNET_SYSERR;
156 }
157 snprintf (fname, PATH_MAX, "%s/%s%s", pdir, pp_filename, PABC_PP_EXT);
158 if (GNUNET_YES != GNUNET_DISK_file_test (fname))
159 {
160 GNUNET_free (pp_filename);
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error testing %s\n", fname);
162 return GNUNET_SYSERR;
163 }
164 *pp = PABC_read_issuer_ppfile (fname, ctx);
165 if (*pp)
166 return GNUNET_OK;
167 else
168 return GNUNET_SYSERR;
169}
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:427
size_t GNUNET_STRINGS_urlencode(size_t len, const char data[static len], char **out)
url/percent encode (RFC3986).
Definition: strings.c:1863
struct pabc_public_parameters * PABC_read_issuer_ppfile(const char *f, struct pabc_context *const ctx)
Definition: pabc_helper.c:106
static const char * get_pabcdir()
Definition: pabc_helper.c:63
#define PABC_PP_EXT
Definition: pabc_helper.h:11
#define PATH_MAX
Assumed maximum path length.
Definition: platform.h:241

References ctx, get_pabcdir(), GNUNET_DISK_directory_test(), GNUNET_DISK_file_test(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_STRINGS_urlencode(), GNUNET_SYSERR, GNUNET_YES, PABC_PP_EXT, PABC_read_issuer_ppfile(), and PATH_MAX.

Referenced by pabc_create_presentation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PABC_write_public_parameters()

enum GNUNET_GenericReturnValue PABC_write_public_parameters ( char const *const  pp_name,
struct pabc_public_parameters *const  pp 
)

Definition at line 173 of file pabc_helper.c.

175{
176 char *json;
177 char *filename;
178 char *pp_filename;
179 enum pabc_status status;
180 struct pabc_context *ctx = NULL;
181
182 GNUNET_STRINGS_urlencode (strlen (pp_name),
183 pp_name,
184 &pp_filename);
185 PABC_ASSERT (pabc_new_ctx (&ctx));
186 // store in json file
187 status = pabc_encode_public_parameters (ctx, pp, &json);
188 if (status != PABC_OK)
189 {
190 GNUNET_free (pp_filename);
192 "Failed to encode public parameters.\n");
193 pabc_free_ctx (&ctx);
194 return GNUNET_SYSERR;
195 }
196
197 size_t filename_size =
198 strlen (get_pabcdir ()) + 1 + strlen (pp_filename) + strlen (PABC_PP_EXT)
199 + 1;
200 filename = GNUNET_malloc (filename_size);
201 if (! filename)
202 {
203 GNUNET_free (pp_filename);
204 PABC_FREE_NULL (json);
205 pabc_free_ctx (&ctx);
206 return GNUNET_SYSERR;
207 }
208 snprintf (filename, filename_size, "%s/%s%s", get_pabcdir (), pp_filename,
210
211 GNUNET_free (pp_filename);
212 if (GNUNET_OK != write_file (filename, json))
213 {
214 PABC_FREE_NULL (filename);
215 PABC_FREE_NULL (json);
216 pabc_free_ctx (&ctx);
217 return GNUNET_SYSERR;
218 }
219 PABC_FREE_NULL (filename);
220 PABC_FREE_NULL (json);
221 pabc_free_ctx (&ctx);
222 return GNUNET_OK;
223}
static int status
The program status; 0 for success.
Definition: gnunet-nse.c:39
#define GNUNET_malloc(size)
Wrapper around malloc.
static enum GNUNET_GenericReturnValue write_file(char const *const filename, const char *buffer)
Definition: pabc_helper.c:28

References ctx, filename, get_pabcdir(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_urlencode(), GNUNET_SYSERR, PABC_PP_EXT, status, and write_file().

Referenced by cr_cont().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PABC_write_usr_ctx()

enum GNUNET_GenericReturnValue PABC_write_usr_ctx ( char const *const  usr_name,
char const *const  pp_name,
struct pabc_context const *const  ctx,
struct pabc_public_parameters const *const  pp,
struct pabc_user_context *const  usr_ctx 
)

Definition at line 227 of file pabc_helper.c.

232{
233
234 char *pp_filename;
235 char *json = NULL;
236 enum pabc_status status;
237 char *fname = NULL;
238
239 if (NULL == usr_name)
240 {
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
242 return GNUNET_SYSERR;
243 }
244 if (NULL == pp_name)
245 {
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
247 return GNUNET_SYSERR;
248 }
249 if (NULL == ctx)
250 {
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
252 return GNUNET_SYSERR;
253 }
254 if (NULL == pp)
255 {
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
257 return GNUNET_SYSERR;
258 }
259 if (NULL == usr_ctx)
260 {
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
262 return GNUNET_SYSERR;
263 }
264
265 GNUNET_STRINGS_urlencode (strlen (pp_name),
266 pp_name,
267 &pp_filename);
268 status = pabc_encode_user_ctx (ctx, pp, usr_ctx, &json);
269 if (PABC_OK != status)
270 {
271 GNUNET_free (pp_filename);
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
273 return status;
274 }
275
276 size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
277 + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
278 fname = GNUNET_malloc (fname_size);
279
280 snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
281 pp_filename,
283
284 GNUNET_free (pp_filename);
285 if (GNUNET_OK == write_file (fname, json))
286 {
287 GNUNET_free (fname);
288 GNUNET_free (json);
289 return GNUNET_OK;
290 }
291 else
292 {
293 GNUNET_free (fname);
294 GNUNET_free (json);
295 return GNUNET_SYSERR;
296 }
297}
#define PABC_USR_EXT
Definition: pabc_helper.h:13

References ctx, get_pabcdir(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_urlencode(), GNUNET_SYSERR, PABC_USR_EXT, status, and write_file().

Referenced by cr_cont().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PABC_read_usr_ctx()

enum GNUNET_GenericReturnValue PABC_read_usr_ctx ( char const *const  usr_name,
char const *const  pp_name,
struct pabc_context const *const  ctx,
struct pabc_public_parameters const *const  pp,
struct pabc_user_context **  usr_ctx 
)

Definition at line 301 of file pabc_helper.c.

306{
307 char *json = NULL;
308 char *pp_filename;
309 enum pabc_status status;
310
311 char *fname = NULL;
312
313 if (NULL == usr_name)
314 {
315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
316 return GNUNET_SYSERR;
317 }
318 if (NULL == pp_name)
319 {
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
321 return GNUNET_SYSERR;
322 }
323 if (NULL == ctx)
324 {
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
326 return GNUNET_SYSERR;
327 }
328 if (NULL == pp)
329 {
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
331 return GNUNET_SYSERR;
332 }
333 if (NULL == usr_ctx)
334 {
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
336 return GNUNET_SYSERR;
337 }
338 GNUNET_STRINGS_urlencode (strlen (pp_name),
339 pp_name,
340 &pp_filename);
341
342 size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
343 + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
344 fname = GNUNET_malloc (fname_size);
345 snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
346 pp_filename,
348 GNUNET_free (pp_filename);
349 if (GNUNET_OK != read_file (fname, &json))
350 {
352 "Failed to read `%s'\n", fname);
353 PABC_FREE_NULL (fname);
354 return GNUNET_SYSERR;
355 }
356 GNUNET_free (fname);
357
358 status = pabc_new_user_context (ctx, pp, usr_ctx);
359 if (PABC_OK != status)
360 {
361 GNUNET_free (json);
362 return GNUNET_SYSERR;
363 }
364 status = pabc_decode_user_ctx (ctx, pp, *usr_ctx, json);
365 GNUNET_free (json);
366 if (PABC_OK != status)
367 {
368 pabc_free_user_context (ctx, pp, usr_ctx);
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
370 return GNUNET_SYSERR;
371 }
372
373 return GNUNET_OK;
374}

References ctx, get_pabcdir(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_urlencode(), GNUNET_SYSERR, PABC_USR_EXT, read_file(), and status.

Referenced by cr_cont(), and pabc_create_presentation().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ pabc_dir

char pabc_dir[4096+1]
static

Definition at line 13 of file pabc_helper.c.

Referenced by get_pabcdir(), and init_pabc_dir().