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