2 ** mts.c -- definitions for the mail transport system
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.
9 #include <h/mh.h> /* for snprintf() */
17 #ifdef HAVE_SYS_UTSNAME_H
18 # include <sys/utsname.h>
27 static void getuserinfo(void);
29 /* Cache the username and fullname of the user */
30 static char username[BUFSIZ];
31 static char fullname[BUFSIZ];
35 ** Get the fully qualified name of the local host.
40 static char buffer[BUFSIZ] = "";
41 struct addrinfo hints, *res;
46 /* check if we have cached the local name */
50 memset(buffer, 0, sizeof(buffer));
52 /* first get our local name */
54 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
56 /* first get our local name */
57 gethostname(buffer, sizeof(buffer) - 1);
59 /* now fully qualify our name */
61 memset(&hints, 0, sizeof(hints));
62 hints.ai_flags = AI_CANONNAME;
63 hints.ai_family = PF_UNSPEC;
64 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
65 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
74 ** This is only for UUCP mail. It gets the hostname
75 ** as part of the UUCP "domain".
80 static char buffer[BUFSIZ] = "";
86 /* check if we have cached the system name */
92 strncpy(buffer, name.nodename, sizeof(buffer));
94 gethostname(buffer, sizeof(buffer));
102 ** Get the username of current user
107 if (username[0] == '\0')
115 ** Get full name of current user (typically from GECOS
116 ** field of password file).
121 if (username[0] == '\0')
129 ** Find the user's username and full name, and cache them.
141 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
142 strncpy(username, "unknown", sizeof(username));
143 snprintf(fullname, sizeof(fullname),
144 "The Unknown User-ID (%d)", (int)getuid());
151 ** Get the user's real name from the GECOS field. Stop once
152 ** we hit a ',', which some OSes use to separate other 'finger'
153 ** information in the GECOS field, like phone number.
155 for (cp = tmp; *np != '\0' && *np != ',';) {
160 ** On BSD(-derived) systems, the system utilities that
161 ** deal with the GECOS field (finger, mail, sendmail,
162 ** etc.) translate any '&' character in it to the login name,
163 ** with the first letter capitalized. So, for instance,
164 ** fingering a user "bob" with the GECOS field "& Jones"
165 ** would reveal him to be "In real life: Bob Jones".
166 ** Surprisingly, though, the OS doesn't do the translation
167 ** for you, so we have to do it manually here.
169 if (*np == '&') { /* blech! */
170 strcpy(cp, pw->pw_name);
181 strncpy(username, pw->pw_name, sizeof(username));
184 ** The $SIGNATURE environment variable overrides the GECOS field's
185 ** idea of your real name.
187 if ((cp = getenv("SIGNATURE")) && *cp)
188 strncpy(tmp, cp, sizeof(tmp));
190 /* quote the fullname as needed */
192 for (tp=tmp; *tp; tp++) {
194 case '(': case ')': case '<': case '>': case '[': case ']':
195 case ':': case ';': case '@': case '\\': case ',': case '.':
196 case '"': /* cf. RFC 5322 */
197 break; /* ... the switch */
199 continue; /* ... the loop */
201 /* we've found a special char */
209 for (tp=tmp; *tp; *cp++=*tp++) {
211 *cp++ = '\\'; /* prepend backslash */