3 * mts.c -- definitions for the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 #include <h/mh.h> /* for snprintf() */
14 #define nmhetcdir(file) NMHETCDIR#file
20 #include <sys/socket.h>
26 static char *tailor_value (unsigned char *);
27 static void getuserinfo (void);
28 static const char *get_mtsconf_pathname(void);
29 static const char *get_mtsuserconf_pathname(void);
30 static void mts_read_conf_file (FILE *fp);
33 * *mmdfldir and *uucpldir are the maildrop directories. If maildrops
34 * are kept in the user's home directory, then these should be empty
35 * strings. In this case, the appropriate ...lfil array should contain
36 * the name of the file in the user's home directory. Usually, this is
37 * something like ".mail".
41 * nmh mail transport interface customization file
43 static char *mtsconf = nmhetcdir(/mts.conf);
45 static char *localname = "";
46 static char *localdomain = "";
47 static char *systemname = "";
49 char *mmdfldir = MAILSPOOL;
51 char *uucpldir = "/usr/spool/mail";
54 char *mmdlm1 = "\001\001\001\001\n";
55 char *mmdlm2 = "\001\001\001\001\n";
57 /* Cache the username, fullname, and mailbox of the user */
58 static char username[BUFSIZ];
59 static char fullname[BUFSIZ];
60 static char localmbox[BUFSIZ];
63 * MTS specific variables
65 static char *sm_method = "smtp";
66 int sm_mts = MTS_SMTP;
67 char *hostable = nmhetcdir(/hosts);
68 char *sendmail = SENDMAILPATH;
73 char *clientname = NULL;
74 char *servers = "localhost";
78 * Global MailDelivery file
80 char *maildelivery = nmhetcdir(/maildelivery);
84 * Aliasing Facility (doesn't belong here)
87 static char *everyone = "-1";
91 * Customize the MTS settings for nmh by adjusting
92 * the file mts.conf in the nmh etc directory.
100 static struct bind binds[] = {
101 { "localname", &localname },
102 { "localdomain", &localdomain },
103 { "systemname", &systemname },
104 { "mmdfldir", &mmdfldir },
105 { "mmdflfil", &mmdflfil },
106 { "uucpldir", &uucpldir },
107 { "uucplfil", &uucplfil },
108 { "mmdelim1", &mmdlm1 },
109 { "mmdelim2", &mmdlm2 },
110 { "mts", &sm_method },
111 { "hostable", &hostable },
112 { "sendmail", &sendmail },
113 { "clientname", &clientname },
114 { "servers", &servers },
115 { "pophost", &pophost },
117 { "maildelivery", &maildelivery },
118 { "everyone", &everyone },
119 { "noshell", &NoShell },
125 * Read the configuration file for the nmh interface
126 * to the mail transport system (MTS).
130 mts_init (char *name)
136 static int inited = 0;
138 if (inited++ || (fp = fopen (get_mtsconf_pathname(), "r")) == NULL)
140 mts_read_conf_file(fp);
143 cp = get_mtsuserconf_pathname();
145 ((fp = fopen (get_mtsuserconf_pathname(), "r")) != NULL)) {
146 mts_read_conf_file(fp);
150 Everyone = atoi (everyone);
152 if (strcmp(sm_method, "smtp") == 0)
154 else if (strcmp(sm_method, "sendmail") == 0)
155 sm_mts = MTS_SENDMAIL;
157 advise(NULL, "unsupported \"mts\" value in mts.conf: %s", sm_method);
166 * Convert escaped values, malloc some new space,
167 * and copy string to malloc'ed memory.
171 tailor_value (unsigned char *s)
178 for (bp = buffer; *s; bp++, s++) {
183 case 'b': *bp = '\b'; break;
184 case 'f': *bp = '\f'; break;
185 case 'n': *bp = '\n'; break;
186 case 't': *bp = '\t'; break;
198 r = *s != '0' ? 10 : 8;
199 for (i = 0; isdigit (*s); s++)
200 i = i * r + *s - '0';
209 len = strlen (buffer) + 1;
210 bp = mh_xmalloc (len);
211 memcpy (bp, buffer, len);
217 * Get the fully qualified name of the local host.
219 * If flag is 0, then use anything out of mts.conf (like localname).
220 * If flag is 1, then only use the "proper" local hostname.
226 static char buffer0[BUFSIZ] = "";
227 static char buffer1[BUFSIZ] = "";
228 static char *buffer[] = { buffer0, buffer1 };
230 struct addrinfo hints, *res;
232 if (flag < 0 || flag > 1)
237 /* check if we have cached the local name */
243 /* check if the mts.conf file specifies a "localname" */
244 if (*localname && flag == 0) {
245 strncpy (buf, localname, sizeof(buffer0));
247 memset(buf, 0, sizeof(buffer0));
248 /* first get our local name */
249 gethostname (buf, sizeof(buffer0) - 1);
250 /* now fully qualify our name */
252 memset(&hints, 0, sizeof(hints));
253 hints.ai_flags = AI_CANONNAME;
254 hints.ai_family = PF_UNSPEC;
255 if (getaddrinfo(buf, NULL, &hints, &res) == 0) {
256 strncpy(buf, res->ai_canonname, sizeof(buffer0) - 1);
262 * If the mts.conf file specifies a "localdomain",
263 * we append that now. This should rarely be needed.
267 strcat (buf, localdomain);
275 * This is only for UUCP mail. It gets the hostname
276 * as part of the UUCP "domain".
282 static char buffer[BUFSIZ] = "";
284 /* check if we have cached the system name */
290 /* check if mts.conf file specifies a "systemname" */
292 strncpy (buffer, systemname, sizeof(buffer));
296 gethostname (buffer, sizeof(buffer));
303 * Get the username of current user
309 if (username[0] == '\0')
317 * Get full name of current user (typically from GECOS
318 * field of password file).
324 if (username[0] == '\0')
332 * Get the full local mailbox name. This is in the form:
334 * User Name <user@name.com>
340 if (username[0] == '\0')
343 if (localmbox[0] == '\0') {
346 if ((cp = context_find("Local-Mailbox")) != NULL) {
347 strncpy(localmbox, cp, sizeof(localmbox));
349 snprintf(localmbox, sizeof(localmbox), "%s <%s@%s>", fullname,
350 username, LocalName(0));
353 localmbox[sizeof(localmbox) - 1] = '\0';
360 * Find the user's username and full name, and cache them.
366 register unsigned char *cp;
368 register struct passwd *pw;
370 if ((pw = getpwuid (getuid ())) == NULL
371 || pw->pw_name == NULL
372 || *pw->pw_name == '\0') {
373 strncpy (username, "unknown", sizeof(username));
374 snprintf (fullname, sizeof(fullname), "The Unknown User-ID (%d)",
381 /* Get the user's real name from the GECOS field. Stop once we hit a ',',
382 which some OSes use to separate other 'finger' information in the GECOS
383 field, like phone number. */
384 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
388 strncpy (username, pw->pw_name, sizeof(username));
390 /* The $SIGNATURE environment variable overrides the GECOS field's idea of
391 your real name. If SIGNATURE isn't set, use the Signature profile
392 setting if it exists. */
393 if ((cp = getenv ("SIGNATURE")) && *cp)
394 strncpy (fullname, cp, sizeof(fullname));
395 else if ((cp = context_find("Signature")))
396 strncpy (fullname, cp, sizeof(fullname));
398 fullname[sizeof(fullname) - 1] = '\0';
400 escape_display_name(fullname, sizeof(fullname));
408 get_mtsconf_pathname (void)
410 const char *cp = getenv ( "MHMTSCONF" );
411 if (cp != NULL && *cp != '\0') {
418 get_mtsuserconf_pathname (void)
420 const char *cp = getenv ( "MHMTSUSERCONF" );
421 if (cp != NULL && *cp != '\0') {
428 mts_read_conf_file (FILE *fp)
431 char *cp, buffer[BUFSIZ];
434 while (fgets (buffer, sizeof(buffer), fp)) {
435 if (!(cp = strchr(buffer, '\n')))
438 if (*buffer == '#' || *buffer == '\0')
440 if (!(bp = strchr(buffer, ':')))
443 while (isspace (*bp))
446 for (b = binds; b->keyword; b++)
447 if (!strcmp (buffer, b->keyword))
449 if (b->keyword && (cp = tailor_value (bp)))