Relayouted all switch statements: case aligns with switch.
[mmh] / sbr / mts.c
1 /*
2 ** mts.c -- definitions for the mail transport system
3 **
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.
7 */
8
9 #include <h/mh.h>   /* for snprintf() */
10 #include <h/nmh.h>
11 #include <h/utils.h>
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <h/mts.h>
15 #include <pwd.h>
16 #include <netdb.h>
17
18 #ifdef HAVE_SYS_UTSNAME_H
19 # include <sys/utsname.h>
20 #endif
21
22 #define NOTOK  (-1)
23 #define OK     0
24
25 /*
26 ** static prototypes
27 */
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);
33
34 /*
35 ** *mmdfldir and *uucpldir are the maildrop directories.  If maildrops
36 ** are kept in the user's home directory, then these should be empty
37 ** strings.  In this case, the appropriate ...lfil array should contain
38 ** the name of the file in the user's home directory.  Usually, this is
39 ** something like ".mail".
40 */
41
42 /*
43 ** nmh mail transport interface customization file
44 */
45 static char *mtsconf = NMHETCDIR"/mts.conf";
46
47 char *mmdfldir = MAILSPOOL;
48 char *mmdflfil = "";
49 char *uucpldir = "/usr/spool/mail";
50 char *uucplfil = "";
51
52 char *mmdlm1 = "\001\001\001\001\n";
53 char *mmdlm2 = "\001\001\001\001\n";
54
55 /* Cache the username and fullname of the user */
56 static char username[BUFSIZ];
57 static char fullname[BUFSIZ];
58
59 /* Variables for username masquerading: */
60 boolean  draft_from_masquerading = FALSE;
61 static boolean  mmailid_masquerading = FALSE;
62 boolean  username_extension_masquerading = FALSE;  /* " from addrsbr.c */
63 static char* masquerade = "";
64
65 /*
66 ** Global MailDelivery file
67 */
68 char *maildelivery = NMHETCDIR"/maildelivery";
69
70
71 /*
72 ** Aliasing Facility (doesn't belong here)
73 */
74 int Everyone = NOTOK;
75 static char *everyone = "-1";
76 char *NoShell = "";
77
78 /*
79 ** Customize the MTS settings for nmh by adjusting
80 ** the file mts.conf in the nmh etc directory.
81 */
82
83 struct bind {
84         char *keyword;
85         char **value;
86 };
87
88 static struct bind binds[] = {
89         { "mmdfldir", &mmdfldir },
90         { "mmdflfil", &mmdflfil },
91         { "uucpldir", &uucpldir },
92         { "uucplfil", &uucplfil },
93         { "mmdelim1", &mmdlm1 },
94         { "mmdelim2", &mmdlm2 },
95         { "masquerade", &masquerade },
96         { "maildelivery", &maildelivery },
97         { "everyone", &everyone },
98         { "noshell", &NoShell },
99         { NULL, NULL }
100 };
101
102
103 /*
104 ** Read the configuration file for the nmh interface
105 ** to the mail transport system (MTS).
106 */
107
108 void
109 mts_init(char *name)
110 {
111         const char *cp;
112         FILE *fp;
113         static int inited = 0;
114
115         if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
116                 return;
117         mts_read_conf_file(fp);
118         fclose(fp);
119
120         cp = get_mtsuserconf_pathname();
121         if (cp != NULL &&
122                 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
123                 mts_read_conf_file(fp);
124                 fclose(fp);
125         }
126
127         Everyone = atoi(everyone);
128
129         if (strstr(masquerade, "draft_from") != NULL)
130                 draft_from_masquerading = TRUE;
131
132         if (strstr(masquerade, "mmailid") != NULL)
133                 mmailid_masquerading = TRUE;
134
135         if (strstr(masquerade, "username_extension") != NULL)
136                 username_extension_masquerading = TRUE;
137 }
138
139
140 #define QUOTE  '\\'
141
142 /*
143 ** Convert escaped values, malloc some new space,
144 ** and copy string to malloc'ed memory.
145 */
146
147 static char *
148 tailor_value(unsigned char *s)
149 {
150         int i, r;
151         char *bp;
152         char buffer[BUFSIZ];
153         size_t len;
154
155         for (bp = buffer; *s; bp++, s++) {
156                 if (*s != QUOTE) {
157                         *bp = *s;
158                 } else {
159                         switch (*++s) {
160                         case 'b': *bp = '\b'; break;
161                         case 'f': *bp = '\f'; break;
162                         case 'n': *bp = '\n'; break;
163                         case 't': *bp = '\t'; break;
164
165                         case 0: s--;
166                         case QUOTE:
167                                 *bp = QUOTE;
168                                 break;
169
170                         default:
171                                 if (!isdigit(*s)) {
172                                         *bp++ = QUOTE;
173                                         *bp = *s;
174                                 }
175                                 r = *s != '0' ? 10 : 8;
176                                 for (i = 0; isdigit(*s); s++)
177                                         i = i * r + *s - '0';
178                                 s--;
179                                 *bp = toascii(i);
180                                 break;
181                         }
182                 }
183         }
184         *bp = 0;
185
186         len = strlen(buffer) + 1;
187         bp = mh_xmalloc(len);
188         memcpy(bp, buffer, len);
189
190         return bp;
191 }
192
193 /*
194 ** Get the fully qualified name of the local host.
195 */
196
197 char *
198 LocalName(void)
199 {
200         static char buffer[BUFSIZ] = "";
201         struct addrinfo hints, *res;
202 #ifdef HAVE_UNAME
203         struct utsname name;
204 #endif
205
206         /* check if we have cached the local name */
207         if (buffer[0])
208                 return buffer;
209
210         mts_init("mts");
211
212         memset(buffer, 0, sizeof(buffer));
213 #ifdef HAVE_UNAME
214         /* first get our local name */
215         uname(&name);
216         strncpy(buffer, name.nodename, sizeof(buffer) - 1);
217 #else
218         /* first get our local name */
219         gethostname(buffer, sizeof(buffer) - 1);
220 #endif
221         /* now fully qualify our name */
222
223         memset(&hints, 0, sizeof(hints));
224         hints.ai_flags = AI_CANONNAME;
225         hints.ai_family = PF_UNSPEC;
226         if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
227                 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
228                 freeaddrinfo(res);
229         }
230
231         return buffer;
232 }
233
234
235 /*
236 ** This is only for UUCP mail.  It gets the hostname
237 ** as part of the UUCP "domain".
238 */
239
240 char *
241 SystemName(void)
242 {
243         static char buffer[BUFSIZ] = "";
244
245 #ifdef HAVE_UNAME
246         struct utsname name;
247 #endif
248
249         /* check if we have cached the system name */
250         if (buffer[0])
251                 return buffer;
252
253         mts_init("mts");
254
255 #ifdef HAVE_UNAME
256         uname(&name);
257         strncpy(buffer, name.nodename, sizeof(buffer));
258 #else
259         gethostname(buffer, sizeof(buffer));
260 #endif
261
262         return buffer;
263 }
264
265
266 /*
267 ** Get the username of current user
268 */
269
270 char *
271 getusername(void)
272 {
273         if (username[0] == '\0')
274                 getuserinfo();
275
276         return username;
277 }
278
279
280 /*
281 ** Get full name of current user (typically from GECOS
282 ** field of password file).
283 */
284
285 char *
286 getfullname(void)
287 {
288         if (username[0] == '\0')
289                 getuserinfo();
290
291         return fullname;
292 }
293
294
295 /*
296 ** Find the user's username and full name, and cache them.
297 ** Also, handle "mmailid" username masquerading controlled from the GECOS field
298 ** of the passwd file.
299 */
300
301 static void
302 getuserinfo(void)
303 {
304         register unsigned char *cp;
305         register char *np;
306         register struct passwd *pw;
307
308         if ((pw = getpwuid(getuid())) == NULL
309                 || pw->pw_name == NULL
310                 || *pw->pw_name == '\0') {
311                 strncpy(username, "unknown", sizeof(username));
312                 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
313                         (int) getuid());
314                 return;
315         }
316
317         np = pw->pw_gecos;
318
319         /*
320         ** Get the user's real name from the GECOS field.  Stop once
321         ** we hit a ',', which some OSes use to separate other 'finger'
322         ** information in the GECOS field, like phone number.  Also, if
323         ** mmailid masquerading is turned on due to "mmailid" appearing
324         ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
325         ** (which should precede any ','s).
326         */
327 #ifndef BSD42
328         if (mmailid_masquerading)
329                 /* Stop at ',' or '<'. */
330                 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
331                         *cp++ = *np++)
332                         continue;
333         else
334                 /*
335                 ** Allow '<' as a legal character of the user's name.
336                 ** This code is basically a duplicate of the code above the
337                 ** "else" -- we don't collapse it down to one copy and put
338                 ** the mmailid_masquerading check inside the loop with "(x
339                 ** ? y : z)" because that's inefficient and the value'll
340                 ** never change while it's in there.
341                 */
342                 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
343                         continue;
344 #else /* BSD42 */
345         /*
346         ** On BSD(-derived) systems, the system utilities that deal with
347         ** the GECOS field (finger, mail, sendmail, etc.) translate
348         ** any '&' character in it to the login name, with the first
349         ** letter capitalized.  So, for instance, fingering a user "bob"
350         ** with the GECOS field "& Jones" would reveal him to be "In real
351         ** life: Bob Jones".  Surprisingly, though, the OS doesn't do the
352         ** translation for you, so we have to do it manually here.
353         */
354         if (mmailid_masquerading)
355                 /* Stop at ',' or '<'. */
356                 for (cp = fullname;
357                         *np != '\0' && *np != ',' && *np != '<';) {
358                         if (*np == '&') {  /* blech! */
359                                 strcpy(cp, pw->pw_name);
360                                 *cp = toupper(*cp);
361                                 while (*cp)
362                                         cp++;
363                                 np++;
364                         } else {
365                                 *cp++ = *np++;
366                         }
367                 }
368         else
369                 /*
370                 ** Allow '<' as a legal character of the user's name.
371                 ** This code is basically a duplicate of the code above the
372                 ** "else" -- we don't collapse it down to one copy and put
373                 ** the mmailid_masquerading check inside the loop with "(x
374                 ** ? y : z)" because that's inefficient and the value'll
375                 ** never change while it's in there.
376                 */
377                 for (cp = fullname; *np != '\0' && *np != ',';) {
378                         if (*np == '&') {  /* blech! */
379                                 strcpy(cp, pw->pw_name);
380                                 *cp = toupper(*cp);
381                                 while (*cp)
382                                         cp++;
383                                 np++;
384                         } else {
385                                 *cp++ = *np++;
386                         }
387                 }
388 #endif /* BSD42 */
389         *cp = '\0';
390
391         if (mmailid_masquerading) {
392                 /*
393                 ** Do mmailid processing.  The GECOS field should have
394                 ** the form "Full Name <fakeusername>".  For instance,
395                 ** "Dan Harkless <Dan.Harkless>".  Naturally, you'll want
396                 ** your MTA to have an alias (e.g. in /etc/aliases) from
397                 ** "fakeusername" to your account name.
398                 */
399                 if (*np)
400                         np++;
401                 for (cp = username; *np && *np != '>'; *cp++ = *np++)
402                         continue;
403                 *cp = '\0';
404         }
405         if (!mmailid_masquerading || *np == '\0')
406                 strncpy(username, pw->pw_name, sizeof(username));
407
408         /*
409         ** The $SIGNATURE environment variable overrides the GECOS field's
410         ** idea of your real name.
411         */
412         if ((cp = getenv("SIGNATURE")) && *cp)
413                 strncpy(fullname, cp, sizeof(fullname));
414
415         if (strchr(fullname, '.')) {  /*  quote any .'s */
416                 char tmp[BUFSIZ];
417
418                 /* should quote "'s too */
419                 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
420                 strncpy(fullname, tmp, sizeof(fullname));
421         }
422
423         return;
424 }
425
426 static const char*
427 get_mtsconf_pathname(void)
428 {
429         const char *cp = getenv( "MHMTSCONF ");
430         if (cp != NULL && *cp != '\0') {
431                 return cp;
432         }
433         return mtsconf;
434 }
435
436 static const char*
437 get_mtsuserconf_pathname(void)
438 {
439         const char *cp = getenv( "MHMTSUSERCONF" );
440         if (cp != NULL && *cp != '\0') {
441                 return cp;
442         }
443         return NULL;
444 }
445
446 static void
447 mts_read_conf_file(FILE *fp)
448 {
449         unsigned char *bp;
450         char *cp, buffer[BUFSIZ];
451         struct bind *b;
452
453         while (fgets(buffer, sizeof(buffer), fp)) {
454                 if (!(cp = strchr(buffer, '\n')))
455                         break;
456                 *cp = 0;
457                 if (*buffer == '#' || *buffer == '\0')
458                         continue;
459                 if (!(bp = strchr(buffer, ':')))
460                         break;
461                 *bp++ = 0;
462                 while (isspace(*bp))
463                         *bp++ = 0;
464
465                 for (b = binds; b->keyword; b++)
466                         if (!strcmp(buffer, b->keyword))
467                                 break;
468                 if (b->keyword && (cp = tailor_value(bp)))
469                         *b->value = cp;
470         }
471 }