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 *sendmail = SENDMAILPATH;
72 char *clientname = NULL;
73 char *servers = "localhost";
77 * Global MailDelivery file
79 char *maildelivery = nmhetcdir(/maildelivery);
83 * Aliasing Facility (doesn't belong here)
86 static char *everyone = "-1";
90 * Customize the MTS settings for nmh by adjusting
91 * the file mts.conf in the nmh etc directory.
99 static struct bind binds[] = {
100 { "localname", &localname },
101 { "localdomain", &localdomain },
102 { "systemname", &systemname },
103 { "mmdfldir", &mmdfldir },
104 { "mmdflfil", &mmdflfil },
105 { "uucpldir", &uucpldir },
106 { "uucplfil", &uucplfil },
107 { "mmdelim1", &mmdlm1 },
108 { "mmdelim2", &mmdlm2 },
109 { "mts", &sm_method },
110 { "sendmail", &sendmail },
111 { "clientname", &clientname },
112 { "servers", &servers },
113 { "pophost", &pophost },
115 { "maildelivery", &maildelivery },
116 { "everyone", &everyone },
117 { "noshell", &NoShell },
123 * Read the configuration file for the nmh interface
124 * to the mail transport system (MTS).
128 mts_init (char *name)
132 static int inited = 0;
135 if (inited++ || (fp = fopen (get_mtsconf_pathname(), "r")) == NULL)
137 mts_read_conf_file(fp);
140 cp = get_mtsuserconf_pathname();
142 ((fp = fopen (get_mtsuserconf_pathname(), "r")) != NULL)) {
143 mts_read_conf_file(fp);
147 Everyone = atoi (everyone);
149 if (strcmp(sm_method, "smtp") == 0)
151 else if (strcmp(sm_method, "sendmail") == 0)
152 sm_mts = MTS_SENDMAIL;
154 advise(NULL, "unsupported \"mts\" value in mts.conf: %s", sm_method);
163 * Convert escaped values, malloc some new space,
164 * and copy string to malloc'ed memory.
168 tailor_value (unsigned char *s)
175 for (bp = buffer; *s; bp++, s++) {
180 case 'b': *bp = '\b'; break;
181 case 'f': *bp = '\f'; break;
182 case 'n': *bp = '\n'; break;
183 case 't': *bp = '\t'; break;
195 r = *s != '0' ? 10 : 8;
196 for (i = 0; isdigit (*s); s++)
197 i = i * r + *s - '0';
206 len = strlen (buffer) + 1;
207 bp = mh_xmalloc (len);
208 memcpy (bp, buffer, len);
214 * Get the fully qualified name of the local host.
216 * If flag is 0, then use anything out of mts.conf (like localname).
217 * If flag is 1, then only use the "proper" local hostname.
223 static char buffer0[BUFSIZ] = "";
224 static char buffer1[BUFSIZ] = "";
225 static char *buffer[] = { buffer0, buffer1 };
227 struct addrinfo hints, *res;
229 if (flag < 0 || flag > 1)
234 /* check if we have cached the local name */
240 /* check if the mts.conf file specifies a "localname" */
241 if (*localname && flag == 0) {
242 strncpy (buf, localname, sizeof(buffer0));
244 memset(buf, 0, sizeof(buffer0));
245 /* first get our local name */
246 gethostname (buf, sizeof(buffer0) - 1);
247 /* now fully qualify our name */
249 memset(&hints, 0, sizeof(hints));
250 hints.ai_flags = AI_CANONNAME;
251 hints.ai_family = PF_UNSPEC;
252 if (getaddrinfo(buf, NULL, &hints, &res) == 0) {
253 strncpy(buf, res->ai_canonname, sizeof(buffer0) - 1);
259 * If the mts.conf file specifies a "localdomain",
260 * we append that now. This should rarely be needed.
264 strcat (buf, localdomain);
272 * This is only for UUCP mail. It gets the hostname
273 * as part of the UUCP "domain".
279 static char buffer[BUFSIZ] = "";
281 /* check if we have cached the system name */
287 /* check if mts.conf file specifies a "systemname" */
289 strncpy (buffer, systemname, sizeof(buffer));
293 gethostname (buffer, sizeof(buffer));
300 * Get the username of current user
306 if (username[0] == '\0')
314 * Get full name of current user (typically from GECOS
315 * field of password file).
321 if (username[0] == '\0')
329 * Get the full local mailbox name. This is in the form:
331 * User Name <user@name.com>
337 if (username[0] == '\0')
340 if (localmbox[0] == '\0') {
343 if ((cp = context_find("Local-Mailbox")) != NULL) {
344 strncpy(localmbox, cp, sizeof(localmbox));
346 snprintf(localmbox, sizeof(localmbox), "%s <%s@%s>", fullname,
347 username, LocalName(0));
350 localmbox[sizeof(localmbox) - 1] = '\0';
357 * Find the user's username and full name, and cache them.
363 register unsigned char *cp;
365 register struct passwd *pw;
367 if ((pw = getpwuid (getuid ())) == NULL
368 || pw->pw_name == NULL
369 || *pw->pw_name == '\0') {
370 strncpy (username, "unknown", sizeof(username));
371 snprintf (fullname, sizeof(fullname), "The Unknown User-ID (%d)",
378 /* Get the user's real name from the GECOS field. Stop once we hit a ',',
379 which some OSes use to separate other 'finger' information in the GECOS
380 field, like phone number. */
381 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
385 strncpy (username, pw->pw_name, sizeof(username));
387 /* The $SIGNATURE environment variable overrides the GECOS field's idea of
388 your real name. If SIGNATURE isn't set, use the Signature profile
389 setting if it exists. */
390 if ((cp = getenv ("SIGNATURE")) && *cp)
391 strncpy (fullname, cp, sizeof(fullname));
392 else if ((cp = context_find("Signature")))
393 strncpy (fullname, cp, sizeof(fullname));
395 fullname[sizeof(fullname) - 1] = '\0';
397 escape_display_name(fullname, sizeof(fullname));
405 get_mtsconf_pathname (void)
407 const char *cp = getenv ( "MHMTSCONF" );
408 if (cp != NULL && *cp != '\0') {
415 get_mtsuserconf_pathname (void)
417 const char *cp = getenv ( "MHMTSUSERCONF" );
418 if (cp != NULL && *cp != '\0') {
425 mts_read_conf_file (FILE *fp)
428 char *cp, buffer[BUFSIZ];
431 while (fgets (buffer, sizeof(buffer), fp)) {
432 if (!(cp = strchr(buffer, '\n')))
435 if (*buffer == '#' || *buffer == '\0')
437 if (!(bp = strchr(buffer, ':')))
440 while (isspace (*bp))
443 for (b = binds; b->keyword; b++)
444 if (!strcmp (buffer, b->keyword))
446 if (b->keyword && (cp = tailor_value (bp)))