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).
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)",
323 ** Get the user's real name from the GECOS field. Stop once
324 ** we hit a ',', which some OSes use to separate other 'finger'
325 ** information in the GECOS field, like phone number. Also, if
326 ** mmailid masquerading is turned on due to "mmailid" appearing
327 ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
328 ** (which should precede any ','s).
331 if (mmailid_masquerading)
332 /* Stop at ',' or '<'. */
333 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
338 ** Allow '<' as a legal character of the user's name.
339 ** This code is basically a duplicate of the code above the
340 ** "else" -- we don't collapse it down to one copy and put
341 ** the mmailid_masquerading check inside the loop with "(x
342 ** ? y : z)" because that's inefficient and the value'll
343 ** never change while it's in there.
345 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
349 ** On BSD(-derived) systems, the system utilities that deal with
350 ** the GECOS field (finger, mail, sendmail, etc.) translate
351 ** any '&' character in it to the login name, with the first
352 ** letter capitalized. So, for instance, fingering a user "bob"
353 ** with the GECOS field "& Jones" would reveal him to be "In real
354 ** life: Bob Jones". Surprisingly, though, the OS doesn't do the
355 ** translation for you, so we have to do it manually here.
357 if (mmailid_masquerading)
358 /* Stop at ',' or '<'. */
360 *np != '\0' && *np != ',' && *np != '<';) {
361 if (*np == '&') { /* blech! */
362 strcpy(cp, pw->pw_name);
373 ** Allow '<' as a legal character of the user's name.
374 ** This code is basically a duplicate of the code above the
375 ** "else" -- we don't collapse it down to one copy and put
376 ** the mmailid_masquerading check inside the loop with "(x
377 ** ? y : z)" because that's inefficient and the value'll
378 ** never change while it's in there.
380 for (cp = fullname; *np != '\0' && *np != ',';) {
381 if (*np == '&') { /* blech! */
382 strcpy(cp, pw->pw_name);
394 if (mmailid_masquerading) {
396 ** Do mmailid processing. The GECOS field should have
397 ** the form "Full Name <fakeusername>". For instance,
398 ** "Dan Harkless <Dan.Harkless>". Naturally, you'll want
399 ** your MTA to have an alias (e.g. in /etc/aliases) from
400 ** "fakeusername" to your account name.
404 for (cp = username; *np && *np != '>'; *cp++ = *np++)
408 if (!mmailid_masquerading || *np == '\0')
409 strncpy(username, pw->pw_name, sizeof(username));
412 ** The $SIGNATURE environment variable overrides the GECOS field's
413 ** idea of your real name.
415 if ((cp = getenv("SIGNATURE")) && *cp)
416 strncpy(fullname, cp, sizeof(fullname));
418 if (strchr(fullname, '.')) { /* quote any .'s */
421 /* should quote "'s too */
422 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
423 strncpy(fullname, tmp, sizeof(fullname));
430 get_mtsconf_pathname(void)
432 const char *cp = getenv( "MHMTSCONF ");
433 if (cp != NULL && *cp != '\0') {
440 get_mtsuserconf_pathname(void)
442 const char *cp = getenv( "MHMTSUSERCONF" );
443 if (cp != NULL && *cp != '\0') {
450 mts_read_conf_file(FILE *fp)
453 char *cp, buffer[BUFSIZ];
456 while (fgets(buffer, sizeof(buffer), fp)) {
457 if (!(cp = strchr(buffer, '\n')))
460 if (*buffer == '#' || *buffer == '\0')
462 if (!(bp = strchr(buffer, ':')))
468 for (b = binds; b->keyword; b++)
469 if (!strcmp(buffer, b->keyword))
471 if (b->keyword && (cp = tailor_value(bp)))