Coverage Report

Created: 2022-12-08 06:10

/src/libassuan/src/assuan-defs.h
Line
Count
Source (jump to first uncovered line)
1
/* assuan-defs.h - Internal definitions to Assuan
2
 * Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008,
3
 *               2009, 2010 Free Software Foundation, Inc.
4
 *
5
 * This file is part of Assuan.
6
 *
7
 * Assuan is free software; you can redistribute it and/or modify it
8
 * under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation; either version 2.1 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * Assuan is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
 * SPDX-License-Identifier: LGPL-2.1+
20
 */
21
22
#ifndef ASSUAN_DEFS_H
23
#define ASSUAN_DEFS_H
24
25
#ifdef HAVE_SYS_TYPES_H
26
# include <sys/types.h>
27
#endif
28
#ifndef HAVE_W32_SYSTEM
29
# include <sys/socket.h>
30
# include <sys/un.h>
31
#else
32
# ifdef HAVE_WINSOCK2_H
33
#  /* Avoid inclusion of winsock.h via windows.h. */
34
#  include <winsock2.h>
35
# endif
36
# include <windows.h>
37
#endif
38
#ifdef HAVE_UNISTD_H
39
# include <unistd.h>
40
#endif
41
42
#include "assuan.h"
43
44
#if __GNUC__ > 2
45
# define ASSUAN_GCC_A_PURE  __attribute__ ((__pure__))
46
#else
47
# define ASSUAN_GCC_A_PURE
48
#endif
49
50
#ifndef HAVE_W32_SYSTEM
51
614k
#define DIRSEP_C '/'
52
#else
53
#define DIRSEP_C '\\'
54
#endif
55
56
0
#define LINELENGTH ASSUAN_LINELENGTH
57
58
59
struct cmdtbl_s
60
{
61
  const char *name;
62
  assuan_handler_t handler;
63
  const char *helpstr;
64
};
65
66
67

68
/* The context we use with most functions. */
69
struct assuan_context_s
70
{
71
  /* Members managed by the generic routines in assuan.c.  */
72
73
  /* The error source for errors generated from this context.  */
74
  gpg_err_source_t err_source;
75
76
#ifdef HAVE_W32_SYSTEM
77
  /* The per-context w32 error string.  */
78
  char w32_strerror[256];
79
#endif
80
81
  /* The allocation hooks.  */
82
  struct assuan_malloc_hooks malloc_hooks;
83
84
  /* Logging callback handler.  */
85
  assuan_log_cb_t log_cb;
86
  void *log_cb_data;
87
88
  void *user_pointer;
89
90
  /* Context specific flags (cf. assuan_flag_t). */
91
  struct
92
  {
93
    unsigned int no_waitpid : 1;
94
    unsigned int confidential : 1;
95
    unsigned int no_fixsignals : 1;
96
    unsigned int convey_comments : 1;
97
    unsigned int no_logging : 1;
98
    unsigned int force_close : 1;
99
    /* From here, we have internal flags, not defined by assuan_flag_t.  */
100
    unsigned int is_socket : 1;
101
    unsigned int is_server : 1; /* Set if this is context belongs to a server */
102
    unsigned int in_inquire : 1; /* Server: inside assuan_inquire */
103
    unsigned int in_process_next : 1;
104
    unsigned int process_complete : 1;
105
    unsigned int in_command : 1;
106
    unsigned int in_inq_cb : 1; /* Client: inquire callback is active */
107
    unsigned int confidential_inquiry : 1; /* Client: inquiry is confidential */
108
  } flags;
109
110
  /* If set, this is called right before logging an I/O line.  */
111
  assuan_io_monitor_t io_monitor;
112
  void *io_monitor_data;
113
114
  /* Callback handlers replacing system I/O functions.  */
115
  struct assuan_system_hooks system;
116
117
  int peercred_valid;   /* Whether this structure has valid information. */
118
  struct _assuan_peercred peercred;
119
120
  /* Now come the members specific to subsystems or engines.  FIXME:
121
     This is not developed yet.  See below for the legacy members.  */
122
  struct
123
  {
124
    void (*release) (assuan_context_t ctx);
125
126
    /* Routine to read from input_fd.  Sets errno on failure.  */
127
    ssize_t (*readfnc) (assuan_context_t, void *, size_t);
128
    /* Routine to write to output_fd.  Sets errno on failure.  */
129
    ssize_t (*writefnc) (assuan_context_t, const void *, size_t);
130
    /* Send a file descriptor.  */
131
    gpg_error_t (*sendfd) (assuan_context_t, assuan_fd_t);
132
    /* Receive a file descriptor.  */
133
    gpg_error_t (*receivefd) (assuan_context_t, assuan_fd_t *);
134
  } engine;
135
136
137
  /* Engine specific or other subsystem members.  */
138
139
  /* assuan-logging.c.  Does not require deallocation from us.  */
140
  FILE *log_fp;
141
142
  /* assuan-util.c  */
143
  gpg_error_t err_no;
144
  const char *err_str;
145
146
  /* The following members are used by assuan_inquire_ext.  */
147
  gpg_error_t (*inquire_cb) (void *cb_data, gpg_error_t rc,
148
           unsigned char *buf, size_t len);
149
  void *inquire_cb_data;
150
  void *inquire_membuf;
151
152
  char *hello_line;
153
  char *okay_line;    /* See assuan_set_okay_line() */
154
155
156
  struct {
157
    assuan_fd_t fd;
158
    int eof;
159
    char line[LINELENGTH];
160
    int linelen;  /* w/o CR, LF - might not be the same as
161
                     strlen(line) due to embedded nuls. However a nul
162
                     is always written at this pos. */
163
    struct {
164
      char line[LINELENGTH];
165
      int linelen ;
166
      int pending; /* i.e. at least one line is available in the attic */
167
    } attic;
168
  } inbound;
169
170
  struct {
171
    assuan_fd_t fd;
172
    struct {
173
      FILE *fp;
174
      char line[LINELENGTH];
175
      int linelen;
176
      int error;
177
    } data;
178
  } outbound;
179
180
  int max_accepts;  /* If we can not handle more than one connection,
181
           set this to 1, otherwise to -1.  */
182
  pid_t pid;    /* The pid of the peer. */
183
#if defined(HAVE_W64_SYSTEM) || defined(HAVE_W32_SYSTEM)
184
  int process_id;     /* process ID of the server for FD passing.  */
185
#endif
186
  assuan_fd_t listen_fd;  /* The fd we are listening on (used by
187
                             socket servers) */
188
  assuan_sock_nonce_t listen_nonce; /* Used with LISTEN_FD.  */
189
  assuan_fd_t connected_fd; /* helper */
190
191
  /* Used for Unix domain sockets.  */
192
  struct sockaddr_un myaddr;
193
  struct sockaddr_un serveraddr;
194
195
  /* Structure used for unix domain sockets.  */
196
  struct {
197
    assuan_fd_t pendingfds[5]; /* Array to save received descriptors.  */
198
    int pendingfdscount;  /* Number of received descriptors. */
199
  } uds;
200
201
  gpg_error_t (*accept_handler)(assuan_context_t);
202
  void (*finish_handler)(assuan_context_t);
203
204
  struct cmdtbl_s *cmdtbl;
205
  size_t cmdtbl_used; /* used entries */
206
  size_t cmdtbl_size; /* allocated size of table */
207
208
  /* The name of the command currently processed by a command handler.
209
     This is a pointer into CMDTBL.  NULL if not in a command
210
     handler.  */
211
  const char *current_cmd_name;
212
213
  assuan_handler_t bye_notify_fnc;
214
  assuan_handler_t reset_notify_fnc;
215
  assuan_handler_t cancel_notify_fnc;
216
  gpg_error_t  (*option_handler_fnc)(assuan_context_t,const char*, const char*);
217
  assuan_handler_t input_notify_fnc;
218
  assuan_handler_t output_notify_fnc;
219
220
  /* This function is called right before a command handler is called. */
221
  gpg_error_t (*pre_cmd_notify_fnc)(assuan_context_t, const char *cmd);
222
223
  /* This function is called right after a command has been processed.
224
     It may be used to command related cleanup.  */
225
  void (*post_cmd_notify_fnc)(assuan_context_t, gpg_error_t);
226
227
228
  assuan_fd_t input_fd;   /* Set by the INPUT command.  */
229
  assuan_fd_t output_fd;  /* Set by the OUTPUT command.  */
230
};
231
232
233

234
/* Generate an error code specific to a context.  */
235
static GPG_ERR_INLINE gpg_error_t
236
_assuan_error (assuan_context_t ctx, gpg_err_code_t errcode)
237
307k
{
238
307k
  return gpg_err_make (ctx?ctx->err_source: GPG_ERR_SOURCE_ASSUAN, errcode);
239
307k
}
Unexecuted instantiation: assuan.c:_assuan_error
Unexecuted instantiation: context.c:_assuan_error
Unexecuted instantiation: system.c:_assuan_error
Unexecuted instantiation: debug.c:_assuan_error
Unexecuted instantiation: sysutils.c:_assuan_error
Unexecuted instantiation: client.c:_assuan_error
Unexecuted instantiation: assuan-error.c:_assuan_error
Unexecuted instantiation: assuan-buffer.c:_assuan_error
Unexecuted instantiation: assuan-handler.c:_assuan_error
Unexecuted instantiation: assuan-inquire.c:_assuan_error
Unexecuted instantiation: assuan-listen.c:_assuan_error
Unexecuted instantiation: assuan-pipe-server.c:_assuan_error
assuan-socket-connect.c:_assuan_error
Line
Count
Source
237
307k
{
238
307k
  return gpg_err_make (ctx?ctx->err_source: GPG_ERR_SOURCE_ASSUAN, errcode);
239
307k
}
Unexecuted instantiation: assuan-uds.c:_assuan_error
Unexecuted instantiation: assuan-logging.c:_assuan_error
Unexecuted instantiation: assuan-socket.c:_assuan_error
Unexecuted instantiation: system-posix.c:_assuan_error
Unexecuted instantiation: assuan-io.c:_assuan_error
Unexecuted instantiation: conversion.c:_assuan_error
Unexecuted instantiation: server.c:_assuan_error
240
241
/* Release all resources associated with an engine operation.  */
242
void _assuan_reset (assuan_context_t ctx);
243
244
/* Default log handler.  */
245
int _assuan_log_handler (assuan_context_t ctx, void *hook,
246
        unsigned int cat, const char *msg);
247
248

249
/* Manage memory specific to a context.  */
250
void *_assuan_malloc (assuan_context_t ctx, size_t cnt);
251
void *_assuan_realloc (assuan_context_t ctx, void *ptr, size_t cnt);
252
void *_assuan_calloc (assuan_context_t ctx, size_t cnt, size_t elsize);
253
void _assuan_free (assuan_context_t ctx, void *ptr);
254
255
/* System hooks.  */
256
void _assuan_usleep (assuan_context_t ctx, unsigned int usec);
257
int _assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx);
258
int _assuan_close (assuan_context_t ctx, assuan_fd_t fd);
259
int _assuan_close_inheritable (assuan_context_t ctx, assuan_fd_t fd);
260
ssize_t _assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer,
261
          size_t size);
262
ssize_t _assuan_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer,
263
           size_t size);
264
int _assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd,
265
         assuan_msghdr_t msg, int flags);
266
int _assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd,
267
         assuan_msghdr_t msg, int flags);
268
int _assuan_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name,
269
       const char *argv[],
270
       assuan_fd_t fd_in, assuan_fd_t fd_out,
271
       assuan_fd_t *fd_child_list,
272
       void (*atfork) (void *opaque, int reserved),
273
       void *atforkvalue, unsigned int flags);
274
pid_t  _assuan_waitpid (assuan_context_t ctx, pid_t pid, int nowait,
275
      int *status, int options);
276
int _assuan_socketpair (assuan_context_t ctx, int namespace, int style,
277
      int protocol, assuan_fd_t filedes[2]);
278
assuan_fd_t _assuan_socket (assuan_context_t ctx, int namespace,
279
                            int style, int protocol);
280
int _assuan_connect (assuan_context_t ctx, assuan_fd_t sock,
281
                     struct sockaddr *addr, socklen_t length);
282
283
extern struct assuan_system_hooks _assuan_system_hooks;
284
285
/* Copy the system hooks struct, paying attention to version
286
   differences.  SRC is usually from the user, DST MUST be from the
287
   library.  */
288
void
289
_assuan_system_hooks_copy (assuan_system_hooks_t dst,
290
         assuan_system_hooks_t src);
291
292

293
/*-- assuan-pipe-server.c --*/
294
void _assuan_release_context (assuan_context_t ctx);
295
296
/*-- assuan-uds.c --*/
297
void _assuan_uds_close_fds (assuan_context_t ctx);
298
void _assuan_uds_deinit (assuan_context_t ctx);
299
void _assuan_init_uds_io (assuan_context_t ctx);
300
301
302
/*-- assuan-handler.c --*/
303
gpg_error_t _assuan_register_std_commands (assuan_context_t ctx);
304
305
/*-- assuan-buffer.c --*/
306
gpg_error_t _assuan_read_line (assuan_context_t ctx);
307
int _assuan_cookie_write_data (void *cookie, const char *buffer, size_t size);
308
int _assuan_cookie_write_flush (void *cookie);
309
gpg_error_t _assuan_write_line (assuan_context_t ctx, const char *prefix,
310
                                   const char *line, size_t len);
311
312
/*-- client.c --*/
313
gpg_error_t _assuan_read_from_server (assuan_context_t ctx,
314
              assuan_response_t *okay, int *off,
315
                                      int convey_comments);
316
317
/*-- assuan-error.c --*/
318
319
/*-- assuan-inquire.c --*/
320
gpg_error_t _assuan_inquire_ext_cb (assuan_context_t ctx);
321
void _assuan_inquire_release (assuan_context_t ctx);
322
323
/* Check if ERR means EAGAIN.  */
324
int _assuan_error_is_eagain (assuan_context_t ctx, gpg_error_t err);
325
326
327
328
#define set_error(c,e,t)            \
329
0
  assuan_set_error ((c), _assuan_error (c,e), (t))
330
331
#ifdef HAVE_W32_SYSTEM
332
char *_assuan_w32_strerror (assuan_context_t ctx, int ec);
333
gpg_error_t w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd);
334
gpg_error_t w32_fdpass_recv (assuan_context_t ctx, assuan_fd_t *fd);
335
#endif /*HAVE_W32_SYSTEM*/
336
337
338
/*-- assuan-logging.c --*/
339
void _assuan_init_log_envvars (void);
340
void _assuan_log_control_channel (assuan_context_t ctx, int outbound,
341
                                  const char *string,
342
                                  const void *buffer1, size_t length1,
343
                                  const void *buffer2, size_t length2);
344
345
346
/*-- assuan-io.c --*/
347
ssize_t _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size);
348
ssize_t _assuan_simple_write (assuan_context_t ctx, const void *buffer,
349
            size_t size);
350
351
/*-- assuan-socket.c --*/
352
353
assuan_fd_t _assuan_sock_new (assuan_context_t ctx, int domain, int type,
354
            int proto);
355
int _assuan_sock_connect (assuan_context_t ctx, assuan_fd_t sockfd,
356
                          struct sockaddr *addr, int addrlen);
357
int _assuan_sock_bind (assuan_context_t ctx, assuan_fd_t sockfd,
358
           struct sockaddr *addr, int addrlen);
359
int _assuan_sock_set_sockaddr_un (const char *fname, struct sockaddr *addr,
360
                                  int *r_redirected);
361
int _assuan_sock_get_nonce (assuan_context_t ctx, struct sockaddr *addr,
362
          int addrlen, assuan_sock_nonce_t *nonce);
363
int _assuan_sock_check_nonce (assuan_context_t ctx, assuan_fd_t fd,
364
            assuan_sock_nonce_t *nonce);
365
#ifdef HAVE_W32_SYSTEM
366
wchar_t *_assuan_utf8_to_wchar (const char *string);
367
int _assuan_sock_wsa2errno (int err);
368
#endif
369
370
#ifdef HAVE_FOPENCOOKIE
371
/* We have to implement funopen in terms of glibc's fopencookie. */
372
FILE *_assuan_funopen(void *cookie,
373
                      cookie_read_function_t *readfn,
374
                      cookie_write_function_t *writefn,
375
                      cookie_seek_function_t *seekfn,
376
                      cookie_close_function_t *closefn);
377
0
#define funopen(a,r,w,s,c) _assuan_funopen ((a), (r), (w), (s), (c))
378
#endif /*HAVE_FOPENCOOKIE*/
379
380
/*-- sysutils.c --*/
381
const char *_assuan_sysutils_blurb (void);
382
383
/* Prototypes for replacement functions.  */
384
#ifndef HAVE_MEMRCHR
385
void *memrchr (const void *block, int c, size_t size);
386
#endif
387
#ifndef HAVE_STPCPY
388
char *stpcpy (char *dest, const char *src);
389
#endif
390
#ifndef HAVE_SETENV
391
#define setenv _assuan_setenv
392
#define unsetenv _assuan_unsetenv
393
#define clearenv _assuan_clearenv
394
int setenv (const char *name, const char *value, int replace);
395
#endif
396
#ifndef HAVE_PUTC_UNLOCKED
397
int putc_unlocked (int c, FILE *stream);
398
#endif
399
400
401
0
#define DIM(v)         (sizeof(v)/sizeof((v)[0]))
402
403
/* To avoid that a compiler optimizes memset calls away, these macros
404
   can be used. */
405
614k
#define wipememory2(_ptr,_set,_len) do { \
406
614k
              volatile char *_vptr=(volatile char *)(_ptr); \
407
614k
              size_t _vlen=(_len); \
408
941M
              while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
409
614k
                  } while(0)
410
614k
#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
411
412
413
#if HAVE_W64_SYSTEM
414
# define SOCKET2HANDLE(s) ((void *)(s))
415
# define HANDLE2SOCKET(h) ((uintptr_t)(h))
416
#elif HAVE_W32_SYSTEM
417
# define SOCKET2HANDLE(s) ((void *)(s))
418
# define HANDLE2SOCKET(h) ((unsigned int)(h))
419
#else
420
# define SOCKET2HANDLE(s) (s)
421
0
# define HANDLE2SOCKET(h) (h)
422
#endif
423
424
425
void _assuan_client_finish (assuan_context_t ctx);
426
void _assuan_client_release (assuan_context_t ctx);
427
428
void _assuan_server_finish (assuan_context_t ctx);
429
void _assuan_server_release (assuan_context_t ctx);
430
431
432
/* Encode the C formatted string SRC and return the malloc'ed result.  */
433
char *_assuan_encode_c_string (assuan_context_t ctx, const char *src);
434
435
void _assuan_pre_syscall (void);
436
void _assuan_post_syscall (void);
437
438
#endif /*ASSUAN_DEFS_H*/