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 ** nmh mail transport interface customization file
37 static char *mtsconf = NMHETCDIR"/mts.conf";
40 /* Cache the username and fullname of the user */
41 static char username[BUFSIZ];
42 static char fullname[BUFSIZ];
44 /* Variables for username masquerading: */
45 boolean draft_from_masquerading = FALSE;
46 static boolean mmailid_masquerading = FALSE;
47 boolean username_extension_masquerading = FALSE; /* " from addrsbr.c */
48 static char* masquerade = "";
51 ** Global MailDelivery file
53 char *maildelivery = NMHETCDIR"/maildelivery";
57 ** Aliasing Facility (doesn't belong here)
60 static char *everyone = "-1";
64 ** Customize the MTS settings for nmh by adjusting
65 ** the file mts.conf in the nmh etc directory.
73 static struct bind binds[] = {
74 { "masquerade", &masquerade },
75 { "maildelivery", &maildelivery },
76 { "everyone", &everyone },
77 { "noshell", &NoShell },
83 ** Read the configuration file for the nmh interface
84 ** to the mail transport system (MTS).
92 static int inited = 0;
94 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
96 mts_read_conf_file(fp);
99 cp = get_mtsuserconf_pathname();
101 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
102 mts_read_conf_file(fp);
106 Everyone = atoi(everyone);
108 if (strstr(masquerade, "draft_from") != NULL)
109 draft_from_masquerading = TRUE;
111 if (strstr(masquerade, "mmailid") != NULL)
112 mmailid_masquerading = TRUE;
114 if (strstr(masquerade, "username_extension") != NULL)
115 username_extension_masquerading = TRUE;
122 ** Convert escaped values, malloc some new space,
123 ** and copy string to malloc'ed memory.
127 tailor_value(unsigned char *s)
134 for (bp = buffer; *s; bp++, s++) {
139 case 'b': *bp = '\b'; break;
140 case 'f': *bp = '\f'; break;
141 case 'n': *bp = '\n'; break;
142 case 't': *bp = '\t'; break;
154 r = *s != '0' ? 10 : 8;
155 for (i = 0; isdigit(*s); s++)
156 i = i * r + *s - '0';
165 len = strlen(buffer) + 1;
166 bp = mh_xmalloc(len);
167 memcpy(bp, buffer, len);
173 ** Get the fully qualified name of the local host.
179 static char buffer[BUFSIZ] = "";
180 struct addrinfo hints, *res;
185 /* check if we have cached the local name */
191 memset(buffer, 0, sizeof(buffer));
193 /* first get our local name */
195 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
197 /* first get our local name */
198 gethostname(buffer, sizeof(buffer) - 1);
200 /* now fully qualify our name */
202 memset(&hints, 0, sizeof(hints));
203 hints.ai_flags = AI_CANONNAME;
204 hints.ai_family = PF_UNSPEC;
205 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
206 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
215 ** This is only for UUCP mail. It gets the hostname
216 ** as part of the UUCP "domain".
222 static char buffer[BUFSIZ] = "";
228 /* check if we have cached the system name */
236 strncpy(buffer, name.nodename, sizeof(buffer));
238 gethostname(buffer, sizeof(buffer));
246 ** Get the username of current user
252 if (username[0] == '\0')
260 ** Get full name of current user (typically from GECOS
261 ** field of password file).
267 if (username[0] == '\0')
275 ** Find the user's username and full name, and cache them.
276 ** Also, handle "mmailid" username masquerading controlled from the GECOS field
277 ** of the passwd file.
283 register unsigned char *cp;
285 register struct passwd *pw;
287 if ((pw = getpwuid(getuid())) == NULL
288 || pw->pw_name == NULL
289 || *pw->pw_name == '\0') {
290 strncpy(username, "unknown", sizeof(username));
291 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
299 ** Get the user's real name from the GECOS field. Stop once
300 ** we hit a ',', which some OSes use to separate other 'finger'
301 ** information in the GECOS field, like phone number. Also, if
302 ** mmailid masquerading is turned on due to "mmailid" appearing
303 ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
304 ** (which should precede any ','s).
307 if (mmailid_masquerading)
308 /* Stop at ',' or '<'. */
309 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
314 ** Allow '<' as a legal character of the user's name.
315 ** This code is basically a duplicate of the code above the
316 ** "else" -- we don't collapse it down to one copy and put
317 ** the mmailid_masquerading check inside the loop with "(x
318 ** ? y : z)" because that's inefficient and the value'll
319 ** never change while it's in there.
321 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
325 ** On BSD(-derived) systems, the system utilities that deal with
326 ** the GECOS field (finger, mail, sendmail, etc.) translate
327 ** any '&' character in it to the login name, with the first
328 ** letter capitalized. So, for instance, fingering a user "bob"
329 ** with the GECOS field "& Jones" would reveal him to be "In real
330 ** life: Bob Jones". Surprisingly, though, the OS doesn't do the
331 ** translation for you, so we have to do it manually here.
333 if (mmailid_masquerading)
334 /* Stop at ',' or '<'. */
336 *np != '\0' && *np != ',' && *np != '<';) {
337 if (*np == '&') { /* blech! */
338 strcpy(cp, pw->pw_name);
349 ** Allow '<' as a legal character of the user's name.
350 ** This code is basically a duplicate of the code above the
351 ** "else" -- we don't collapse it down to one copy and put
352 ** the mmailid_masquerading check inside the loop with "(x
353 ** ? y : z)" because that's inefficient and the value'll
354 ** never change while it's in there.
356 for (cp = fullname; *np != '\0' && *np != ',';) {
357 if (*np == '&') { /* blech! */
358 strcpy(cp, pw->pw_name);
370 if (mmailid_masquerading) {
372 ** Do mmailid processing. The GECOS field should have
373 ** the form "Full Name <fakeusername>". For instance,
374 ** "Dan Harkless <Dan.Harkless>". Naturally, you'll want
375 ** your MTA to have an alias (e.g. in /etc/aliases) from
376 ** "fakeusername" to your account name.
380 for (cp = username; *np && *np != '>'; *cp++ = *np++)
384 if (!mmailid_masquerading || *np == '\0')
385 strncpy(username, pw->pw_name, sizeof(username));
388 ** The $SIGNATURE environment variable overrides the GECOS field's
389 ** idea of your real name.
391 if ((cp = getenv("SIGNATURE")) && *cp)
392 strncpy(fullname, cp, sizeof(fullname));
394 if (strchr(fullname, '.')) { /* quote any .'s */
397 /* should quote "'s too */
398 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
399 strncpy(fullname, tmp, sizeof(fullname));
406 get_mtsconf_pathname(void)
408 const char *cp = getenv( "MHMTSCONF ");
409 if (cp != NULL && *cp != '\0') {
416 get_mtsuserconf_pathname(void)
418 const char *cp = getenv( "MHMTSUSERCONF" );
419 if (cp != NULL && *cp != '\0') {
426 mts_read_conf_file(FILE *fp)
429 char *cp, buffer[BUFSIZ];
432 while (fgets(buffer, sizeof(buffer), fp)) {
433 if (!(cp = strchr(buffer, '\n')))
436 if (*buffer == '#' || *buffer == '\0')
438 if (!(bp = strchr(buffer, ':')))
444 for (b = binds; b->keyword; b++)
445 if (strcmp(buffer, b->keyword)==0)
447 if (b->keyword && (cp = tailor_value(bp)))