]> diplodocus.org Git - nmh/blob - sbr/mts.c
Formatting cleanup.
[nmh] / sbr / mts.c
1
2 /*
3 * mts.c -- definitions for the mail transport system
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h> /* for snprintf() */
11 #include <h/nmh.h>
12 #include <h/utils.h>
13
14 #define nmhetcdir(file) NMHETCDIR#file
15
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <h/mts.h>
19 #include <pwd.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
22
23 /*
24 * static prototypes
25 */
26 static char *tailor_value (unsigned char *);
27 static void getuserinfo (void);
28 static const char *get_mtsconf_pathname(void);
29 static const char *get_mtsuserconf_pathname(void);
30 static void mts_read_conf_file (FILE *fp);
31
32 /*
33 * *mmdfldir and *uucpldir are the maildrop directories. If maildrops
34 * are kept in the user's home directory, then these should be empty
35 * strings. In this case, the appropriate ...lfil array should contain
36 * the name of the file in the user's home directory. Usually, this is
37 * something like ".mail".
38 */
39
40 /*
41 * nmh mail transport interface customization file
42 */
43 static char *mtsconf = nmhetcdir(/mts.conf);
44
45 static char *localname = "";
46 static char *localdomain = "";
47 static char *systemname = "";
48
49 char *mmdfldir = MAILSPOOL;
50 char *mmdflfil = "";
51 char *uucpldir = "/usr/spool/mail";
52 char *uucplfil = "";
53
54 char *mmdlm1 = "\001\001\001\001\n";
55 char *mmdlm2 = "\001\001\001\001\n";
56
57 /* Cache the username, fullname, and mailbox of the user */
58 static char username[BUFSIZ];
59 static char fullname[BUFSIZ];
60 static char localmbox[BUFSIZ];
61
62 /*
63 * MTS specific variables
64 */
65 static char *sm_method = "smtp";
66 int sm_mts = MTS_SMTP;
67 char *sendmail = SENDMAILPATH;
68
69 /*
70 * SMTP/POP stuff
71 */
72 char *clientname = NULL;
73 char *servers = "localhost";
74 char *pophost = "";
75
76 /*
77 * Global MailDelivery file
78 */
79 char *maildelivery = nmhetcdir(/maildelivery);
80
81
82 /*
83 * Aliasing Facility (doesn't belong here)
84 */
85 int Everyone = NOTOK;
86 static char *everyone = "-1";
87 char *NoShell = "";
88
89 /*
90 * Customize the MTS settings for nmh by adjusting
91 * the file mts.conf in the nmh etc directory.
92 */
93
94 struct bind {
95 char *keyword;
96 char **value;
97 };
98
99 static struct bind binds[] = {
100 { "localname", &localname },
101 { "localdomain", &localdomain },
102 { "systemname", &systemname },
103 { "mmdfldir", &mmdfldir },
104 { "mmdflfil", &mmdflfil },
105 { "uucpldir", &uucpldir },
106 { "uucplfil", &uucplfil },
107 { "mmdelim1", &mmdlm1 },
108 { "mmdelim2", &mmdlm2 },
109 { "mts", &sm_method },
110 { "sendmail", &sendmail },
111 { "clientname", &clientname },
112 { "servers", &servers },
113 { "pophost", &pophost },
114
115 { "maildelivery", &maildelivery },
116 { "everyone", &everyone },
117 { "noshell", &NoShell },
118 { NULL, NULL }
119 };
120
121
122 /*
123 * Read the configuration file for the nmh interface
124 * to the mail transport system (MTS).
125 */
126
127 void
128 mts_init (char *name)
129 {
130 NMH_UNUSED (name);
131
132 const char *cp;
133 FILE *fp;
134 static int inited = 0;
135
136 if (inited++ || (fp = fopen (get_mtsconf_pathname(), "r")) == NULL)
137 return;
138 mts_read_conf_file(fp);
139 fclose (fp);
140
141 cp = get_mtsuserconf_pathname();
142 if (cp != NULL &&
143 ((fp = fopen (get_mtsuserconf_pathname(), "r")) != NULL)) {
144 mts_read_conf_file(fp);
145 fclose (fp);
146 }
147
148 Everyone = atoi (everyone);
149
150 if (strcmp(sm_method, "smtp") == 0)
151 sm_mts = MTS_SMTP;
152 else if (strcmp(sm_method, "sendmail") == 0)
153 sm_mts = MTS_SENDMAIL;
154 else {
155 advise(NULL, "unsupported \"mts\" value in mts.conf: %s", sm_method);
156 sm_mts = MTS_SMTP;
157 }
158 }
159
160
161 #define QUOTE '\\'
162
163 /*
164 * Convert escaped values, malloc some new space,
165 * and copy string to malloc'ed memory.
166 */
167
168 static char *
169 tailor_value (unsigned char *s)
170 {
171 int i, r;
172 char *bp;
173 char buffer[BUFSIZ];
174 size_t len;
175
176 for (bp = buffer; *s; bp++, s++) {
177 if (*s != QUOTE) {
178 *bp = *s;
179 } else {
180 switch (*++s) {
181 case 'b': *bp = '\b'; break;
182 case 'f': *bp = '\f'; break;
183 case 'n': *bp = '\n'; break;
184 case 't': *bp = '\t'; break;
185
186 case 0: s--;
187 case QUOTE:
188 *bp = QUOTE;
189 break;
190
191 default:
192 if (!isdigit (*s)) {
193 *bp++ = QUOTE;
194 *bp = *s;
195 }
196 r = *s != '0' ? 10 : 8;
197 for (i = 0; isdigit (*s); s++)
198 i = i * r + *s - '0';
199 s--;
200 *bp = toascii (i);
201 break;
202 }
203 }
204 }
205 *bp = 0;
206
207 len = strlen (buffer) + 1;
208 bp = mh_xmalloc (len);
209 memcpy (bp, buffer, len);
210
211 return bp;
212 }
213
214 /*
215 * Get the fully qualified name of the local host.
216 *
217 * If flag is 0, then use anything out of mts.conf (like localname).
218 * If flag is 1, then only use the "proper" local hostname.
219 */
220
221 char *
222 LocalName (int flag)
223 {
224 static char buffer0[BUFSIZ] = "";
225 static char buffer1[BUFSIZ] = "";
226 static char *buffer[] = { buffer0, buffer1 };
227 char *buf;
228 struct addrinfo hints, *res;
229
230 if (flag < 0 || flag > 1)
231 return NULL;
232
233 buf = buffer[flag];
234
235 /* check if we have cached the local name */
236 if (buf[0])
237 return buf;
238
239 mts_init ("mts");
240
241 /* check if the mts.conf file specifies a "localname" */
242 if (*localname && flag == 0) {
243 strncpy (buf, localname, sizeof(buffer0));
244 } else {
245 memset(buf, 0, sizeof(buffer0));
246 /* first get our local name */
247 gethostname (buf, sizeof(buffer0) - 1);
248 /* now fully qualify our name */
249
250 memset(&hints, 0, sizeof(hints));
251 hints.ai_flags = AI_CANONNAME;
252 hints.ai_family = PF_UNSPEC;
253 if (getaddrinfo(buf, NULL, &hints, &res) == 0) {
254 strncpy(buf, res->ai_canonname, sizeof(buffer0) - 1);
255 freeaddrinfo(res);
256 }
257 }
258
259 /*
260 * If the mts.conf file specifies a "localdomain",
261 * we append that now. This should rarely be needed.
262 */
263 if (*localdomain) {
264 strcat (buf, ".");
265 strcat (buf, localdomain);
266 }
267
268 return buf;
269 }
270
271
272 /*
273 * This is only for UUCP mail. It gets the hostname
274 * as part of the UUCP "domain".
275 */
276
277 char *
278 SystemName (void)
279 {
280 static char buffer[BUFSIZ] = "";
281
282 /* check if we have cached the system name */
283 if (buffer[0])
284 return buffer;
285
286 mts_init ("mts");
287
288 /* check if mts.conf file specifies a "systemname" */
289 if (*systemname) {
290 strncpy (buffer, systemname, sizeof(buffer));
291 return buffer;
292 }
293
294 gethostname (buffer, sizeof(buffer));
295
296 return buffer;
297 }
298
299
300 /*
301 * Get the username of current user
302 */
303
304 char *
305 getusername (void)
306 {
307 if (username[0] == '\0')
308 getuserinfo();
309
310 return username;
311 }
312
313
314 /*
315 * Get full name of current user (typically from GECOS
316 * field of password file).
317 */
318
319 char *
320 getfullname (void)
321 {
322 if (username[0] == '\0')
323 getuserinfo();
324
325 return fullname;
326 }
327
328
329 /*
330 * Get the full local mailbox name. This is in the form:
331 *
332 * User Name <user@name.com>
333 */
334
335 char *
336 getlocalmbox (void)
337 {
338 if (username[0] == '\0')
339 getuserinfo();
340
341 if (localmbox[0] == '\0') {
342 char *cp;
343
344 if ((cp = context_find("Local-Mailbox")) != NULL) {
345 strncpy(localmbox, cp, sizeof(localmbox));
346 } else {
347 snprintf(localmbox, sizeof(localmbox), "%s <%s@%s>", fullname,
348 username, LocalName(0));
349 }
350
351 localmbox[sizeof(localmbox) - 1] = '\0';
352 }
353
354 return localmbox;
355 }
356
357 /*
358 * Find the user's username and full name, and cache them.
359 */
360
361 static void
362 getuserinfo (void)
363 {
364 register unsigned char *cp;
365 register char *np;
366 register struct passwd *pw;
367
368 if ((pw = getpwuid (getuid ())) == NULL
369 || pw->pw_name == NULL
370 || *pw->pw_name == '\0') {
371 strncpy (username, "unknown", sizeof(username));
372 snprintf (fullname, sizeof(fullname), "The Unknown User-ID (%d)",
373 (int) getuid ());
374 return;
375 }
376
377 np = pw->pw_gecos;
378
379 /* Get the user's real name from the GECOS field. Stop once we hit a ',',
380 which some OSes use to separate other 'finger' information in the GECOS
381 field, like phone number. */
382 for (cp = fullname; *np != '\0' && *np != ','; *cp++ = *np++)
383 continue;
384 *cp = '\0';
385
386 strncpy (username, pw->pw_name, sizeof(username));
387
388 /* The $SIGNATURE environment variable overrides the GECOS field's idea of
389 your real name. If SIGNATURE isn't set, use the Signature profile
390 setting if it exists. */
391 if ((cp = getenv ("SIGNATURE")) && *cp)
392 strncpy (fullname, cp, sizeof(fullname));
393 else if ((cp = context_find("Signature")))
394 strncpy (fullname, cp, sizeof(fullname));
395
396 fullname[sizeof(fullname) - 1] = '\0';
397
398 escape_display_name(fullname, sizeof(fullname));
399
400 localmbox[0] = '\0';
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))
446 break;
447 if (b->keyword && (cp = tailor_value (bp)))
448 *b->value = cp;
449 }
450 }