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() */
18 #ifdef HAVE_SYS_UTSNAME_H
19 # include <sys/utsname.h>
28 static char *tailor_value(unsigned char *);
29 static void getuserinfo(void);
30 static const char *get_mtsconf_pathname(void);
31 static const char *get_mtsuserconf_pathname(void);
32 static void mts_read_conf_file(FILE *fp);
35 ** *mmdfldir and *uucpldir are the maildrop directories. If maildrops
36 ** are kept in the user's home directory, then these should be empty
37 ** strings. In this case, the appropriate ...lfil array should contain
38 ** the name of the file in the user's home directory. Usually, this is
39 ** something like ".mail".
43 ** nmh mail transport interface customization file
45 static char *mtsconf = NMHETCDIR"/mts.conf";
47 char *mmdfldir = MAILSPOOL;
49 char *uucpldir = "/usr/spool/mail";
52 char *mmdlm1 = "\001\001\001\001\n";
53 char *mmdlm2 = "\001\001\001\001\n";
55 /* Cache the username and fullname of the user */
56 static char username[BUFSIZ];
57 static char fullname[BUFSIZ];
59 /* Variables for username masquerading: */
60 boolean draft_from_masquerading = FALSE;
61 static boolean mmailid_masquerading = FALSE;
62 boolean username_extension_masquerading = FALSE; /* " from addrsbr.c */
63 static char* masquerade = "";
66 ** Global MailDelivery file
68 char *maildelivery = NMHETCDIR"/maildelivery";
72 ** Aliasing Facility (doesn't belong here)
75 static char *everyone = "-1";
79 ** Customize the MTS settings for nmh by adjusting
80 ** the file mts.conf in the nmh etc directory.
88 static struct bind binds[] = {
89 { "mmdfldir", &mmdfldir },
90 { "mmdflfil", &mmdflfil },
91 { "uucpldir", &uucpldir },
92 { "uucplfil", &uucplfil },
93 { "masquerade", &masquerade },
94 { "maildelivery", &maildelivery },
95 { "everyone", &everyone },
96 { "noshell", &NoShell },
102 ** Read the configuration file for the nmh interface
103 ** to the mail transport system (MTS).
111 static int inited = 0;
113 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
115 mts_read_conf_file(fp);
118 cp = get_mtsuserconf_pathname();
120 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
121 mts_read_conf_file(fp);
125 Everyone = atoi(everyone);
127 if (strstr(masquerade, "draft_from") != NULL)
128 draft_from_masquerading = TRUE;
130 if (strstr(masquerade, "mmailid") != NULL)
131 mmailid_masquerading = TRUE;
133 if (strstr(masquerade, "username_extension") != NULL)
134 username_extension_masquerading = TRUE;
141 ** Convert escaped values, malloc some new space,
142 ** and copy string to malloc'ed memory.
146 tailor_value(unsigned char *s)
153 for (bp = buffer; *s; bp++, s++) {
158 case 'b': *bp = '\b'; break;
159 case 'f': *bp = '\f'; break;
160 case 'n': *bp = '\n'; break;
161 case 't': *bp = '\t'; break;
173 r = *s != '0' ? 10 : 8;
174 for (i = 0; isdigit(*s); s++)
175 i = i * r + *s - '0';
184 len = strlen(buffer) + 1;
185 bp = mh_xmalloc(len);
186 memcpy(bp, buffer, len);
192 ** Get the fully qualified name of the local host.
198 static char buffer[BUFSIZ] = "";
199 struct addrinfo hints, *res;
204 /* check if we have cached the local name */
210 memset(buffer, 0, sizeof(buffer));
212 /* first get our local name */
214 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
216 /* first get our local name */
217 gethostname(buffer, sizeof(buffer) - 1);
219 /* now fully qualify our name */
221 memset(&hints, 0, sizeof(hints));
222 hints.ai_flags = AI_CANONNAME;
223 hints.ai_family = PF_UNSPEC;
224 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
225 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
234 ** This is only for UUCP mail. It gets the hostname
235 ** as part of the UUCP "domain".
241 static char buffer[BUFSIZ] = "";
247 /* check if we have cached the system name */
255 strncpy(buffer, name.nodename, sizeof(buffer));
257 gethostname(buffer, sizeof(buffer));
265 ** Get the username of current user
271 if (username[0] == '\0')
279 ** Get full name of current user (typically from GECOS
280 ** field of password file).
286 if (username[0] == '\0')
294 ** Find the user's username and full name, and cache them.
295 ** Also, handle "mmailid" username masquerading controlled from the GECOS field
296 ** of the passwd file.
302 register unsigned char *cp;
304 register struct passwd *pw;
306 if ((pw = getpwuid(getuid())) == NULL
307 || pw->pw_name == NULL
308 || *pw->pw_name == '\0') {
309 strncpy(username, "unknown", sizeof(username));
310 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
318 ** Get the user's real name from the GECOS field. Stop once
319 ** we hit a ',', which some OSes use to separate other 'finger'
320 ** information in the GECOS field, like phone number. Also, if
321 ** mmailid masquerading is turned on due to "mmailid" appearing
322 ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
323 ** (which should precede any ','s).
326 if (mmailid_masquerading)
327 /* Stop at ',' or '<'. */
328 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
333 ** Allow '<' as a legal character of the user's name.
334 ** This code is basically a duplicate of the code above the
335 ** "else" -- we don't collapse it down to one copy and put
336 ** the mmailid_masquerading check inside the loop with "(x
337 ** ? y : z)" because that's inefficient and the value'll
338 ** never change while it's in there.
340 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
344 ** On BSD(-derived) systems, the system utilities that deal with
345 ** the GECOS field (finger, mail, sendmail, etc.) translate
346 ** any '&' character in it to the login name, with the first
347 ** letter capitalized. So, for instance, fingering a user "bob"
348 ** with the GECOS field "& Jones" would reveal him to be "In real
349 ** life: Bob Jones". Surprisingly, though, the OS doesn't do the
350 ** translation for you, so we have to do it manually here.
352 if (mmailid_masquerading)
353 /* Stop at ',' or '<'. */
355 *np != '\0' && *np != ',' && *np != '<';) {
356 if (*np == '&') { /* blech! */
357 strcpy(cp, pw->pw_name);
368 ** Allow '<' as a legal character of the user's name.
369 ** This code is basically a duplicate of the code above the
370 ** "else" -- we don't collapse it down to one copy and put
371 ** the mmailid_masquerading check inside the loop with "(x
372 ** ? y : z)" because that's inefficient and the value'll
373 ** never change while it's in there.
375 for (cp = fullname; *np != '\0' && *np != ',';) {
376 if (*np == '&') { /* blech! */
377 strcpy(cp, pw->pw_name);
389 if (mmailid_masquerading) {
391 ** Do mmailid processing. The GECOS field should have
392 ** the form "Full Name <fakeusername>". For instance,
393 ** "Dan Harkless <Dan.Harkless>". Naturally, you'll want
394 ** your MTA to have an alias (e.g. in /etc/aliases) from
395 ** "fakeusername" to your account name.
399 for (cp = username; *np && *np != '>'; *cp++ = *np++)
403 if (!mmailid_masquerading || *np == '\0')
404 strncpy(username, pw->pw_name, sizeof(username));
407 ** The $SIGNATURE environment variable overrides the GECOS field's
408 ** idea of your real name.
410 if ((cp = getenv("SIGNATURE")) && *cp)
411 strncpy(fullname, cp, sizeof(fullname));
413 if (strchr(fullname, '.')) { /* quote any .'s */
416 /* should quote "'s too */
417 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
418 strncpy(fullname, tmp, sizeof(fullname));
425 get_mtsconf_pathname(void)
427 const char *cp = getenv( "MHMTSCONF ");
428 if (cp != NULL && *cp != '\0') {
435 get_mtsuserconf_pathname(void)
437 const char *cp = getenv( "MHMTSUSERCONF" );
438 if (cp != NULL && *cp != '\0') {
445 mts_read_conf_file(FILE *fp)
448 char *cp, buffer[BUFSIZ];
451 while (fgets(buffer, sizeof(buffer), fp)) {
452 if (!(cp = strchr(buffer, '\n')))
455 if (*buffer == '#' || *buffer == '\0')
457 if (!(bp = strchr(buffer, ':')))
463 for (b = binds; b->keyword; b++)
464 if (strcmp(buffer, b->keyword)==0)
466 if (b->keyword && (cp = tailor_value(bp)))