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.

References getenv().

Referenced by init_pabc_dir().

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()
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 27 of file pabc_helper.c.

References fh, 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().

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 }
Create file if it doesn&#39;t exist.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1126
Truncate file if it exists.
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:666
static char * filename
static int fh
Handle to the unique file.
Open the file for writing.
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:1055
Handle used to access files (and pipes).
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 51 of file pabc_helper.c.

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

Referenced by get_pabcdir().

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 }
static const char * get_homedir()
Definition: pabc_helper.c:15
static char pabc_dir[4096+1]
Definition: pabc_helper.c:12
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:476
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 62 of file pabc_helper.c.

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().

63 {
64  init_pabc_dir ();
65  return pabc_dir;
66 }
static char pabc_dir[4096+1]
Definition: pabc_helper.c:12
static enum GNUNET_GenericReturnValue init_pabc_dir()
Definition: pabc_helper.c:51
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 70 of file pabc_helper.c.

References fh, 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().

71 {
72  struct GNUNET_DISK_FileHandle *fh;
74  return GNUNET_SYSERR;
75 
79  if (fh == NULL)
80  return GNUNET_SYSERR;
81  long lSize = GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_END);
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 }
Open the file for reading.
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:602
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1126
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
static char * filename
static int fh
Handle to the unique file.
Seek an absolute position from the end of the file.
Seek an absolute position (from the start of the file).
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:1055
Handle used to access files (and pipes).
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:437
#define GNUNET_free(ptr)
Wrapper around free.
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 105 of file pabc_helper.c.

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, and read_file().

Referenced by PABC_load_public_parameters().

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.
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:70
#define GNUNET_log(kind,...)
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 134 of file pabc_helper.c.

References 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().

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 }
static const char * get_pabcdir()
Definition: pabc_helper.c:62
struct pabc_public_parameters * PABC_read_issuer_ppfile(const char *f, struct pabc_context *const ctx)
Definition: pabc_helper.c:105
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define PABC_PP_EXT
Definition: pabc_helper.h:11
#define PATH_MAX
Assumed maximum path length.
Definition: platform.h:247
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:2147
#define GNUNET_log(kind,...)
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
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:437
#define GNUNET_free(ptr)
Wrapper around free.
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 170 of file pabc_helper.c.

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().

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 }
static const char * get_pabcdir()
Definition: pabc_helper.c:62
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define PABC_PP_EXT
Definition: pabc_helper.h:11
uint16_t status
See PRISM_STATUS_*-constants.
static char * filename
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:2147
static enum GNUNET_GenericReturnValue write_file(char const *const filename, const char *buffer)
Definition: pabc_helper.c:27
#define GNUNET_log(kind,...)
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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 222 of file pabc_helper.c.

References 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().

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 }
static const char * get_pabcdir()
Definition: pabc_helper.c:62
#define PABC_USR_EXT
Definition: pabc_helper.h:13
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
uint16_t status
See PRISM_STATUS_*-constants.
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:2147
static enum GNUNET_GenericReturnValue write_file(char const *const filename, const char *buffer)
Definition: pabc_helper.c:27
#define GNUNET_log(kind,...)
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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 294 of file pabc_helper.c.

References 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().

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 }
static const char * get_pabcdir()
Definition: pabc_helper.c:62
#define PABC_USR_EXT
Definition: pabc_helper.h:13
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
uint16_t status
See PRISM_STATUS_*-constants.
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:70
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:2147
#define GNUNET_log(kind,...)
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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 12 of file pabc_helper.c.

Referenced by get_pabcdir(), and init_pabc_dir().