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