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";
56 ** Customize the MTS settings for nmh by adjusting
57 ** the file mts.conf in the nmh etc directory.
65 static struct bind binds[] = {
66 { "masquerade", &masquerade },
67 { "maildelivery", &maildelivery },
73 ** Read the configuration file for the nmh interface
74 ** to the mail transport system (MTS).
82 static int inited = 0;
84 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
86 mts_read_conf_file(fp);
89 cp = get_mtsuserconf_pathname();
91 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
92 mts_read_conf_file(fp);
96 if (strstr(masquerade, "draft_from") != NULL)
97 draft_from_masquerading = TRUE;
99 if (strstr(masquerade, "mmailid") != NULL)
100 mmailid_masquerading = TRUE;
102 if (strstr(masquerade, "username_extension") != NULL)
103 username_extension_masquerading = TRUE;
110 ** Convert escaped values, malloc some new space,
111 ** and copy string to malloc'ed memory.
115 tailor_value(unsigned char *s)
122 for (bp = buffer; *s; bp++, s++) {
127 case 'b': *bp = '\b'; break;
128 case 'f': *bp = '\f'; break;
129 case 'n': *bp = '\n'; break;
130 case 't': *bp = '\t'; break;
142 r = *s != '0' ? 10 : 8;
143 for (i = 0; isdigit(*s); s++)
144 i = i * r + *s - '0';
153 len = strlen(buffer) + 1;
154 bp = mh_xmalloc(len);
155 memcpy(bp, buffer, len);
161 ** Get the fully qualified name of the local host.
167 static char buffer[BUFSIZ] = "";
168 struct addrinfo hints, *res;
173 /* check if we have cached the local name */
179 memset(buffer, 0, sizeof(buffer));
181 /* first get our local name */
183 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
185 /* first get our local name */
186 gethostname(buffer, sizeof(buffer) - 1);
188 /* now fully qualify our name */
190 memset(&hints, 0, sizeof(hints));
191 hints.ai_flags = AI_CANONNAME;
192 hints.ai_family = PF_UNSPEC;
193 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
194 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
203 ** This is only for UUCP mail. It gets the hostname
204 ** as part of the UUCP "domain".
210 static char buffer[BUFSIZ] = "";
216 /* check if we have cached the system name */
224 strncpy(buffer, name.nodename, sizeof(buffer));
226 gethostname(buffer, sizeof(buffer));
234 ** Get the username of current user
240 if (username[0] == '\0')
248 ** Get full name of current user (typically from GECOS
249 ** field of password file).
255 if (username[0] == '\0')
263 ** Find the user's username and full name, and cache them.
264 ** Also, handle "mmailid" username masquerading controlled from the GECOS field
265 ** of the passwd file.
271 register unsigned char *cp;
273 register struct passwd *pw;
275 if ((pw = getpwuid(getuid())) == NULL
276 || pw->pw_name == NULL
277 || *pw->pw_name == '\0') {
278 strncpy(username, "unknown", sizeof(username));
279 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
287 ** Get the user's real name from the GECOS field. Stop once
288 ** we hit a ',', which some OSes use to separate other 'finger'
289 ** information in the GECOS field, like phone number. Also, if
290 ** mmailid masquerading is turned on due to "mmailid" appearing
291 ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
292 ** (which should precede any ','s).
295 if (mmailid_masquerading)
296 /* Stop at ',' or '<'. */
297 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
302 ** Allow '<' as a legal character of the user's name.
303 ** This code is basically a duplicate of the code above the
304 ** "else" -- we don't collapse it down to one copy and put
305 ** the mmailid_masquerading check inside the loop with "(x
306 ** ? y : z)" because that's inefficient and the value'll
307 ** never change while it's in there.
309 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
313 ** On BSD(-derived) systems, the system utilities that deal with
314 ** the GECOS field (finger, mail, sendmail, etc.) translate
315 ** any '&' character in it to the login name, with the first
316 ** letter capitalized. So, for instance, fingering a user "bob"
317 ** with the GECOS field "& Jones" would reveal him to be "In real
318 ** life: Bob Jones". Surprisingly, though, the OS doesn't do the
319 ** translation for you, so we have to do it manually here.
321 if (mmailid_masquerading)
322 /* Stop at ',' or '<'. */
324 *np != '\0' && *np != ',' && *np != '<';) {
325 if (*np == '&') { /* blech! */
326 strcpy(cp, pw->pw_name);
337 ** Allow '<' as a legal character of the user's name.
338 ** This code is basically a duplicate of the code above the
339 ** "else" -- we don't collapse it down to one copy and put
340 ** the mmailid_masquerading check inside the loop with "(x
341 ** ? y : z)" because that's inefficient and the value'll
342 ** never change while it's in there.
344 for (cp = fullname; *np != '\0' && *np != ',';) {
345 if (*np == '&') { /* blech! */
346 strcpy(cp, pw->pw_name);
358 if (mmailid_masquerading) {
360 ** Do mmailid processing. The GECOS field should have
361 ** the form "Full Name <fakeusername>". For instance,
362 ** "Dan Harkless <Dan.Harkless>". Naturally, you'll want
363 ** your MTA to have an alias (e.g. in /etc/aliases) from
364 ** "fakeusername" to your account name.
368 for (cp = username; *np && *np != '>'; *cp++ = *np++)
372 if (!mmailid_masquerading || *np == '\0')
373 strncpy(username, pw->pw_name, sizeof(username));
376 ** The $SIGNATURE environment variable overrides the GECOS field's
377 ** idea of your real name.
379 if ((cp = getenv("SIGNATURE")) && *cp)
380 strncpy(fullname, cp, sizeof(fullname));
382 if (strchr(fullname, '.')) { /* quote any .'s */
385 /* should quote "'s too */
386 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
387 strncpy(fullname, tmp, sizeof(fullname));
394 get_mtsconf_pathname(void)
396 const char *cp = getenv( "MHMTSCONF ");
397 if (cp != NULL && *cp != '\0') {
404 get_mtsuserconf_pathname(void)
406 const char *cp = getenv( "MHMTSUSERCONF" );
407 if (cp != NULL && *cp != '\0') {
414 mts_read_conf_file(FILE *fp)
417 char *cp, buffer[BUFSIZ];
420 while (fgets(buffer, sizeof(buffer), fp)) {
421 if (!(cp = strchr(buffer, '\n')))
424 if (*buffer == '#' || *buffer == '\0')
426 if (!(bp = strchr(buffer, ':')))
432 for (b = binds; b->keyword; b++)
433 if (strcmp(buffer, b->keyword)==0)
435 if (b->keyword && (cp = tailor_value(bp)))