GNUnet  0.11.x
Functions | Variables
pabc_helper.c File Reference
#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 15 of file pabc_helper.c.

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

References getenv().

Here is the call graph for this function:

◆ write_file()

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

Definition at line 15 of file pabc_helper.c.

28 {
29  struct GNUNET_DISK_FileHandle *fh;
36  if (fh == NULL)
37  return GNUNET_SYSERR;
39  buffer, strlen (buffer) + 1))
40  goto fail;
42  return GNUNET_OK;
43 
44 fail:
46  return GNUNET_SYSERR;
47 }
static char * filename
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
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:1235
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:686
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
@ 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.
Handle used to access files (and pipes).

◆ init_pabc_dir()

static enum GNUNET_GenericReturnValue init_pabc_dir ( )
static

Definition at line 15 of file pabc_helper.c.

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

Referenced by get_pabcdir().

Here is the caller graph for this function:

◆ get_pabcdir()

static const char* get_pabcdir ( )
static

Definition at line 62 of file pabc_helper.c.

63 {
64  init_pabc_dir ();
65  return pabc_dir;
66 }
static enum GNUNET_GenericReturnValue init_pabc_dir()
Definition: pabc_helper.c:51

References init_pabc_dir(), and pabc_dir.

Here is the call graph for this function:

◆ read_file()

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

Definition at line 62 of file pabc_helper.c.

71 {
72  struct GNUNET_DISK_FileHandle *fh;
74  return GNUNET_SYSERR;
75 
79  if (fh == NULL)
80  return GNUNET_SYSERR;
82  if (lSize < 0)
83  goto fail;
85  *buffer = calloc ((size_t) lSize + 1, sizeof(char));
86  if (*buffer == NULL)
87  goto fail;
88 
89  // copy the file into the buffer:
90  size_t r = GNUNET_DISK_file_read (fh, *buffer, (size_t) lSize);
91  if (r != (size_t) lSize)
92  goto fail;
93 
95  return GNUNET_OK;
96 
97 fail:
99  GNUNET_free (*buffer);
100  return GNUNET_SYSERR;
101 }
@ GNUNET_YES
Definition: gnunet_common.h:97
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:482
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:622
@ 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.
#define GNUNET_free(ptr)
Wrapper around free.

Referenced by PABC_load_public_parameters().

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 105 of file pabc_helper.c.

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

◆ 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 105 of file pabc_helper.c.

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

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

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 105 of file pabc_helper.c.

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

Referenced by cr_cont().

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 105 of file pabc_helper.c.

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

Referenced by cr_cont().

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 105 of file pabc_helper.c.

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

Referenced by cr_cont(), and pabc_create_presentation().

Here is the caller graph for this function:

Variable Documentation

◆ pabc_dir

char pabc_dir[4096+1]
static

Definition at line 12 of file pabc_helper.c.

Referenced by get_pabcdir().