diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/argparse.c | 6 | ||||
-rw-r--r-- | common/audit.h | 2 | ||||
-rw-r--r-- | common/convert.c | 2 | ||||
-rw-r--r-- | common/dotlock.c | 2 | ||||
-rw-r--r-- | common/iobuf.c | 58 | ||||
-rw-r--r-- | common/mbox-util.c | 71 | ||||
-rw-r--r-- | common/mbox-util.h | 3 | ||||
-rwxr-xr-x | common/mkerrors | 2 | ||||
-rwxr-xr-x | common/mkerrtok | 2 | ||||
-rw-r--r-- | common/openpgp-oid.c | 4 | ||||
-rw-r--r-- | common/percent.c | 2 | ||||
-rw-r--r-- | common/sexp-parse.h | 2 | ||||
-rw-r--r-- | common/sexputil.c | 2 | ||||
-rw-r--r-- | common/simple-pwquery.c | 3 | ||||
-rw-r--r-- | common/ssh-utils.c | 2 | ||||
-rw-r--r-- | common/stringhelp.c | 2 | ||||
-rw-r--r-- | common/sysutils.c | 11 | ||||
-rw-r--r-- | common/t-exechelp.c | 2 | ||||
-rw-r--r-- | common/t-mbox-util.c | 238 |
19 files changed, 373 insertions, 43 deletions
diff --git a/common/argparse.c b/common/argparse.c index 331998bb2..db0b7e079 100644 --- a/common/argparse.c +++ b/common/argparse.c @@ -408,7 +408,7 @@ static void store_alias( ARGPARSE_ARGS *arg, char *name, char *value ) { /* TODO: replace this dummy function with a rea one - * and fix the probelms IRIX has with (ALIAS_DEV)arg.. + * and fix the problems IRIX has with (ALIAS_DEV)arg.. * used as lvalue */ (void)arg; @@ -439,7 +439,7 @@ ignore_invalid_option_p (ARGPARSE_ARGS *arg, const char *keyword) /* Add the keywords up to the next LF to the list of to be ignored options. After returning FP will either be at EOF or the next - character read wll be the first of a new line. The function + character read will be the first of a new line. The function returns 0 on success or true on malloc failure. */ static int ignore_invalid_option_add (ARGPARSE_ARGS *arg, FILE *fp) @@ -1280,7 +1280,7 @@ long_opt_strlen( ARGPARSE_OPTS *o ) * this option * - a description,ine which starts with a '@' and is followed by * any other characters is printed as is; this may be used for examples - * ans such. + * and such. * - A description which starts with a '|' outputs the string between this * bar and the next one as arguments of the long option. */ diff --git a/common/audit.h b/common/audit.h index 4ef2645da..05f39533d 100644 --- a/common/audit.h +++ b/common/audit.h @@ -185,7 +185,7 @@ typedef enum if no real recipient has been given. */ AUDIT_SESSION_KEY, /* string */ - /* Mark the creation or availibility of the session key. The + /* Mark the creation or availability of the session key. The parameter is the algorithm ID. */ AUDIT_ENCRYPTED_TO, /* cert, err */ diff --git a/common/convert.c b/common/convert.c index 6d03adc3d..40fb4eecf 100644 --- a/common/convert.c +++ b/common/convert.c @@ -177,7 +177,7 @@ bin2hexcolon (const void *buffer, size_t length, char *stringbuf) string or a white space character. The function makes sure that the resulting string in BUFFER is terminated by a Nul byte. Note that the returned string may include embedded Nul bytes; the extra - Nul byte at the end is used to make sure tha the result can always + Nul byte at the end is used to make sure that the result can always be used as a C-string. BUFSIZE is the available length of BUFFER; if the converted result diff --git a/common/dotlock.c b/common/dotlock.c index 5227bb64e..1bc31d8a6 100644 --- a/common/dotlock.c +++ b/common/dotlock.c @@ -140,7 +140,7 @@ you pass (0) instead of (-1) the function does not wait in case the file is already locked but returns -1 and sets ERRNO to EACCES. Any other positive value for the second parameter is considered a - timeout valuie in milliseconds. + timeout value in milliseconds. To release the lock you call: diff --git a/common/iobuf.c b/common/iobuf.c index 02c9b491c..5eeba8fe6 100644 --- a/common/iobuf.c +++ b/common/iobuf.c @@ -68,8 +68,8 @@ /*-- End configurable part. --*/ -/* The size of the iobuffers. This can be chnages using the - * iobuf_set_buffer_size fucntion. */ +/* The size of the iobuffers. This can be changed using the + * iobuf_set_buffer_size function. */ static unsigned int iobuf_buffer_size = DEFAULT_IOBUF_BUFFER_SIZE; @@ -878,9 +878,9 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer, } else if (c == 255) { - a->size = (size_t)iobuf_get (chain) << 24; - a->size |= iobuf_get (chain) << 16; - a->size |= iobuf_get (chain) << 8; + a->size = iobuf_get_noeof (chain) << 24; + a->size |= iobuf_get_noeof (chain) << 16; + a->size |= iobuf_get_noeof (chain) << 8; if ((c = iobuf_get (chain)) == -1) { log_error ("block_filter: invalid 4 byte length\n"); @@ -2610,12 +2610,50 @@ iobuf_read_line (iobuf_t a, byte ** addr_of_buffer, } p = buffer; - while ((c = iobuf_get (a)) != -1) + while (1) { - *p++ = c; - nbytes++; - if (c == '\n') - break; + if (!a->nofast && a->d.start < a->d.len && nbytes < length - 1) + /* Fast path for finding '\n' by using standard C library's optimized + memchr. */ + { + unsigned size = a->d.len - a->d.start; + byte *newline_pos; + + if (size > length - 1 - nbytes) + size = length - 1 - nbytes; + + newline_pos = memchr (a->d.buf + a->d.start, '\n', size); + if (newline_pos) + { + /* Found newline, copy buffer and return. */ + size = (newline_pos - (a->d.buf + a->d.start)) + 1; + memcpy (p, a->d.buf + a->d.start, size); + p += size; + nbytes += size; + a->d.start += size; + a->nbytes += size; + break; + } + else + { + /* No newline, copy buffer and continue. */ + memcpy (p, a->d.buf + a->d.start, size); + p += size; + nbytes += size; + a->d.start += size; + a->nbytes += size; + } + } + else + { + c = iobuf_readbyte (a); + if (c == -1) + break; + *p++ = c; + nbytes++; + if (c == '\n') + break; + } if (nbytes == length - 1) /* We don't have enough space to add a \n and a \0. Increase diff --git a/common/mbox-util.c b/common/mbox-util.c index c1f05b834..a9086a3f5 100644 --- a/common/mbox-util.c +++ b/common/mbox-util.c @@ -173,11 +173,12 @@ is_valid_mailbox (const char *name) /* Return the mailbox (local-part@domain) form a standard user id. - All plain ASCII characters in the result are converted to - lowercase. Caller must free the result. Returns NULL if no valid - mailbox was found (or we are out of memory). */ + * All plain ASCII characters in the result are converted to + * lowercase. If SUBADDRESS is 1, '+' denoted sub-addresses are not + * included in the result. Caller must free the result. Returns NULL + * if no valid mailbox was found (or we are out of memory). */ char * -mailbox_from_userid (const char *userid) +mailbox_from_userid (const char *userid, int subaddress) { const char *s, *s_end; size_t len; @@ -226,6 +227,29 @@ mailbox_from_userid (const char *userid) else errno = EINVAL; + if (result && subaddress == 1) + { + char *atsign, *plus; + + if ((atsign = strchr (result, '@'))) + { + /* We consider a subaddress only if there is a single '+' + * in the local part and the '+' is not the first or last + * character. */ + *atsign = 0; + if ((plus = strchr (result, '+')) + && !strchr (plus+1, '+') + && result != plus + && plus[1] ) + { + *atsign = '@'; + memmove (plus, atsign, strlen (atsign)+1); + } + else + *atsign = '@'; + } + } + return result? ascii_strlwr (result): NULL; } @@ -241,3 +265,42 @@ is_valid_user_id (const char *uid) return 1; } + + +/* Returns true if STRING is a valid domain name according to the LDH + * rule. */ +int +is_valid_domain_name (const char *string) +{ + static char const ldh_chars[] = + "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"; + const char *s; + + /* Note that we do not check the length limit of a label or the + * entire name */ + + for (s=string; *s; s++) + if (*s == '.') + { + if (string == s) + return 0; /* Dot at the start of the string. */ + /* (may also be at the end like in ".") */ + if (s[1] == '.') + return 0; /* No - double dot. */ + } + else if (!strchr (ldh_chars, *s)) + return 0; + else if (*s == '-') + { + if (string == s) + return 0; /* Leading hyphen. */ + if (s[-1] == '.') + return 0; /* Hyphen at begin of a label. */ + if (s[1] == '.') + return 0; /* Hyphen at start of a label. */ + if (!s[1]) + return 0; /* Trailing hyphen. */ + } + + return !!*string; +} diff --git a/common/mbox-util.h b/common/mbox-util.h index bce003f31..10ff2c4a0 100644 --- a/common/mbox-util.h +++ b/common/mbox-util.h @@ -22,8 +22,9 @@ int has_invalid_email_chars (const void *buffer, size_t length); int is_valid_mailbox (const char *name); int is_valid_mailbox_mem (const void *buffer, size_t length); -char *mailbox_from_userid (const char *userid); +char *mailbox_from_userid (const char *userid, int subaddress); int is_valid_user_id (const char *uid); +int is_valid_domain_name (const char *string); #endif /*GNUPG_COMMON_MBOX_UTIL_H*/ diff --git a/common/mkerrors b/common/mkerrors index 138d3c1d1..2a6960ab6 100755 --- a/common/mkerrors +++ b/common/mkerrors @@ -30,7 +30,7 @@ cat <<EOF * gnupg_strerror: * @err: Error code * - * This function returns a textual representaion of the given + * This function returns a textual representation of the given * errorcode. If this is an unknown value, a string with the value * is returned (Beware: it is hold in a static buffer). * diff --git a/common/mkerrtok b/common/mkerrtok index e6310722a..49c10e595 100755 --- a/common/mkerrtok +++ b/common/mkerrtok @@ -26,7 +26,7 @@ cat <<EOF * gnupg_error_token: * @err: Error code * - * This function returns a textual representaion of the given + * This function returns a textual representation of the given * errorcode. If this is an unknown value, a static string is returned. * This function differs from gnupg_strerror that it yields the string * representation of the macro which is never subject to i18n. diff --git a/common/openpgp-oid.c b/common/openpgp-oid.c index d800e7d57..86885e0aa 100644 --- a/common/openpgp-oid.c +++ b/common/openpgp-oid.c @@ -184,7 +184,7 @@ openpgp_oid_from_str (const char *string, gcry_mpi_t *r_mpi) } -/* Return a malloced string represenation of the OID in the opaque MPI +/* Return a malloced string representation of the OID in the opaque MPI A. In case of an error NULL is returned and ERRNO is set. */ char * openpgp_oid_to_str (gcry_mpi_t a) @@ -221,7 +221,7 @@ openpgp_oid_to_str (gcry_mpi_t a) /* To calculate the length of the string we can safely assume an upper limit of 3 decimal characters per byte. Two extra bytes - account for the special first octect */ + account for the special first octet */ string = p = xtrymalloc (length*(1+3)+2+1); if (!string) return NULL; diff --git a/common/percent.c b/common/percent.c index eeb026fbe..7b817684d 100644 --- a/common/percent.c +++ b/common/percent.c @@ -42,7 +42,7 @@ failure. Note that we also escape the quote character to work around a bug - in the mingw32 runtime which does not correcty handle command line + in the mingw32 runtime which does not correctly handle command line quoting. We correctly double the quote mark when calling a program (i.e. gpg-protect-tool), but the pre-main code does not notice the double quote as an escaped quote. We do this also on POSIX systems diff --git a/common/sexp-parse.h b/common/sexp-parse.h index 4f77f1430..0403d65f5 100644 --- a/common/sexp-parse.h +++ b/common/sexp-parse.h @@ -105,7 +105,7 @@ smatch (unsigned char const **buf, size_t buflen, const char *token) } /* Format VALUE for use as the length indicatior of an S-expression. - The caller needs to provide a buffer HELP_BUFFER wth a length of + The caller needs to provide a buffer HELP_BUFFER with a length of HELP_BUFLEN. The return value is a pointer into HELP_BUFFER with the formatted length string. The colon and a trailing nul are appended. HELP_BUFLEN must be at least 3 - a more useful value is diff --git a/common/sexputil.c b/common/sexputil.c index f30790aa1..02e52d0ed 100644 --- a/common/sexputil.c +++ b/common/sexputil.c @@ -303,7 +303,7 @@ make_simple_sexp_from_hexstr (const char *line, size_t *nscanned) for (; n > 1; n -=2, s += 2) *p++ = xtoi_2 (s); *p++ = ')'; - *p = 0; /* (Not really neaded.) */ + *p = 0; /* (Not really needed.) */ return buf; } diff --git a/common/simple-pwquery.c b/common/simple-pwquery.c index e7f4af341..7688c846d 100644 --- a/common/simple-pwquery.c +++ b/common/simple-pwquery.c @@ -75,7 +75,7 @@ /* Name of the socket to be used. This is a kludge to keep on using - the existsing code despite that we only support a standard socket. */ + the existing code despite that we only support a standard socket. */ static char *default_gpg_agent_info; @@ -246,6 +246,7 @@ agent_open (assuan_context_t *ctx) #ifdef SPWQ_USE_LOGGING log_error (_("no gpg-agent running in this session\n")); #endif + *ctx = NULL; return SPWQ_NO_AGENT; } diff --git a/common/ssh-utils.c b/common/ssh-utils.c index 38d6e8aa2..013b28e5b 100644 --- a/common/ssh-utils.c +++ b/common/ssh-utils.c @@ -247,7 +247,7 @@ get_fingerprint (gcry_sexp_t key, int algo, goto leave; } - strncpy (*r_fpr, algo_name, strlen (algo_name)); + memcpy (*r_fpr, algo_name, strlen (algo_name)); fpr = (char *) *r_fpr + strlen (algo_name); *fpr++ = ':'; diff --git a/common/stringhelp.c b/common/stringhelp.c index 0abac8ae5..751e5711f 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -1400,7 +1400,7 @@ parse_version_number (const char *s, int *number) /* This function breaks up the complete string-representation of the - version number S, which is of the following struture: <major + version number S, which is of the following structure: <major number>.<minor number>[.<micro number>]<patch level>. The major, minor, and micro number components will be stored in *MAJOR, *MINOR and *MICRO. If MICRO is not given 0 is used instead. diff --git a/common/sysutils.c b/common/sysutils.c index 55a7ee9ec..0a3dc2eaf 100644 --- a/common/sysutils.c +++ b/common/sysutils.c @@ -551,14 +551,13 @@ gnupg_tmpfile (void) void gnupg_reopen_std (const char *pgmname) { -#if defined(HAVE_STAT) && !defined(HAVE_W32_SYSTEM) - struct stat statbuf; +#ifdef F_GETFD int did_stdin = 0; int did_stdout = 0; int did_stderr = 0; FILE *complain; - if (fstat (STDIN_FILENO, &statbuf) == -1 && errno ==EBADF) + if (fcntl (STDIN_FILENO, F_GETFD) == -1 && errno ==EBADF) { if (open ("/dev/null",O_RDONLY) == STDIN_FILENO) did_stdin = 1; @@ -566,7 +565,7 @@ gnupg_reopen_std (const char *pgmname) did_stdin = 2; } - if (fstat (STDOUT_FILENO, &statbuf) == -1 && errno == EBADF) + if (fcntl (STDOUT_FILENO, F_GETFD) == -1 && errno == EBADF) { if (open ("/dev/null",O_WRONLY) == STDOUT_FILENO) did_stdout = 1; @@ -574,7 +573,7 @@ gnupg_reopen_std (const char *pgmname) did_stdout = 2; } - if (fstat (STDERR_FILENO, &statbuf)==-1 && errno==EBADF) + if (fcntl (STDERR_FILENO, F_GETFD)==-1 && errno==EBADF) { if (open ("/dev/null", O_WRONLY) == STDERR_FILENO) did_stderr = 1; @@ -607,7 +606,7 @@ gnupg_reopen_std (const char *pgmname) if (did_stdin == 2 || did_stdout == 2 || did_stderr == 2) exit (3); -#else /* !(HAVE_STAT && !HAVE_W32_SYSTEM) */ +#else /* !F_GETFD */ (void)pgmname; #endif } diff --git a/common/t-exechelp.c b/common/t-exechelp.c index cf967fcc7..3bf082bbb 100644 --- a/common/t-exechelp.c +++ b/common/t-exechelp.c @@ -131,7 +131,7 @@ test_close_all_fds (void) free (array); /* Now let's check the realloc we use. We do this and the next - tests only if we are allowed to open enought descriptors. */ + tests only if we are allowed to open enough descriptors. */ if (get_max_fds () > 32) { int except[] = { 20, 23, 24, -1 }; diff --git a/common/t-mbox-util.c b/common/t-mbox-util.c index 979d4b37c..ae717f96f 100644 --- a/common/t-mbox-util.c +++ b/common/t-mbox-util.c @@ -25,6 +25,9 @@ #include "util.h" #include "mbox-util.h" +#define PGM "t-mbox-util" + + #define pass() do { ; } while(0) #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\ __FILE__,__LINE__, (a)); \ @@ -32,8 +35,73 @@ } while(0) +static int verbose; +static int debug; + + +static void +run_mbox_test (void) +{ + static struct + { + const char *userid; + const char *mbox; + } testtbl[] = + { + { "Werner Koch <[email protected]>", "[email protected]" }, + { "<[email protected]>", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected] ", NULL }, + { " [email protected]", NULL }, + { "Werner Koch (test) <[email protected]>", "[email protected]" }, + { "Werner Koch <[email protected]> (test)", "[email protected]" }, + { "Werner Koch <[email protected] (test)", NULL }, + { "Werner Koch <[email protected] >", NULL }, + { "Werner Koch <[email protected]", NULL }, + { "", NULL }, + { "@", NULL }, + { "bar <>", NULL }, + { "<[email protected]>", "[email protected]" }, + { "<[email protected]>", "[email protected]" }, + { "<[email protected]>", "[email protected]" }, + { "<[email protected]>", "[email protected]" }, + { "<[email protected]>", "[email protected]" }, + { "<[email protected].>", NULL }, + { "<[email protected]>", NULL }, + { "<foo@.>", NULL }, + { "<@example.org>", NULL }, + { "<foo@@example.org>", NULL }, + { "<@[email protected]>", NULL }, + { "<[email protected]> ()", "[email protected]" }, + { "<fo()[email protected]> ()", "fo()[email protected]" }, + { "<fo()[email protected]> ()", "fo()[email protected]" }, + { "fo()[email protected]", NULL}, + { NULL, NULL } + }; + int idx; + + for (idx=0; testtbl[idx].userid; idx++) + { + char *mbox = mailbox_from_userid (testtbl[idx].userid, 0); + + if (!testtbl[idx].mbox) + { + if (mbox) + fail (idx); + } + else if (!mbox) + fail (idx); + else if (strcmp (mbox, testtbl[idx].mbox)) + fail (idx); + + xfree (mbox); + } +} + + static void -run_test (void) +run_mbox_no_sub_test (void) { static struct { @@ -41,6 +109,7 @@ run_test (void) const char *mbox; } testtbl[] = { + { "[email protected]", "[email protected]" }, { "Werner Koch <[email protected]>", "[email protected]" }, { "<[email protected]>", "[email protected]" }, { "[email protected]", "[email protected]" }, @@ -70,13 +139,30 @@ run_test (void) { "<fo()[email protected]> ()", "fo()[email protected]" }, { "fo()[email protected]", NULL}, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { "[email protected]", "[email protected]" }, + { NULL, NULL } }; int idx; for (idx=0; testtbl[idx].userid; idx++) { - char *mbox = mailbox_from_userid (testtbl[idx].userid); + char *mbox = mailbox_from_userid (testtbl[idx].userid, 1); if (!testtbl[idx].mbox) { @@ -93,13 +179,155 @@ run_test (void) } +static void +run_dns_test (void) +{ + static struct + { + const char *name; + int valid; + } testtbl[] = + { + { "", 0 }, + { ".", 0 }, + { "-", 0 }, + { "a", 1 }, + { "ab", 1 }, + { "a.b", 1 }, + { "a.b.", 1 }, + { ".a.b.", 0 }, + { ".a.b", 0 }, + { "-a.b", 0 }, + { "a-.b", 0 }, + { "a.-b", 0 }, + { "a.b-", 0 }, + { "a.b-.", 0 }, + { "a..b", 0 }, + { "ab.c", 1 }, + { "a-b.c", 1 }, + { "a-b-.c", 0 }, + { "-a-b.c", 0 }, + { "example.org", 1 }, + { "x.example.org", 1 }, + { "xy.example.org", 1 }, + { "Xy.example.org", 1 }, + { "-Xy.example.org", 0 }, + { "Xy.example-.org", 0 }, + { "foo.example.org..", 0 }, + { "foo.example.org.", 1 }, + { ".foo.example.org.", 0 }, + { "..foo.example.org.", 0 }, + { NULL, 0 } + }; + int idx; + + for (idx=0; testtbl[idx].name; idx++) + { + if (is_valid_domain_name (testtbl[idx].name) != testtbl[idx].valid) + fail (idx); + } +} + + +static void +run_filter (int no_sub) +{ + char buf[4096]; + int c; + char *p, *mbox; + unsigned int count1 = 0; + unsigned int count2 = 0; + + while (fgets (buf, sizeof buf, stdin)) + { + p = strchr (buf, '\n'); + if (p) + *p = 0; + else + { + /* Skip to the end of the line. */ + while ((c = getc (stdin)) != EOF && c != '\n') + ; + } + count1++; + trim_spaces (buf); + mbox = mailbox_from_userid (buf, no_sub); + if (mbox) + { + printf ("%s\n", mbox); + xfree (mbox); + count2++; + } + } + if (verbose) + fprintf (stderr, PGM ": lines=%u mboxes=%u\n", count1, count2); +} + + int main (int argc, char **argv) { - (void)argc; - (void)argv; + int last_argc = -1; + int opt_filter = 0; + int opt_no_sub = 0; - run_test (); + if (argc) + { argc--; argv++; } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--help")) + { + fputs ("usage: " PGM " [FILE]\n" + "Options:\n" + " --verbose Print timings etc.\n" + " --debug Flyswatter\n" + " --filter Filter mboxes from input lines\n" + " --no-sub Ignore '+'-sub-addresses\n" + , stdout); + exit (0); + } + else if (!strcmp (*argv, "--verbose")) + { + verbose++; + argc--; argv++; + } + else if (!strcmp (*argv, "--debug")) + { + verbose += 2; + debug++; + argc--; argv++; + } + else if (!strcmp (*argv, "--filter")) + { + opt_filter = 1; + argc--; argv++; + } + else if (!strcmp (*argv, "--no-sub")) + { + opt_no_sub = 1; + argc--; argv++; + } + else if (!strncmp (*argv, "--", 2)) + { + fprintf (stderr, PGM ": unknown option '%s'\n", *argv); + exit (1); + } + } + + if (opt_filter) + run_filter (opt_no_sub); + else + { + run_mbox_test (); + run_mbox_no_sub_test (); + run_dns_test (); + } return 0; } |