1 /* mbox-util.c - Mail address helper functions
2 * Copyright (C) 1998-2010 Free Software Foundation, Inc.
3 * Copyright (C) 1998-2015 Werner Koch
5 * This file is part of GnuPG.
7 * This file is free software; you can redistribute it and/or modify
8 * it 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.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 /* NB: GPGME uses the same code to reflect our idea on how to extract
22 * a mail address from a user id.
33 #include "mbox-util.h"
37 string_count_chr (const char *string, int c)
41 for (count=0; *string; string++ )
48 mem_count_chr (const void *buffer, int c, size_t length)
50 const char *s = buffer;
53 for (count=0; length; length--, s++)
60 /* This is a case-sensitive version of our memistr. I wonder why no
61 standard function memstr exists but I better do not use the name
62 memstr to avoid future conflicts. */
64 my_memstr (const void *buffer, size_t buflen, const char *sub)
66 const unsigned char *buf = buffer;
67 const unsigned char *t = (const unsigned char *)buf;
68 const unsigned char *s = (const unsigned char *)sub;
71 for ( ; n ; t++, n-- )
75 for (buf = t++, buflen = n--, s++; n && *t ==*s; t++, s++, n--)
78 return (const char*)buf;
79 t = (const unsigned char *)buf;
80 s = (const unsigned char *)sub ;
90 string_has_ctrl_or_space (const char *string)
92 for (; *string; string++ )
93 if (!(*string & 0x80) && *string <= 0x20)
99 /* Return true if STRING has two consecutive '.' after an '@'
102 has_dotdot_after_at (const char *string)
104 string = strchr (string, '@');
106 return 0; /* No at-sign. */
108 return !!strstr (string, "..");
112 /* Check whether BUFFER has characters not valid in an RFC-822
113 address. LENGTH gives the length of BUFFER.
115 To cope with OpenPGP we ignore non-ascii characters so that for
116 example umlauts are legal in an email address. An OpenPGP user ID
117 must be utf-8 encoded but there is no strict requirement for
118 RFC-822. Thus to avoid IDNA encoding we put the address verbatim
119 as utf-8 into the user ID under the assumption that mail programs
120 handle IDNA at a lower level and take OpenPGP user IDs as utf-8.
121 Note that we can't do an utf-8 encoding checking here because in
122 keygen.c this function is called with the native encoding and
123 native to utf-8 encoding is only done later. */
125 has_invalid_email_chars (const void *buffer, size_t length)
127 const unsigned char *s = buffer;
129 const char *valid_chars=
130 "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
132 for ( ; length && *s; length--, s++ )
135 continue; /* We only care about ASCII. */
138 else if (!at_seen && !(strchr (valid_chars, *s)
139 || strchr ("!#$%&'*+/=?^`{|}~", *s)))
141 else if (at_seen && !strchr (valid_chars, *s))
148 /* Same as is_valid_mailbox (see below) but operates on non-nul
149 terminated buffer. */
151 is_valid_mailbox_mem (const void *name_arg, size_t namelen)
153 const char *name = name_arg;
157 || has_invalid_email_chars (name, namelen)
158 || mem_count_chr (name, '@', namelen) != 1
160 || name[namelen-1] == '@'
161 || name[namelen-1] == '.'
162 || my_memstr (name, namelen, ".."));
166 /* Check whether NAME represents a valid mailbox according to
167 RFC822. Returns true if so. */
169 is_valid_mailbox (const char *name)
171 return name? is_valid_mailbox_mem (name, strlen (name)) : 0;
175 /* Return the mailbox (local-part@domain) form a standard user id.
176 All plain ASCII characters in the result are converted to
177 lowercase. Caller must free the result. Returns NULL if no valid
178 mailbox was found (or we are out of memory). */
180 mailbox_from_userid (const char *userid)
182 const char *s, *s_end;
186 s = strchr (userid, '<');
189 /* Seems to be a standard user id. */
191 s_end = strchr (s, '>');
192 if (s_end && s_end > s)
195 result = xtrymalloc (len + 1);
197 return NULL; /* Ooops - out of core. */
198 strncpy (result, s, len);
200 /* Apply some basic checks on the address. We do not use
201 is_valid_mailbox because those checks are too strict. */
202 if (string_count_chr (result, '@') != 1 /* Need exactly one '@. */
203 || *result == '@' /* local-part missing. */
204 || result[len-1] == '@' /* domain missing. */
205 || result[len-1] == '.' /* ends with a dot. */
206 || string_has_ctrl_or_space (result)
207 || has_dotdot_after_at (result))
217 else if (is_valid_mailbox (userid))
219 /* The entire user id is a mailbox. Return that one. Note that
220 this fallback method has some restrictions on the valid
221 syntax of the mailbox. However, those who want weird
222 addresses should know about it and use the regular <...>
224 result = xtrystrdup (userid);
229 return result? ascii_strlwr (result): NULL;
233 /* Check whether UID is a valid standard user id of the form
234 "Heinrich Heine <heinrichh@duesseldorf.de>"
235 and return true if this is the case. */
237 is_valid_user_id (const char *uid)