]>
diplodocus.org Git - nmh/blob - sbr/mts.c
3 * mts.c -- definitions for the mail transport system
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.
10 #include <h/mh.h> /* for snprintf() */
13 #define nmhetcdir(file) NMHETCDIR#file
17 #include <sys/socket.h>
23 static char *tailor_value (char *);
24 static void getuserinfo (void);
25 static const char *get_mtsconf_pathname(void);
26 static const char *get_mtsuserconf_pathname(void);
27 static void mts_read_conf_file (FILE *fp
);
30 * *mmdfldir and *uucpldir are the maildrop directories. If maildrops
31 * are kept in the user's home directory, then these should be empty
32 * strings. In this case, the appropriate ...lfil array should contain
33 * the name of the file in the user's home directory. Usually, this is
34 * something like ".mail".
38 * nmh mail transport interface customization file
40 static char *mtsconf
= nmhetcdir(/mts
.conf
);
42 static char *localname
= "";
43 static char *localdomain
= "";
44 static char *systemname
= "";
46 char *mmdfldir
= MAILSPOOL
;
48 char *uucpldir
= "/usr/spool/mail";
51 char *mmdlm1
= "\001\001\001\001\n";
52 char *mmdlm2
= "\001\001\001\001\n";
54 char *spoollocking
= DEFAULT_LOCKING
;
56 /* Cache the username, fullname, and mailbox of the user */
57 static char username
[BUFSIZ
];
58 static char fullname
[BUFSIZ
];
59 static char localmbox
[BUFSIZ
];
62 * MTS specific variables
64 static char *mts_method
= "smtp";
65 int sm_mts
= MTS_SMTP
;
66 char *sendmail
= SENDMAILPATH
;
71 char *clientname
= NULL
;
72 char *servers
= "localhost";
76 * Global MailDelivery file
78 char *maildelivery
= nmhetcdir(/maildelivery
);
82 * Customize the MTS settings for nmh by adjusting
83 * the file mts.conf in the nmh etc directory.
91 static struct bind binds
[] = {
92 { "localname", &localname
},
93 { "localdomain", &localdomain
},
94 { "systemname", &systemname
},
95 { "mmdfldir", &mmdfldir
},
96 { "mmdflfil", &mmdflfil
},
97 { "spoollocking", &spoollocking
},
98 { "uucpldir", &uucpldir
},
99 { "uucplfil", &uucplfil
},
100 { "mmdelim1", &mmdlm1
},
101 { "mmdelim2", &mmdlm2
},
102 { "mts", &mts_method
},
103 { "sendmail", &sendmail
},
104 { "clientname", &clientname
},
105 { "servers", &servers
},
106 { "pophost", &pophost
},
108 { "maildelivery", &maildelivery
},
113 /* Convert name of mts method to integer value and store it. */
115 save_mts_method (const char *value
) {
116 if (! strcasecmp (value
, "smtp")) {
119 } else if (! strcasecmp (value
, "sendmail/smtp") ||
120 ! strcasecmp (value
, "sendmail")) {
121 mts_method
= "sendmail/smtp";
122 sm_mts
= MTS_SENDMAIL_SMTP
;
123 } else if (! strcasecmp (value
, "sendmail/pipe")) {
124 mts_method
= "sendmail/pipe";
125 sm_mts
= MTS_SENDMAIL_PIPE
;
127 adios (NULL
, "unsupported mts selection \"%s\"", value
);
133 * Read the configuration file for the nmh interface
134 * to the mail transport system (MTS).
142 static int inited
= 0;
144 if (inited
++ || (fp
= fopen (get_mtsconf_pathname(), "r")) == NULL
)
146 mts_read_conf_file(fp
);
149 cp
= get_mtsuserconf_pathname();
151 ((fp
= fopen (get_mtsuserconf_pathname(), "r")) != NULL
)) {
152 mts_read_conf_file(fp
);
156 save_mts_method (mts_method
);
163 * Convert escaped values, malloc some new space,
164 * and copy string to malloc'ed memory.
168 tailor_value (char *s
)
174 for (bp
= buffer
; *s
; bp
++, s
++) {
179 case 'b': *bp
= '\b'; break;
180 case 'f': *bp
= '\f'; break;
181 case 'n': *bp
= '\n'; break;
182 case 't': *bp
= '\t'; break;
190 if (!isdigit ((unsigned char) *s
)) {
194 r
= ((unsigned char) *s
) != '0' ? 10 : 8;
195 for (i
= 0; isdigit ((unsigned char) *s
); s
++)
196 i
= i
* r
+ ((unsigned char) *s
) - '0';
205 return mh_xstrdup(buffer
);
209 * Get the fully qualified name of the local host.
211 * If flag is 0, then use anything out of mts.conf (like localname).
212 * If flag is 1, then only use the "proper" local hostname.
218 static char buffer0
[BUFSIZ
] = "";
219 static char buffer1
[BUFSIZ
] = "";
220 static char *buffer
[] = { buffer0
, buffer1
};
222 struct addrinfo hints
, *res
;
224 if (flag
< 0 || flag
> 1)
229 /* check if we have cached the local name */
235 /* check if the mts.conf file specifies a "localname" */
236 if (*localname
&& flag
== 0) {
237 strncpy (buf
, localname
, sizeof(buffer0
));
239 memset(buf
, 0, sizeof(buffer0
));
240 /* first get our local name */
241 gethostname (buf
, sizeof(buffer0
) - 1);
242 /* now fully qualify our name */
244 memset(&hints
, 0, sizeof(hints
));
245 hints
.ai_flags
= AI_CANONNAME
;
246 hints
.ai_family
= PF_UNSPEC
;
247 if (getaddrinfo(buf
, NULL
, &hints
, &res
) == 0) {
248 strncpy(buf
, res
->ai_canonname
, sizeof(buffer0
) - 1);
254 * If the mts.conf file specifies a "localdomain",
255 * we append that now. This should rarely be needed.
259 strcat (buf
, localdomain
);
267 * This is only for UUCP mail. It gets the hostname
268 * as part of the UUCP "domain".
274 static char buffer
[BUFSIZ
] = "";
276 /* check if we have cached the system name */
282 /* check if mts.conf file specifies a "systemname" */
284 strncpy (buffer
, systemname
, sizeof(buffer
));
288 gethostname (buffer
, sizeof(buffer
));
295 * Get the username of current user
301 if (username
[0] == '\0')
309 * Get full name of current user (typically from GECOS
310 * field of password file).
316 if (username
[0] == '\0')
324 * Get the full local mailbox name. This is in the form:
326 * User Name <user@name.com>
332 if (username
[0] == '\0')
339 * Find the user's username and full name, and cache them.
348 if ((pw
= getpwuid (getuid ())) == NULL
349 || pw
->pw_name
== NULL
350 || *pw
->pw_name
== '\0') {
351 strncpy (username
, "unknown", sizeof(username
));
352 snprintf (fullname
, sizeof(fullname
), "The Unknown User-ID (%d)",
359 /* If there's a Local-Mailbox profile component, try to extract
360 the username from it. But don't try very hard, this assumes
361 the very simple User Name <user@name.com> form.
362 Note that post(8) and whom(1) use context_foil (), so they
363 won't see the profile component. */
364 if ((np
= context_find("Local-Mailbox")) != NULL
) {
365 char *left_angle_bracket
= strchr (np
, '<');
366 char *at_sign
= strchr (np
, '@');
367 char *right_angle_bracket
= strchr (np
, '>');
369 strncpy(localmbox
, np
, sizeof(localmbox
));
371 if (left_angle_bracket
&& at_sign
&& right_angle_bracket
) {
372 if (at_sign
> left_angle_bracket
&&
373 at_sign
- left_angle_bracket
< BUFSIZ
) {
374 strncpy(username
, left_angle_bracket
+ 1,
375 at_sign
- left_angle_bracket
- 1);
380 if (username
[0] == '\0') {
381 strncpy (username
, pw
->pw_name
, sizeof(username
));
384 username
[sizeof(username
) - 1] = '\0';
386 escape_local_part(username
, sizeof(username
));
392 /* Get the user's real name from the GECOS field. Stop once we hit a ',',
393 which some OSes use to separate other 'finger' information in the GECOS
394 field, like phone number. */
395 for (cp
= fullname
; *np
!= '\0' && *np
!= ','; *cp
++ = *np
++)
399 /* The $SIGNATURE environment variable overrides the GECOS field's idea of
400 your real name. If SIGNATURE isn't set, use the Signature profile
401 setting if it exists.
402 Note that post(8) and whom(1) use context_foil (), so they
403 won't see the profile component. */
404 if ((cp
= getenv ("SIGNATURE")) && *cp
)
405 strncpy (fullname
, cp
, sizeof(fullname
));
406 else if ((cp
= context_find("Signature")))
407 strncpy (fullname
, cp
, sizeof(fullname
));
409 fullname
[sizeof(fullname
) - 1] = '\0';
411 escape_display_name(fullname
, sizeof(fullname
));
414 /* localmbox, if not using Local-Mailbox */
415 if (localmbox
[0] == '\0') {
416 snprintf(localmbox
, sizeof(localmbox
), "%s <%s@%s>", fullname
,
417 username
, LocalName(0));
420 localmbox
[sizeof(localmbox
) - 1] = '\0';
424 get_mtsconf_pathname (void)
426 const char *cp
= getenv ( "MHMTSCONF" );
427 if (cp
!= NULL
&& *cp
!= '\0') {
434 get_mtsuserconf_pathname (void)
436 const char *cp
= getenv ( "MHMTSUSERCONF" );
437 if (cp
!= NULL
&& *cp
!= '\0') {
444 mts_read_conf_file (FILE *fp
)
446 char *bp
, *cp
, buffer
[BUFSIZ
];
449 while (fgets (buffer
, sizeof(buffer
), fp
)) {
450 if (!(cp
= strchr(buffer
, '\n')))
453 if (*buffer
== '#' || *buffer
== '\0')
455 if (!(bp
= strchr(buffer
, ':')))
458 while (isspace ((unsigned char) *bp
))
461 for (b
= binds
; b
->keyword
; b
++)
462 if (!strcmp (buffer
, b
->keyword
))
464 if (b
->keyword
&& (cp
= tailor_value (bp
)))