diff --git a/src/assuan.c b/src/assuan.c index 550f057..c8397da 100644 --- a/src/assuan.c +++ b/src/assuan.c @@ -1,326 +1,326 @@ /* assuan.c - Global interface (not specific to context). * Copyright (C) 2009 Free Software Foundation, Inc. * Copyright (C) 2001, 2002, 2012, 2013 g10 Code GmbH * * This file is part of Assuan. * * Assuan is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Assuan is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifdef HAVE_CONFIG_H #include #endif #include #include "assuan-defs.h" #include "debug.h" #define digitp(a) ((a) >= '0' && (a) <= '9') /* Global default state. */ /* Functions called before and after blocking syscalls. */ static void (*pre_syscall_func) (void); static void (*post_syscall_func) (void); /* Variable to see if functions above are initialized. */ static int _assuan_syscall_func_initialized; /* The default error source gor generated error codes. */ static gpg_err_source_t _assuan_default_err_source = GPG_ERR_SOURCE_USER_1; /* The default memory management functions. */ static struct assuan_malloc_hooks _assuan_default_malloc_hooks = { malloc, realloc, free }; /* The default logging handler. */ static assuan_log_cb_t _assuan_default_log_cb = _assuan_log_handler; static void *_assuan_default_log_cb_data = NULL; /* Set the default gpg error source. */ void assuan_set_gpg_err_source (gpg_err_source_t errsource) { _assuan_default_err_source = errsource; } /* Get the default gpg error source. */ gpg_err_source_t assuan_get_gpg_err_source (void) { return _assuan_default_err_source; } /* Set the default malloc hooks. */ void assuan_set_malloc_hooks (assuan_malloc_hooks_t malloc_hooks) { _assuan_default_malloc_hooks = *malloc_hooks; } /* Get the default malloc hooks. */ assuan_malloc_hooks_t assuan_get_malloc_hooks (void) { return &_assuan_default_malloc_hooks; } /* Set the default log callback handler. */ void assuan_set_log_cb (assuan_log_cb_t log_cb, void *log_cb_data) { _assuan_default_log_cb = log_cb; _assuan_default_log_cb_data = log_cb_data; _assuan_init_log_envvars (); } /* Get the default log callback handler. */ void assuan_get_log_cb (assuan_log_cb_t *log_cb, void **log_cb_data) { *log_cb = _assuan_default_log_cb; *log_cb_data = _assuan_default_log_cb_data; } void assuan_set_system_hooks (assuan_system_hooks_t system_hooks) { _assuan_system_hooks_copy (&_assuan_system_hooks, system_hooks); } /* Used before blocking system calls. */ void _assuan_pre_syscall (void) { again: if (pre_syscall_func) pre_syscall_func (); else if (!_assuan_syscall_func_initialized) { gpgrt_get_syscall_clamp (&pre_syscall_func, &post_syscall_func); _assuan_syscall_func_initialized = 1; goto again; } } /* Used after blocking system calls. */ void _assuan_post_syscall (void) { if (post_syscall_func) post_syscall_func (); } /* Create a new Assuan context. The initial parameters are all needed in the creation of the context. */ gpg_error_t assuan_new_ext (assuan_context_t *r_ctx, gpg_err_source_t err_source, assuan_malloc_hooks_t malloc_hooks, assuan_log_cb_t log_cb, void *log_cb_data) { struct assuan_context_s wctx; assuan_context_t ctx; /* Set up a working context so we can use standard functions. */ memset (&wctx, 0, sizeof (wctx)); wctx.err_source = err_source; wctx.malloc_hooks = *malloc_hooks; wctx.log_cb = log_cb; wctx.log_cb_data = log_cb_data; /* Need a new block for the trace macros to work. */ { TRACE_BEG8 (&wctx, ASSUAN_LOG_CTX, "assuan_new_ext", r_ctx, "err_source = %i (%s), malloc_hooks = %p (%p, %p, %p), " "log_cb = %p, log_cb_data = %p", err_source, gpg_strsource (err_source), malloc_hooks, malloc_hooks->malloc, malloc_hooks->realloc, malloc_hooks->free, log_cb, log_cb_data); *r_ctx = NULL; ctx = _assuan_malloc (&wctx, sizeof (*ctx)); if (!ctx) return TRACE_ERR (gpg_err_code_from_syserror ()); memcpy (ctx, &wctx, sizeof (*ctx)); ctx->system = _assuan_system_hooks; /* FIXME: Delegate to subsystems/engines, as the FDs are not our responsibility (we don't deallocate them, for example). */ ctx->input_fd = ASSUAN_INVALID_FD; ctx->output_fd = ASSUAN_INVALID_FD; ctx->inbound.fd = ASSUAN_INVALID_FD; ctx->outbound.fd = ASSUAN_INVALID_FD; ctx->listen_fd = ASSUAN_INVALID_FD; #if defined(HAVE_W32_SYSTEM) ctx->process_id = -1; #else ctx->pid = ASSUAN_INVALID_PID; #endif - ctx->server_proc = ASSUAN_INVALID_PID; + ctx->server_proc = -1; *r_ctx = ctx; return TRACE_SUC1 ("ctx=%p", ctx); } } /* Create a new context with default arguments. */ gpg_error_t assuan_new (assuan_context_t *r_ctx) { return assuan_new_ext (r_ctx, _assuan_default_err_source, &_assuan_default_malloc_hooks, _assuan_default_log_cb, _assuan_default_log_cb_data); } /* Release all resources associated with an engine operation. */ void _assuan_reset (assuan_context_t ctx) { if (ctx->engine.release) { (*ctx->engine.release) (ctx); ctx->engine.release = NULL; } /* FIXME: Clean standard commands */ } /* Release all resources associated with the given context. */ void assuan_release (assuan_context_t ctx) { if (! ctx) return; TRACE (ctx, ASSUAN_LOG_CTX, "assuan_release", ctx); _assuan_reset (ctx); /* None of the members that are our responsibility requires deallocation. To avoid sensitive data in the line buffers we wipe them out, though. Note that we can't wipe the entire context because it also has a pointer to the actual free(). */ wipememory (&ctx->inbound, sizeof ctx->inbound); wipememory (&ctx->outbound, sizeof ctx->outbound); _assuan_free (ctx, ctx); } /* Version number stuff. */ static const char* parse_version_number (const char *s, int *number) { int val = 0; if (*s == '0' && digitp (s[1])) return NULL; /* Leading zeros are not allowed. */ for (; digitp (*s); s++) { val *= 10; val += *s - '0'; } *number = val; return val < 0 ? NULL : s; } static const char * parse_version_string (const char *s, int *major, int *minor, int *micro) { s = parse_version_number (s, major); if (!s || *s != '.') return NULL; s++; s = parse_version_number (s, minor); if (!s || *s != '.') return NULL; s++; s = parse_version_number (s, micro); if (!s) return NULL; return s; /* Patchlevel. */ } static const char * compare_versions (const char *my_version, const char *req_version) { int my_major, my_minor, my_micro; int rq_major, rq_minor, rq_micro; const char *my_plvl, *rq_plvl; if (!req_version) return my_version; if (!my_version) return NULL; my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro); if (!my_plvl) return NULL; /* Very strange: our own version is bogus. */ rq_plvl = parse_version_string(req_version, &rq_major, &rq_minor, &rq_micro); if (!rq_plvl) return NULL; /* Requested version string is invalid. */ if (my_major > rq_major || (my_major == rq_major && my_minor > rq_minor) || (my_major == rq_major && my_minor == rq_minor && my_micro > rq_micro) || (my_major == rq_major && my_minor == rq_minor && my_micro == rq_micro)) { return my_version; } return NULL; } /* * Check that the the version of the library is at minimum REQ_VERSION * and return the actual version string; return NULL if the condition * is not met. If NULL is passed to this function, no check is done * and the version string is simply returned. */ const char * assuan_check_version (const char *req_version) { if (req_version && req_version[0] == 1 && req_version[1] == 1) return _assuan_sysutils_blurb (); return compare_versions (PACKAGE_VERSION, req_version); } diff --git a/src/client.c b/src/client.c index 9235584..410f940 100644 --- a/src/client.c +++ b/src/client.c @@ -1,349 +1,349 @@ /* client.c - Functions common to all clients. * Copyright (C) 2009 Free Software Foundation, Inc. * * This file is part of Assuan. * * Assuan is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Assuan is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifdef HAVE_CONFIG_H #include #endif #include #include "assuan-defs.h" #include "debug.h" #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \ *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10)) #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1)) void _assuan_client_finish (assuan_context_t ctx) { if (ctx->inbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx, ctx->inbound.fd); if (ctx->inbound.fd == ctx->outbound.fd) ctx->outbound.fd = ASSUAN_INVALID_FD; ctx->inbound.fd = ASSUAN_INVALID_FD; } if (ctx->outbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx, ctx->outbound.fd); ctx->outbound.fd = ASSUAN_INVALID_FD; } - if (ctx->server_proc != ASSUAN_INVALID_PID) + if (ctx->server_proc != -1) { if (!ctx->flags.is_socket) _assuan_waitpid (ctx, ctx->server_proc, ctx->flags.no_waitpid, NULL, 0); - ctx->server_proc = ASSUAN_INVALID_PID; + ctx->server_proc = -1; } _assuan_uds_deinit (ctx); } /* Disconnect and release the context CTX. */ void _assuan_client_release (assuan_context_t ctx) { assuan_write_line (ctx, "BYE"); _assuan_client_finish (ctx); } /* This function also does deescaping for data lines. */ gpg_error_t assuan_client_read_response (assuan_context_t ctx, char **line_r, int *linelen_r) { gpg_error_t rc; char *line = NULL; int linelen = 0; *line_r = NULL; *linelen_r = 0; do { do { rc = _assuan_read_line (ctx); } while (_assuan_error_is_eagain (ctx, rc)); if (rc) return rc; line = ctx->inbound.line; linelen = ctx->inbound.linelen; } while (!linelen); /* For data lines, we deescape immediately. The user will never have to worry about it. */ if (linelen >= 1 && line[0] == 'D' && line[1] == ' ') { char *s, *d; for (s=d=line; linelen; linelen--) { if (*s == '%' && linelen > 2) { /* handle escaping */ s++; *d++ = xtoi_2 (s); s += 2; linelen -= 2; } else *d++ = *s++; } *d = 0; /* add a hidden string terminator */ linelen = d - line; ctx->inbound.linelen = linelen; } *line_r = line; *linelen_r = linelen; return 0; } gpg_error_t assuan_client_parse_response (assuan_context_t ctx, char *line, int linelen, assuan_response_t *response, int *off) { *response = ASSUAN_RESPONSE_ERROR; *off = 0; if (linelen >= 1 && line[0] == 'D' && line[1] == ' ') { *response = ASSUAN_RESPONSE_DATA; /* data line */ *off = 2; } else if (linelen >= 1 && line[0] == 'S' && (line[1] == '\0' || line[1] == ' ')) { *response = ASSUAN_RESPONSE_STATUS; *off = 1; while (line[*off] == ' ') ++*off; } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { *response = ASSUAN_RESPONSE_OK; *off = 2; while (line[*off] == ' ') ++*off; } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { *response = ASSUAN_RESPONSE_ERROR; *off = 3; while (line[*off] == ' ') ++*off; } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { *response = ASSUAN_RESPONSE_INQUIRE; *off = 7; while (line[*off] == ' ') ++*off; } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'N' && line[2] == 'D' && (line[3] == '\0' || line[3] == ' ')) { *response = ASSUAN_RESPONSE_END; *off = 3; } else if (linelen >= 1 && line[0] == '#') { *response = ASSUAN_RESPONSE_COMMENT; *off = 1; } else return _assuan_error (ctx, GPG_ERR_ASS_INV_RESPONSE); return 0; } gpg_error_t _assuan_read_from_server (assuan_context_t ctx, assuan_response_t *response, int *off, int convey_comments) { gpg_error_t rc; char *line; int linelen; do { *response = ASSUAN_RESPONSE_ERROR; *off = 0; rc = assuan_client_read_response (ctx, &line, &linelen); if (!rc) rc = assuan_client_parse_response (ctx, line, linelen, response, off); } while (!rc && *response == ASSUAN_RESPONSE_COMMENT && !convey_comments); return rc; } /** * assuan_transact: * @ctx: The Assuan context * @command: Command line to be send to the server * @data_cb: Callback function for data lines * @data_cb_arg: first argument passed to @data_cb * @inquire_cb: Callback function for a inquire response * @inquire_cb_arg: first argument passed to @inquire_cb * @status_cb: Callback function for a status response * @status_cb_arg: first argument passed to @status_cb * * FIXME: Write documentation * * Return value: 0 on success or an error code. The error code may be * the one one returned by the server via error lines or from the * callback functions. Take care: If a callback returns an error * this function returns immediately with this error. **/ gpg_error_t assuan_transact (assuan_context_t ctx, const char *command, gpg_error_t (*data_cb)(void *, const void *, size_t), void *data_cb_arg, gpg_error_t (*inquire_cb)(void*, const char *), void *inquire_cb_arg, gpg_error_t (*status_cb)(void*, const char *), void *status_cb_arg) { gpg_error_t rc; assuan_response_t response; int off; char *line; int linelen; rc = assuan_write_line (ctx, command); if (rc) return rc; if (*command == '#' || !*command) return 0; /* Don't expect a response for a comment line. */ again: rc = _assuan_read_from_server (ctx, &response, &off, ctx->flags.convey_comments); if (rc) return rc; /* error reading from server */ line = ctx->inbound.line + off; linelen = ctx->inbound.linelen - off; if (response == ASSUAN_RESPONSE_ERROR) rc = atoi (line); else if (response == ASSUAN_RESPONSE_DATA) { if (!data_cb) rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB); else { rc = data_cb (data_cb_arg, line, linelen); if (ctx->flags.confidential) wipememory (ctx->inbound.line, LINELENGTH); if (!rc) goto again; } } else if (response == ASSUAN_RESPONSE_INQUIRE) { if (!inquire_cb) { assuan_write_line (ctx, "END"); /* get out of inquire mode */ _assuan_read_from_server (ctx, &response, &off, 0); /* dummy read */ rc = _assuan_error (ctx, GPG_ERR_ASS_NO_INQUIRE_CB); } else { ctx->flags.confidential_inquiry = 0; ctx->flags.in_inq_cb = 1; rc = inquire_cb (inquire_cb_arg, line); if (!rc) rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */ else { /* Flush and send CAN. */ /* Note that in this error case we don't want to return an error code from sending the cancel. The dummy read is to remove the response from the server which we are not interested in. */ assuan_send_data (ctx, NULL, 1); _assuan_read_from_server (ctx, &response, &off, 0); } if (ctx->flags.confidential_inquiry) wipememory (ctx->outbound.data.line, LINELENGTH); ctx->flags.confidential_inquiry = 0; ctx->flags.in_inq_cb = 0; if (!rc) goto again; } } else if (response == ASSUAN_RESPONSE_STATUS) { if (status_cb) rc = status_cb (status_cb_arg, line); if (!rc) goto again; } else if (response == ASSUAN_RESPONSE_COMMENT && ctx->flags.convey_comments) { line -= off; /* Send line with the comment marker. */ if (status_cb) rc = status_cb (status_cb_arg, line); if (!rc) goto again; } else if (response == ASSUAN_RESPONSE_END) { if (!data_cb) rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB); else { rc = data_cb (data_cb_arg, NULL, 0); if (!rc) goto again; } } return rc; }