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