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 username_extension_masquerading = FALSE; /* " from addrsbr.c */
46 static char* masquerade = "";
49 ** Global MailDelivery file
51 char *maildelivery = NMHETCDIR"/maildelivery";
54 ** Customize the MTS settings for nmh by adjusting
55 ** the file mts.conf in the nmh etc directory.
63 static struct bind binds[] = {
64 { "masquerade", &masquerade },
65 { "maildelivery", &maildelivery },
71 ** Read the configuration file for the nmh interface
72 ** to the mail transport system (MTS).
80 static int inited = 0;
82 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
84 mts_read_conf_file(fp);
87 cp = get_mtsuserconf_pathname();
89 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
90 mts_read_conf_file(fp);
94 if (strstr(masquerade, "username_extension") != NULL)
95 username_extension_masquerading = TRUE;
102 ** Convert escaped values, malloc some new space,
103 ** and copy string to malloc'ed memory.
107 tailor_value(unsigned char *s)
114 for (bp = buffer; *s; bp++, s++) {
119 case 'b': *bp = '\b'; break;
120 case 'f': *bp = '\f'; break;
121 case 'n': *bp = '\n'; break;
122 case 't': *bp = '\t'; break;
134 r = *s != '0' ? 10 : 8;
135 for (i = 0; isdigit(*s); s++)
136 i = i * r + *s - '0';
145 len = strlen(buffer) + 1;
146 bp = mh_xmalloc(len);
147 memcpy(bp, buffer, len);
153 ** Get the fully qualified name of the local host.
159 static char buffer[BUFSIZ] = "";
160 struct addrinfo hints, *res;
165 /* check if we have cached the local name */
171 memset(buffer, 0, sizeof(buffer));
173 /* first get our local name */
175 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
177 /* first get our local name */
178 gethostname(buffer, sizeof(buffer) - 1);
180 /* now fully qualify our name */
182 memset(&hints, 0, sizeof(hints));
183 hints.ai_flags = AI_CANONNAME;
184 hints.ai_family = PF_UNSPEC;
185 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
186 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
195 ** This is only for UUCP mail. It gets the hostname
196 ** as part of the UUCP "domain".
202 static char buffer[BUFSIZ] = "";
208 /* check if we have cached the system name */
216 strncpy(buffer, name.nodename, sizeof(buffer));
218 gethostname(buffer, sizeof(buffer));
226 ** Get the username of current user
232 if (username[0] == '\0')
240 ** Get full name of current user (typically from GECOS
241 ** field of password file).
247 if (username[0] == '\0')
255 ** Find the user's username and full name, and cache them.
260 register unsigned char *cp;
262 register struct passwd *pw;
264 if ((pw = getpwuid(getuid())) == NULL
265 || pw->pw_name == NULL
266 || *pw->pw_name == '\0') {
267 strncpy(username, "unknown", sizeof(username));
268 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
276 ** Get the user's real name from the GECOS field. Stop once
277 ** we hit a ',', which some OSes use to separate other 'finger'
278 ** information in the GECOS field, like phone number.
280 for (cp = fullname; *np != '\0' && *np != ',';) {
285 ** On BSD(-derived) systems, the system utilities that
286 ** deal with the GECOS field (finger, mail, sendmail,
287 ** etc.) translate any '&' character in it to the login name,
288 ** with the first letter capitalized. So, for instance,
289 ** fingering a user "bob" with the GECOS field "& Jones"
290 ** would reveal him to be "In real life: Bob Jones".
291 ** Surprisingly, though, the OS doesn't do the translation
292 ** for you, so we have to do it manually here.
294 if (*np == '&') { /* blech! */
295 strcpy(cp, pw->pw_name);
306 strncpy(username, pw->pw_name, sizeof(username));
309 ** The $SIGNATURE environment variable overrides the GECOS field's
310 ** idea of your real name.
312 if ((cp = getenv("SIGNATURE")) && *cp)
313 strncpy(fullname, cp, sizeof(fullname));
315 if (strchr(fullname, '.')) { /* quote any .'s */
318 /* should quote "'s too */
319 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
320 strncpy(fullname, tmp, sizeof(fullname));
327 get_mtsconf_pathname(void)
329 const char *cp = getenv( "MHMTSCONF ");
330 if (cp != NULL && *cp != '\0') {
337 get_mtsuserconf_pathname(void)
339 const char *cp = getenv( "MHMTSUSERCONF" );
340 if (cp != NULL && *cp != '\0') {
347 mts_read_conf_file(FILE *fp)
350 char *cp, buffer[BUFSIZ];
353 while (fgets(buffer, sizeof(buffer), fp)) {
354 if (!(cp = strchr(buffer, '\n')))
357 if (*buffer == '#' || *buffer == '\0')
359 if (!(bp = strchr(buffer, ':')))
365 for (b = binds; b->keyword; b++)
366 if (strcmp(buffer, b->keyword)==0)
368 if (b->keyword && (cp = tailor_value(bp)))