2 * mts.c -- definitions for the mail transport system
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
9 #include <h/mh.h> /* for snprintf() */
13 #define nmhetcdir(file) NMHETCDIR#file
21 #ifdef HAVE_SYS_UTSNAME_H
22 # include <sys/utsname.h>
31 static char *tailor_value (unsigned char *);
32 static void getuserinfo (void);
33 static const char *get_mtsconf_pathname(void);
34 static const char *get_mtsuserconf_pathname(void);
35 static void mts_read_conf_file (FILE *fp);
38 * *mmdfldir and *uucpldir are the maildrop directories. If maildrops
39 * are kept in the user's home directory, then these should be empty
40 * strings. In this case, the appropriate ...lfil array should contain
41 * the name of the file in the user's home directory. Usually, this is
42 * something like ".mail".
46 * nmh mail transport interface customization file
48 static char *mtsconf = nmhetcdir(/mts.conf);
50 char *mmdfldir = MAILSPOOL;
52 char *uucpldir = "/usr/spool/mail";
55 char *mmdlm1 = "\001\001\001\001\n";
56 char *mmdlm2 = "\001\001\001\001\n";
58 /* Cache the username and fullname of the user */
59 static char username[BUFSIZ];
60 static char fullname[BUFSIZ];
62 /* Variables for username masquerading: */
63 boolean draft_from_masquerading = FALSE;
64 static boolean mmailid_masquerading = FALSE;
65 boolean username_extension_masquerading = FALSE; /* " from addrsbr.c */
66 static char* masquerade = "";
69 * Global MailDelivery file
71 char *maildelivery = nmhetcdir(/maildelivery);
75 * Aliasing Facility (doesn't belong here)
78 static char *everyone = "-1";
82 * Customize the MTS settings for nmh by adjusting
83 * the file mts.conf in the nmh etc directory.
91 static struct bind binds[] = {
92 { "mmdfldir", &mmdfldir },
93 { "mmdflfil", &mmdflfil },
94 { "uucpldir", &uucpldir },
95 { "uucplfil", &uucplfil },
96 { "mmdelim1", &mmdlm1 },
97 { "mmdelim2", &mmdlm2 },
98 { "masquerade", &masquerade },
99 { "maildelivery", &maildelivery },
100 { "everyone", &everyone },
101 { "noshell", &NoShell },
107 * Read the configuration file for the nmh interface
108 * to the mail transport system (MTS).
112 mts_init (char *name)
116 static int inited = 0;
118 if (inited++ || (fp = fopen (get_mtsconf_pathname(), "r")) == NULL)
120 mts_read_conf_file(fp);
123 cp = get_mtsuserconf_pathname();
125 ((fp = fopen (get_mtsuserconf_pathname(), "r")) != NULL)) {
126 mts_read_conf_file(fp);
130 Everyone = atoi (everyone);
132 if (strstr(masquerade, "draft_from") != NULL)
133 draft_from_masquerading = TRUE;
135 if (strstr(masquerade, "mmailid") != NULL)
136 mmailid_masquerading = TRUE;
138 if (strstr(masquerade, "username_extension") != NULL)
139 username_extension_masquerading = TRUE;
146 * Convert escaped values, malloc some new space,
147 * and copy string to malloc'ed memory.
151 tailor_value (unsigned char *s)
158 for (bp = buffer; *s; bp++, s++) {
163 case 'b': *bp = '\b'; break;
164 case 'f': *bp = '\f'; break;
165 case 'n': *bp = '\n'; break;
166 case 't': *bp = '\t'; break;
178 r = *s != '0' ? 10 : 8;
179 for (i = 0; isdigit (*s); s++)
180 i = i * r + *s - '0';
189 len = strlen (buffer) + 1;
190 bp = mh_xmalloc (len);
191 memcpy (bp, buffer, len);
197 * Get the fully qualified name of the local host.
203 static char buffer[BUFSIZ] = "";
204 struct addrinfo hints, *res;
209 /* check if we have cached the local name */
215 memset(buffer, 0, sizeof(buffer));
217 /* first get our local name */
219 strncpy (buffer, name.nodename, sizeof(buffer) - 1);
221 /* first get our local name */
222 gethostname (buffer, sizeof(buffer) - 1);
224 /* now fully qualify our name */
226 memset(&hints, 0, sizeof(hints));
227 hints.ai_flags = AI_CANONNAME;
228 hints.ai_family = PF_UNSPEC;
229 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
230 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
239 * This is only for UUCP mail. It gets the hostname
240 * as part of the UUCP "domain".
246 static char buffer[BUFSIZ] = "";
252 /* check if we have cached the system name */
260 strncpy (buffer, name.nodename, sizeof(buffer));
262 gethostname (buffer, sizeof(buffer));
270 * Get the username of current user
276 if (username[0] == '\0')
284 * Get full name of current user (typically from GECOS
285 * field of password file).
291 if (username[0] == '\0')
299 * Find the user's username and full name, and cache them.
300 * Also, handle "mmailid" username masquerading controlled from the GECOS field
301 * of the passwd file.
307 register unsigned char *cp;
309 register struct passwd *pw;
311 if ((pw = getpwuid (getuid ())) == NULL
312 || pw->pw_name == NULL
313 || *pw->pw_name == '\0') {
314 strncpy (username, "unknown", sizeof(username));
315 snprintf (fullname, sizeof(fullname), "The Unknown User-ID (%d)",
322 /* Get the user's real name from the GECOS field. Stop once we hit a ',',
323 * which some OSes use to separate other 'finger' information in the GECOS
324 * field, like phone number. Also, if mmailid masquerading is turned on due
325 * to "mmailid" appearing on the "masquerade:" line of mts.conf, stop if we
326 * hit a '<' (which should precede any ','s). */
328 if (mmailid_masquerading)
329 /* Stop at ',' or '<'. */
330 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
334 /* Allow '<' as a legal character of the user's name. This code is
335 * basically a duplicate of the code above the "else" -- we don't
336 * collapse it down to one copy and put the mmailid_masquerading check
337 * inside the loop with "(x ? y : z)" because that's inefficient and the
338 * value'll never change while it's in there. */
339 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
342 /* On BSD(-derived) systems, the system utilities that deal with the GECOS
343 * field (finger, mail, sendmail, etc.) translate any '&' character in it to
344 * the login name, with the first letter capitalized. So, for instance,
345 * fingering a user "bob" with the GECOS field "& Jones" would reveal him to
346 * be "In real life: Bob Jones". Surprisingly, though, the OS doesn't do
347 * the translation for you, so we have to do it manually here. */
348 if (mmailid_masquerading)
349 /* Stop at ',' or '<'. */
351 *np != '\0' && *np != ',' && *np != '<';) {
352 if (*np == '&') { /* blech! */
353 strcpy (cp, pw->pw_name);
363 /* Allow '<' as a legal character of the user's name. This code is
364 * basically a duplicate of the code above the "else" -- we don't
365 * collapse it down to one copy and put the mmailid_masquerading check
366 * inside the loop with "(x ? y : z)" because that's inefficient and the
367 * value'll never change while it's in there. */
368 for (cp = fullname; *np != '\0' && *np != ',';) {
369 if (*np == '&') { /* blech! */
370 strcpy (cp, pw->pw_name);
382 if (mmailid_masquerading) {
383 /* Do mmailid processing. The GECOS field should have the form
384 * "Full Name <fakeusername>". For instance,
385 * "Dan Harkless <Dan.Harkless>". Naturally, you'll want your MTA to
386 * have an alias (e.g. in /etc/aliases) from "fakeusername" to your
390 for (cp = username; *np && *np != '>'; *cp++ = *np++)
394 if (!mmailid_masquerading || *np == '\0')
395 strncpy (username, pw->pw_name, sizeof(username));
397 /* The $SIGNATURE environment variable overrides the GECOS field's
398 * idea of your real name. */
399 if ((cp = getenv ("SIGNATURE")) && *cp)
400 strncpy (fullname, cp, sizeof(fullname));
402 if (strchr(fullname, '.')) { /* quote any .'s */
405 /* should quote "'s too */
406 snprintf (tmp, sizeof(tmp), "\"%s\"", fullname);
407 strncpy (fullname, tmp, sizeof(fullname));
414 get_mtsconf_pathname (void)
416 const char *cp = getenv ( "MHMTSCONF ");
417 if (cp != NULL && *cp != '\0') {
424 get_mtsuserconf_pathname (void)
426 const char *cp = getenv ( "MHMTSUSERCONF" );
427 if (cp != NULL && *cp != '\0') {
434 mts_read_conf_file (FILE *fp)
437 char *cp, buffer[BUFSIZ];
440 while (fgets (buffer, sizeof(buffer), fp)) {
441 if (!(cp = strchr(buffer, '\n')))
444 if (*buffer == '#' || *buffer == '\0')
446 if (!(bp = strchr(buffer, ':')))
449 while (isspace (*bp))
452 for (b = binds; b->keyword; b++)
453 if (!strcmp (buffer, b->keyword))
455 if (b->keyword && (cp = tailor_value (bp)))