| 1 | /* assuan-defs.c - Internal definitions to Assuan |
| 2 | * Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of Assuan. |
| 5 | * |
| 6 | * Assuan is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Lesser General Public License as |
| 8 | * published by the Free Software Foundation; either version 2.1 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * Assuan is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 19 | * USA. |
| 20 | */ |
| 21 | |
| 22 | #ifndef ASSUAN_DEFS_H |
| 23 | #define ASSUAN_DEFS_H |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #ifndef HAVE_W32_SYSTEM |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/un.h> |
| 29 | #else |
| 30 | #include <windows.h> |
| 31 | #endif |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include "assuan.h" |
| 35 | |
| 36 | #ifndef HAVE_W32_SYSTEM |
| 37 | #define DIRSEP_C '/' |
| 38 | #else |
| 39 | #define DIRSEP_C '\\' |
| 40 | #endif |
| 41 | |
| 42 | #ifdef HAVE_W32_SYSTEM |
| 43 | /* Not needed anymore because the current mingw32 defines this in |
| 44 | sys/types.h */ |
| 45 | /* typedef int ssize_t; */ |
| 46 | |
| 47 | /* Missing W32 functions */ |
| 48 | int putc_unlocked (int c, FILE *stream); |
| 49 | void * memrchr (const void *block, int c, size_t size); |
| 50 | char * stpcpy (char *dest, const char *src); |
| 51 | #endif |
| 52 | |
| 53 | #define LINELENGTH ASSUAN_LINELENGTH |
| 54 | |
| 55 | |
| 56 | struct cmdtbl_s |
| 57 | { |
| 58 | const char *name; |
| 59 | int (*handler)(assuan_context_t, char *line); |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | /* A structure to dispatch I/O functions. All these functions need to |
| 64 | return 0 on success and set ERRNO on failure. */ |
| 65 | struct assuan_io |
| 66 | { |
| 67 | /* Routine to read from input_fd. */ |
| 68 | ssize_t (*readfnc) (assuan_context_t, void *, size_t); |
| 69 | /* Routine to write to output_fd. */ |
| 70 | ssize_t (*writefnc) (assuan_context_t, const void *, size_t); |
| 71 | /* Send a file descriptor. */ |
| 72 | assuan_error_t (*sendfd) (assuan_context_t, assuan_fd_t); |
| 73 | /* Receive a file descriptor. */ |
| 74 | assuan_error_t (*receivefd) (assuan_context_t, assuan_fd_t *); |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | /* The global variable with the optional hook fucntions. */ |
| 79 | extern struct assuan_io_hooks _assuan_io_hooks; |
| 80 | |
| 81 | |
| 82 | /* The context we use with most functions. */ |
| 83 | struct assuan_context_s |
| 84 | { |
| 85 | assuan_error_t err_no; |
| 86 | const char *err_str; |
| 87 | int os_errno; /* Last system error number used with certain |
| 88 | error codes. */ |
| 89 | |
| 90 | /* Context specific flags (cf. assuan_flag_t). */ |
| 91 | struct |
| 92 | { |
| 93 | unsigned int no_waitpid:1; /* See ASSUAN_NO_WAITPID. */ |
| 94 | } flags; |
| 95 | |
| 96 | int confidential; |
| 97 | int is_server; /* Set if this is context belongs to a server */ |
| 98 | int in_inquire; |
| 99 | int in_process_next; |
| 100 | int in_command; |
| 101 | |
| 102 | /* The following members are used by assuan_inquire_ext. */ |
| 103 | int (*inquire_cb) (void *cb_data, int rc, unsigned char *buf, size_t len); |
| 104 | void *inquire_cb_data; |
| 105 | void *inquire_membuf; |
| 106 | |
| 107 | char *hello_line; |
| 108 | char *okay_line; /* See assuan_set_okay_line() */ |
| 109 | |
| 110 | void *user_pointer; /* For assuan_get_pointer and assuan_set_pointer (). */ |
| 111 | |
| 112 | FILE *log_fp; |
| 113 | |
| 114 | struct { |
| 115 | assuan_fd_t fd; |
| 116 | int eof; |
| 117 | char line[LINELENGTH]; |
| 118 | int linelen; /* w/o CR, LF - might not be the same as |
| 119 | strlen(line) due to embedded nuls. However a nul |
| 120 | is always written at this pos. */ |
| 121 | struct { |
| 122 | char line[LINELENGTH]; |
| 123 | int linelen ; |
| 124 | int pending; /* i.e. at least one line is available in the attic */ |
| 125 | } attic; |
| 126 | } inbound; |
| 127 | |
| 128 | struct { |
| 129 | assuan_fd_t fd; |
| 130 | struct { |
| 131 | FILE *fp; |
| 132 | char line[LINELENGTH]; |
| 133 | int linelen; |
| 134 | int error; |
| 135 | } data; |
| 136 | } outbound; |
| 137 | |
| 138 | int pipe_mode; /* We are in pipe mode, i.e. we can handle just one |
| 139 | connection and must terminate then. */ |
| 140 | pid_t pid; /* The pid of the peer. */ |
| 141 | assuan_fd_t listen_fd; /* The fd we are listening on (used by |
| 142 | socket servers) */ |
| 143 | assuan_sock_nonce_t listen_nonce; /* Used with LISTEN_FD. */ |
| 144 | assuan_fd_t connected_fd; /* helper */ |
| 145 | |
| 146 | struct { |
| 147 | int valid; /* Whether this structure has valid information. */ |
| 148 | #ifdef HAVE_SO_PEERCRED |
| 149 | pid_t pid; /* The pid of the peer. */ |
| 150 | uid_t uid; /* The uid of the peer. */ |
| 151 | gid_t gid; /* The gid of the peer. */ |
| 152 | #endif /* HAVE_SO_PEERCRED */ |
| 153 | } peercred; |
| 154 | |
| 155 | /* Used for Unix domain sockets. */ |
| 156 | struct sockaddr_un myaddr; |
| 157 | struct sockaddr_un serveraddr; |
| 158 | |
| 159 | /* Structure used for unix domain socket buffering. FIXME: We don't |
| 160 | use datagrams anymore thus we could get away with a simpler |
| 161 | buffering approach. */ |
| 162 | struct { |
| 163 | void *buffer; /* Malloced buffer. */ |
| 164 | int bufferallocated; /* Memory allocated. */ |
| 165 | int bufferoffset; /* Offset of start of buffer. */ |
| 166 | int buffersize; /* Bytes buffered. */ |
| 167 | |
| 168 | assuan_fd_t pendingfds[5]; /* Array to save received descriptors. */ |
| 169 | int pendingfdscount; /* Number of received descriptors. */ |
| 170 | } uds; |
| 171 | |
| 172 | void (*deinit_handler)(assuan_context_t); |
| 173 | int (*accept_handler)(assuan_context_t); |
| 174 | int (*finish_handler)(assuan_context_t); |
| 175 | |
| 176 | struct cmdtbl_s *cmdtbl; |
| 177 | size_t cmdtbl_used; /* used entries */ |
| 178 | size_t cmdtbl_size; /* allocated size of table */ |
| 179 | |
| 180 | void (*bye_notify_fnc)(assuan_context_t); |
| 181 | void (*reset_notify_fnc)(assuan_context_t); |
| 182 | void (*cancel_notify_fnc)(assuan_context_t); |
| 183 | int (*option_handler_fnc)(assuan_context_t,const char*, const char*); |
| 184 | void (*input_notify_fnc)(assuan_context_t, const char *); |
| 185 | void (*output_notify_fnc)(assuan_context_t, const char *); |
| 186 | |
| 187 | /* This function is called right after a command has been processed. |
| 188 | It may be used to command related cleanup. */ |
| 189 | void (*post_cmd_notify_fnc)(assuan_context_t, int); |
| 190 | |
| 191 | /* If set, this is called right before logging an I/O line. With |
| 192 | DIRECTION set to 1 it is called for an output oeration; 0 means |
| 193 | an input operation. If bit 0 is set in the return value, the |
| 194 | logging of the line will be suppressed. With bit 1 set, the |
| 195 | entire line will be ignored. */ |
| 196 | unsigned int (*io_monitor)(assuan_context_t ctx, |
| 197 | int direction, |
| 198 | const char *line, |
| 199 | size_t linelen); |
| 200 | |
| 201 | assuan_fd_t input_fd; /* Set by the INPUT command. */ |
| 202 | assuan_fd_t output_fd; /* Set by the OUTPUT command. */ |
| 203 | |
| 204 | /* io routines. */ |
| 205 | struct assuan_io *io; |
| 206 | }; |
| 207 | |
| 208 | /*-- assuan-pipe-server.c --*/ |
| 209 | int _assuan_new_context (assuan_context_t *r_ctx); |
| 210 | void _assuan_release_context (assuan_context_t ctx); |
| 211 | |
| 212 | /*-- assuan-uds.c --*/ |
| 213 | void _assuan_uds_close_fds (assuan_context_t ctx); |
| 214 | void _assuan_uds_deinit (assuan_context_t ctx); |
| 215 | void _assuan_init_uds_io (assuan_context_t ctx); |
| 216 | |
| 217 | |
| 218 | /*-- assuan-handler.c --*/ |
| 219 | int _assuan_register_std_commands (assuan_context_t ctx); |
| 220 | |
| 221 | /*-- assuan-buffer.c --*/ |
| 222 | assuan_error_t _assuan_read_line (assuan_context_t ctx); |
| 223 | int _assuan_cookie_write_data (void *cookie, const char *buffer, size_t size); |
| 224 | int _assuan_cookie_write_flush (void *cookie); |
| 225 | assuan_error_t _assuan_write_line (assuan_context_t ctx, const char *prefix, |
| 226 | const char *line, size_t len); |
| 227 | |
| 228 | /*-- assuan-client.c --*/ |
| 229 | assuan_error_t _assuan_read_from_server (assuan_context_t ctx, |
| 230 | int *okay, int *off); |
| 231 | |
| 232 | /*-- assuan-error.c --*/ |
| 233 | |
| 234 | /*-- assuan-inquire.c --*/ |
| 235 | int _assuan_inquire_ext_cb (assuan_context_t ctx); |
| 236 | void _assuan_inquire_release (assuan_context_t ctx); |
| 237 | |
| 238 | /* Map error codes as used in this implementation to the libgpg-error |
| 239 | codes. */ |
| 240 | assuan_error_t _assuan_error (int oldcode); |
| 241 | /* Check if ERR means EAGAIN. */ |
| 242 | int _assuan_error_is_eagain (assuan_error_t err); |
| 243 | |
| 244 | /* Extract the error code from A. This works for both the old and the |
| 245 | new style error codes. This needs to be used whenever an error |
| 246 | code is compared. */ |
| 247 | #define err_code(a) ((a) & 0x00ffffff) |
| 248 | |
| 249 | /* Check whether A is the erro code for EOF. We allow for old and new |
| 250 | style EOF error codes here. */ |
| 251 | #define err_is_eof(a) ((a) == (-1) || err_code (a) == 16383) |
| 252 | |
| 253 | |
| 254 | |
| 255 | /*-- assuan-util.c --*/ |
| 256 | void *_assuan_malloc (size_t n); |
| 257 | void *_assuan_calloc (size_t n, size_t m); |
| 258 | void *_assuan_realloc (void *p, size_t n); |
| 259 | void _assuan_free (void *p); |
| 260 | |
| 261 | #define xtrymalloc(a) _assuan_malloc ((a)) |
| 262 | #define xtrycalloc(a,b) _assuan_calloc ((a),(b)) |
| 263 | #define xtryrealloc(a,b) _assuan_realloc((a),(b)) |
| 264 | #define xfree(a) _assuan_free ((a)) |
| 265 | |
| 266 | #define set_error(c,e,t) \ |
| 267 | assuan_set_error ((c), _assuan_error (ASSUAN_ ## e), (t)) |
| 268 | |
| 269 | #ifdef HAVE_W32_SYSTEM |
| 270 | const char *_assuan_w32_strerror (int ec); |
| 271 | #define w32_strerror(e) _assuan_w32_strerror ((e)) |
| 272 | int _assuan_gpg_strerror_r (unsigned int err, char *buf, size_t buflen); |
| 273 | const char *_assuan_gpg_strsource (unsigned int err); |
| 274 | #endif /*HAVE_W32_SYSTEM*/ |
| 275 | |
| 276 | |
| 277 | /*-- assuan-logging.c --*/ |
| 278 | void _assuan_set_default_log_stream (FILE *fp); |
| 279 | |
| 280 | void _assuan_log_printf (const char *format, ...) |
| 281 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) |
| 282 | __attribute__ ((format (printf,1,2))) |
| 283 | #endif |
| 284 | ; |
| 285 | void _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length); |
| 286 | void _assuan_log_sanitized_string (const char *string); |
| 287 | |
| 288 | |
| 289 | /*-- assuan-io.c --*/ |
| 290 | pid_t _assuan_waitpid (pid_t pid, int *status, int options); |
| 291 | |
| 292 | ssize_t _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size); |
| 293 | ssize_t _assuan_simple_write (assuan_context_t ctx, const void *buffer, |
| 294 | size_t size); |
| 295 | ssize_t _assuan_io_read (assuan_fd_t fd, void *buffer, size_t size); |
| 296 | ssize_t _assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size); |
| 297 | #ifdef HAVE_W32_SYSTEM |
| 298 | int _assuan_simple_sendmsg (assuan_context_t ctx, void *msg); |
| 299 | int _assuan_simple_recvmsg (assuan_context_t ctx, void *msg); |
| 300 | #else |
| 301 | ssize_t _assuan_simple_sendmsg (assuan_context_t ctx, struct msghdr *msg); |
| 302 | ssize_t _assuan_simple_recvmsg (assuan_context_t ctx, struct msghdr *msg); |
| 303 | #endif |
| 304 | |
| 305 | void _assuan_usleep (unsigned int usec); |
| 306 | |
| 307 | |
| 308 | /*-- assuan-socket.c --*/ |
| 309 | int _assuan_close (assuan_fd_t fd); |
| 310 | assuan_fd_t _assuan_sock_new (int domain, int type, int proto); |
| 311 | int _assuan_sock_connect (assuan_fd_t sockfd, |
| 312 | struct sockaddr *addr, int addrlen); |
| 313 | int _assuan_sock_bind (assuan_fd_t sockfd, struct sockaddr *addr, int addrlen); |
| 314 | int _assuan_sock_get_nonce (struct sockaddr *addr, int addrlen, |
| 315 | assuan_sock_nonce_t *nonce); |
| 316 | int _assuan_sock_check_nonce (assuan_fd_t fd, assuan_sock_nonce_t *nonce); |
| 317 | #ifdef HAVE_W32_SYSTEM |
| 318 | int _assuan_sock_wsa2errno (int err); |
| 319 | #endif |
| 320 | |
| 321 | #ifdef HAVE_FOPENCOOKIE |
| 322 | /* We have to implement funopen in terms of glibc's fopencookie. */ |
| 323 | FILE *_assuan_funopen(void *cookie, |
| 324 | cookie_read_function_t *readfn, |
| 325 | cookie_write_function_t *writefn, |
| 326 | cookie_seek_function_t *seekfn, |
| 327 | cookie_close_function_t *closefn); |
| 328 | #define funopen(a,r,w,s,c) _assuan_funopen ((a), (r), (w), (s), (c)) |
| 329 | #endif /*HAVE_FOPENCOOKIE*/ |
| 330 | |
| 331 | /* Prototypes for replacement functions. */ |
| 332 | #ifndef HAVE_MEMRCHR |
| 333 | void *memrchr (const void *block, int c, size_t size); |
| 334 | #endif |
| 335 | #ifndef HAVE_STPCPY |
| 336 | char *stpcpy (char *dest, const char *src); |
| 337 | #endif |
| 338 | #ifndef HAVE_SETENV |
| 339 | #define setenv _assuan_setenv |
| 340 | #define unsetenv _assuan_unsetenv |
| 341 | #define clearenv _assuan_clearenv |
| 342 | int setenv (const char *name, const char *value, int replace); |
| 343 | #endif |
| 344 | #ifndef HAVE_PUTC_UNLOCKED |
| 345 | int putc_unlocked (int c, FILE *stream); |
| 346 | #endif |
| 347 | |
| 348 | #define DIM(v) (sizeof(v)/sizeof((v)[0])) |
| 349 | #define DIMof(type,member) DIM(((type *)0)->member) |
| 350 | |
| 351 | |
| 352 | #if HAVE_W32_SYSTEM |
| 353 | #define SOCKET2HANDLE(s) ((void *)(s)) |
| 354 | #define HANDLE2SOCKET(h) ((unsigned int)(h)) |
| 355 | #else |
| 356 | #define SOCKET2HANDLE(s) (s) |
| 357 | #define HANDLE2SOCKET(h) (h) |
| 358 | #endif |
| 359 | |
| 360 | |
| 361 | #endif /*ASSUAN_DEFS_H*/ |