Reformated comments and long lines
[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         /*
323         ** Get the user's real name from the GECOS field.  Stop once
324         ** we hit a ',', which some OSes use to separate other 'finger'
325         ** information in the GECOS field, like phone number.  Also, if
326         ** mmailid masquerading is turned on due to "mmailid" appearing
327         ** on the "masquerade:" line of mts.conf, stop if we hit a '<'
328         ** (which should precede any ','s).
329         */
330 #ifndef BSD42
331         if (mmailid_masquerading)
332                 /* Stop at ',' or '<'. */
333                 for (cp = fullname; *np != '\0' && *np != ',' && *np != '<';
334                         *cp++ = *np++)
335                         continue;
336         else
337                 /*
338                 ** Allow '<' as a legal character of the user's name.
339                 ** This code is basically a duplicate of the code above the
340                 ** "else" -- we don't collapse it down to one copy and put
341                 ** the mmailid_masquerading check inside the loop with "(x
342                 ** ? y : z)" because that's inefficient and the value'll
343                 ** never change while it's in there.
344                 */
345                 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
346                         continue;
347 #else /* BSD42 */
348         /*
349         ** On BSD(-derived) systems, the system utilities that deal with
350         ** the GECOS field (finger, mail, sendmail, etc.) translate
351         ** any '&' character in it to the login name, with the first
352         ** letter capitalized.  So, for instance, fingering a user "bob"
353         ** with the GECOS field "& Jones" would reveal him to be "In real
354         ** life: Bob Jones".  Surprisingly, though, the OS doesn't do the
355         ** translation for you, so we have to do it manually here.
356         */
357         if (mmailid_masquerading)
358                 /* Stop at ',' or '<'. */
359                 for (cp = fullname;
360                         *np != '\0' && *np != ',' && *np != '<';) {
361                         if (*np == '&') {  /* blech! */
362                                 strcpy (cp, pw->pw_name);
363                                 *cp = toupper(*cp);
364                                 while (*cp)
365                                         cp++;
366                                 np++;
367                         } else {
368                                 *cp++ = *np++;
369                         }
370                 }
371         else
372                 /*
373                 ** Allow '<' as a legal character of the user's name.
374                 ** This code is basically a duplicate of the code above the
375                 ** "else" -- we don't collapse it down to one copy and put
376                 ** the mmailid_masquerading check inside the loop with "(x
377                 ** ? y : z)" because that's inefficient and the value'll
378                 ** never change while it's in there.
379                 */
380                 for (cp = fullname; *np != '\0' && *np != ',';) {
381                         if (*np == '&') {  /* blech! */
382                                 strcpy (cp, pw->pw_name);
383                                 *cp = toupper(*cp);
384                                 while (*cp)
385                                         cp++;
386                                 np++;
387                         } else {
388                                 *cp++ = *np++;
389                         }
390                 }
391 #endif /* BSD42 */
392         *cp = '\0';
393
394         if (mmailid_masquerading) {
395                 /*
396                 ** Do mmailid processing.  The GECOS field should have
397                 ** the form "Full Name <fakeusername>".  For instance,
398                 ** "Dan Harkless <Dan.Harkless>".  Naturally, you'll want
399                 ** your MTA to have an alias (e.g. in /etc/aliases) from
400                 ** "fakeusername" to your account name.
401                 */
402                 if (*np)
403                         np++;
404                 for (cp = username; *np && *np != '>'; *cp++ = *np++)
405                         continue;
406                 *cp = '\0';
407         }
408         if (!mmailid_masquerading || *np == '\0')
409                 strncpy (username, pw->pw_name, sizeof(username));
410
411         /*
412         ** The $SIGNATURE environment variable overrides the GECOS field's
413         ** idea of your real name.
414         */
415         if ((cp = getenv ("SIGNATURE")) && *cp)
416                 strncpy (fullname, cp, sizeof(fullname));
417
418         if (strchr(fullname, '.')) {  /*  quote any .'s */
419                 char tmp[BUFSIZ];
420
421                 /* should quote "'s too */
422                 snprintf (tmp, sizeof(tmp), "\"%s\"", fullname);
423                 strncpy (fullname, tmp, sizeof(fullname));
424         }
425
426         return;
427 }
428
429 static const char*
430 get_mtsconf_pathname (void)
431 {
432         const char *cp = getenv ( "MHMTSCONF ");
433         if (cp != NULL && *cp != '\0') {
434                 return cp;
435         }
436         return mtsconf;
437 }
438
439 static const char*
440 get_mtsuserconf_pathname (void)
441 {
442         const char *cp = getenv ( "MHMTSUSERCONF" );
443         if (cp != NULL && *cp != '\0') {
444                 return cp;
445         }
446         return NULL;
447 }
448
449 static void
450 mts_read_conf_file (FILE *fp)
451 {
452         unsigned char *bp;
453         char *cp, buffer[BUFSIZ];
454         struct bind *b;
455
456         while (fgets (buffer, sizeof(buffer), fp)) {
457                 if (!(cp = strchr(buffer, '\n')))
458                         break;
459                 *cp = 0;
460                 if (*buffer == '#' || *buffer == '\0')
461                         continue;
462                 if (!(bp = strchr(buffer, ':')))
463                         break;
464                 *bp++ = 0;
465                 while (isspace (*bp))
466                         *bp++ = 0;
467
468                 for (b = binds; b->keyword; b++)
469                         if (!strcmp (buffer, b->keyword))
470                                 break;
471                 if (b->keyword && (cp = tailor_value (bp)))
472                         *b->value = cp;
473         }
474 }