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 boolean username_extension_masquerading = FALSE; /* " from addrsbr.c */
47 static char* masquerade = "";
50 ** Global MailDelivery file
52 char *maildelivery = NMHETCDIR"/maildelivery";
55 ** Customize the MTS settings for nmh by adjusting
56 ** the file mts.conf in the nmh etc directory.
64 static struct bind binds[] = {
65 { "masquerade", &masquerade },
66 { "maildelivery", &maildelivery },
72 ** Read the configuration file for the nmh interface
73 ** to the mail transport system (MTS).
81 static int inited = 0;
83 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
85 mts_read_conf_file(fp);
88 cp = get_mtsuserconf_pathname();
90 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
91 mts_read_conf_file(fp);
95 if (strstr(masquerade, "draft_from") != NULL)
96 draft_from_masquerading = TRUE;
98 if (strstr(masquerade, "username_extension") != NULL)
99 username_extension_masquerading = TRUE;
106 ** Convert escaped values, malloc some new space,
107 ** and copy string to malloc'ed memory.
111 tailor_value(unsigned char *s)
118 for (bp = buffer; *s; bp++, s++) {
123 case 'b': *bp = '\b'; break;
124 case 'f': *bp = '\f'; break;
125 case 'n': *bp = '\n'; break;
126 case 't': *bp = '\t'; break;
138 r = *s != '0' ? 10 : 8;
139 for (i = 0; isdigit(*s); s++)
140 i = i * r + *s - '0';
149 len = strlen(buffer) + 1;
150 bp = mh_xmalloc(len);
151 memcpy(bp, buffer, len);
157 ** Get the fully qualified name of the local host.
163 static char buffer[BUFSIZ] = "";
164 struct addrinfo hints, *res;
169 /* check if we have cached the local name */
175 memset(buffer, 0, sizeof(buffer));
177 /* first get our local name */
179 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
181 /* first get our local name */
182 gethostname(buffer, sizeof(buffer) - 1);
184 /* now fully qualify our name */
186 memset(&hints, 0, sizeof(hints));
187 hints.ai_flags = AI_CANONNAME;
188 hints.ai_family = PF_UNSPEC;
189 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
190 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
199 ** This is only for UUCP mail. It gets the hostname
200 ** as part of the UUCP "domain".
206 static char buffer[BUFSIZ] = "";
212 /* check if we have cached the system name */
220 strncpy(buffer, name.nodename, sizeof(buffer));
222 gethostname(buffer, sizeof(buffer));
230 ** Get the username of current user
236 if (username[0] == '\0')
244 ** Get full name of current user (typically from GECOS
245 ** field of password file).
251 if (username[0] == '\0')
259 ** Find the user's username and full name, and cache them.
264 register unsigned char *cp;
266 register struct passwd *pw;
268 if ((pw = getpwuid(getuid())) == NULL
269 || pw->pw_name == NULL
270 || *pw->pw_name == '\0') {
271 strncpy(username, "unknown", sizeof(username));
272 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
280 ** Get the user's real name from the GECOS field. Stop once
281 ** we hit a ',', which some OSes use to separate other 'finger'
282 ** information in the GECOS field, like phone number.
284 for (cp = fullname; *np != '\0' && *np != ',';) {
289 ** On BSD(-derived) systems, the system utilities that
290 ** deal with the GECOS field (finger, mail, sendmail,
291 ** etc.) translate any '&' character in it to the login name,
292 ** with the first letter capitalized. So, for instance,
293 ** fingering a user "bob" with the GECOS field "& Jones"
294 ** would reveal him to be "In real life: Bob Jones".
295 ** Surprisingly, though, the OS doesn't do the translation
296 ** for you, so we have to do it manually here.
298 if (*np == '&') { /* blech! */
299 strcpy(cp, pw->pw_name);
310 strncpy(username, pw->pw_name, sizeof(username));
313 ** The $SIGNATURE environment variable overrides the GECOS field's
314 ** idea of your real name.
316 if ((cp = getenv("SIGNATURE")) && *cp)
317 strncpy(fullname, cp, sizeof(fullname));
319 if (strchr(fullname, '.')) { /* quote any .'s */
322 /* should quote "'s too */
323 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
324 strncpy(fullname, tmp, sizeof(fullname));
331 get_mtsconf_pathname(void)
333 const char *cp = getenv( "MHMTSCONF ");
334 if (cp != NULL && *cp != '\0') {
341 get_mtsuserconf_pathname(void)
343 const char *cp = getenv( "MHMTSUSERCONF" );
344 if (cp != NULL && *cp != '\0') {
351 mts_read_conf_file(FILE *fp)
354 char *cp, buffer[BUFSIZ];
357 while (fgets(buffer, sizeof(buffer), fp)) {
358 if (!(cp = strchr(buffer, '\n')))
361 if (*buffer == '#' || *buffer == '\0')
363 if (!(bp = strchr(buffer, ':')))
369 for (b = binds; b->keyword; b++)
370 if (strcmp(buffer, b->keyword)==0)
372 if (b->keyword && (cp = tailor_value(bp)))