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 { "mmdelim1", &mmdlm1 },
94 { "mmdelim2", &mmdlm2 },
95 { "masquerade", &masquerade },
96 { "maildelivery", &maildelivery },
97 { "everyone", &everyone },
98 { "noshell", &NoShell },
104 ** Read the configuration file for the nmh interface
105 ** to the mail transport system (MTS).
113 static int inited = 0;
115 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
117 mts_read_conf_file(fp);
120 cp = get_mtsuserconf_pathname();
122 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
123 mts_read_conf_file(fp);
127 Everyone = atoi(everyone);
129 if (strstr(masquerade, "draft_from") != NULL)
130 draft_from_masquerading = TRUE;
132 if (strstr(masquerade, "mmailid") != NULL)
133 mmailid_masquerading = TRUE;
135 if (strstr(masquerade, "username_extension") != NULL)
136 username_extension_masquerading = TRUE;
143 ** Convert escaped values, malloc some new space,
144 ** and copy string to malloc'ed memory.
148 tailor_value(unsigned char *s)
155 for (bp = buffer; *s; bp++, s++) {
160 case 'b': *bp = '\b'; break;
161 case 'f': *bp = '\f'; break;
162 case 'n': *bp = '\n'; break;
163 case 't': *bp = '\t'; break;
175 r = *s != '0' ? 10 : 8;
176 for (i = 0; isdigit(*s); s++)
177 i = i * r + *s - '0';
186 len = strlen(buffer) + 1;
187 bp = mh_xmalloc(len);
188 memcpy(bp, buffer, len);
194 ** Get the fully qualified name of the local host.
200 static char buffer[BUFSIZ] = "";
201 struct addrinfo hints, *res;
206 /* check if we have cached the local name */
212 memset(buffer, 0, sizeof(buffer));
214 /* first get our local name */
216 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
218 /* first get our local name */
219 gethostname(buffer, sizeof(buffer) - 1);
221 /* now fully qualify our name */
223 memset(&hints, 0, sizeof(hints));
224 hints.ai_flags = AI_CANONNAME;
225 hints.ai_family = PF_UNSPEC;
226 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
227 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
236 ** This is only for UUCP mail. It gets the hostname
237 ** as part of the UUCP "domain".
243 static char buffer[BUFSIZ] = "";
249 /* check if we have cached the system name */
257 strncpy(buffer, name.nodename, sizeof(buffer));
259 gethostname(buffer, sizeof(buffer));
267 ** Get the username of current user
273 if (username[0] == '\0')
281 ** Get full name of current user (typically from GECOS
282 ** field of password file).
288 if (username[0] == '\0')
296 ** Find the user's username and full name, and cache them.
297 ** Also, handle "mmailid" username masquerading controlled from the GECOS field
298 ** of the passwd file.
304 register unsigned char *cp;
306 register struct passwd *pw;
308 if ((pw = getpwuid(getuid())) == NULL
309 || pw->pw_name == NULL
310 || *pw->pw_name == '\0') {
311 strncpy(username, "unknown", sizeof(username));
312 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
320 ** Get the user's real name from the GECOS field. Stop once
321 ** we hit a ',', which some OSes use to separate other 'finger'
322 ** information in the GECOS field, like phone number. Also, if
323 ** mmailid masquerading is turned on due to "mmailid" appearing
324 ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
325 ** (which should precede any ','s).
328 if (mmailid_masquerading)
329 /* Stop at ',' or '<'. */
330 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
335 ** Allow '<' as a legal character of the user's name.
336 ** This code is basically a duplicate of the code above the
337 ** "else" -- we don't collapse it down to one copy and put
338 ** the mmailid_masquerading check inside the loop with "(x
339 ** ? y : z)" because that's inefficient and the value'll
340 ** never change while it's in there.
342 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
346 ** On BSD(-derived) systems, the system utilities that deal with
347 ** the GECOS field (finger, mail, sendmail, etc.) translate
348 ** any '&' character in it to the login name, with the first
349 ** letter capitalized. So, for instance, fingering a user "bob"
350 ** with the GECOS field "& Jones" would reveal him to be "In real
351 ** life: Bob Jones". Surprisingly, though, the OS doesn't do the
352 ** translation for you, so we have to do it manually here.
354 if (mmailid_masquerading)
355 /* Stop at ',' or '<'. */
357 *np != '\0' && *np != ',' && *np != '<';) {
358 if (*np == '&') { /* blech! */
359 strcpy(cp, pw->pw_name);
370 ** Allow '<' as a legal character of the user's name.
371 ** This code is basically a duplicate of the code above the
372 ** "else" -- we don't collapse it down to one copy and put
373 ** the mmailid_masquerading check inside the loop with "(x
374 ** ? y : z)" because that's inefficient and the value'll
375 ** never change while it's in there.
377 for (cp = fullname; *np != '\0' && *np != ',';) {
378 if (*np == '&') { /* blech! */
379 strcpy(cp, pw->pw_name);
391 if (mmailid_masquerading) {
393 ** Do mmailid processing. The GECOS field should have
394 ** the form "Full Name <fakeusername>". For instance,
395 ** "Dan Harkless <Dan.Harkless>". Naturally, you'll want
396 ** your MTA to have an alias (e.g. in /etc/aliases) from
397 ** "fakeusername" to your account name.
401 for (cp = username; *np && *np != '>'; *cp++ = *np++)
405 if (!mmailid_masquerading || *np == '\0')
406 strncpy(username, pw->pw_name, sizeof(username));
409 ** The $SIGNATURE environment variable overrides the GECOS field's
410 ** idea of your real name.
412 if ((cp = getenv("SIGNATURE")) && *cp)
413 strncpy(fullname, cp, sizeof(fullname));
415 if (strchr(fullname, '.')) { /* quote any .'s */
418 /* should quote "'s too */
419 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
420 strncpy(fullname, tmp, sizeof(fullname));
427 get_mtsconf_pathname(void)
429 const char *cp = getenv( "MHMTSCONF ");
430 if (cp != NULL && *cp != '\0') {
437 get_mtsuserconf_pathname(void)
439 const char *cp = getenv( "MHMTSUSERCONF" );
440 if (cp != NULL && *cp != '\0') {
447 mts_read_conf_file(FILE *fp)
450 char *cp, buffer[BUFSIZ];
453 while (fgets(buffer, sizeof(buffer), fp)) {
454 if (!(cp = strchr(buffer, '\n')))
457 if (*buffer == '#' || *buffer == '\0')
459 if (!(bp = strchr(buffer, ':')))
465 for (b = binds; b->keyword; b++)
466 if (!strcmp(buffer, b->keyword))
468 if (b->keyword && (cp = tailor_value(bp)))