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