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() */
18 #ifdef HAVE_SYS_UTSNAME_H
19 # include <sys/utsname.h>
28 static void getuserinfo(void);
30 /* Cache the username and fullname of the user */
31 static char username[BUFSIZ];
32 static char fullname[BUFSIZ];
36 ** Get the fully qualified name of the local host.
41 static char buffer[BUFSIZ] = "";
42 struct addrinfo hints, *res;
47 /* check if we have cached the local name */
51 memset(buffer, 0, sizeof(buffer));
53 /* first get our local name */
55 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
57 /* first get our local name */
58 gethostname(buffer, sizeof(buffer) - 1);
60 /* now fully qualify our name */
62 memset(&hints, 0, sizeof(hints));
63 hints.ai_flags = AI_CANONNAME;
64 hints.ai_family = PF_UNSPEC;
65 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
66 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
75 ** This is only for UUCP mail. It gets the hostname
76 ** as part of the UUCP "domain".
81 static char buffer[BUFSIZ] = "";
87 /* check if we have cached the system name */
93 strncpy(buffer, name.nodename, sizeof(buffer));
95 gethostname(buffer, sizeof(buffer));
103 ** Get the username of current user
108 if (username[0] == '\0')
116 ** Get full name of current user (typically from GECOS
117 ** field of password file).
122 if (username[0] == '\0')
130 ** Find the user's username and full name, and cache them.
142 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
143 strncpy(username, "unknown", sizeof(username));
144 snprintf(fullname, sizeof(fullname),
145 "The Unknown User-ID (%d)", (int)getuid());
152 ** Get the user's real name from the GECOS field. Stop once
153 ** we hit a ',', which some OSes use to separate other 'finger'
154 ** information in the GECOS field, like phone number.
156 for (cp = tmp; *np != '\0' && *np != ',';) {
161 ** On BSD(-derived) systems, the system utilities that
162 ** deal with the GECOS field (finger, mail, sendmail,
163 ** etc.) translate any '&' character in it to the login name,
164 ** with the first letter capitalized. So, for instance,
165 ** fingering a user "bob" with the GECOS field "& Jones"
166 ** would reveal him to be "In real life: Bob Jones".
167 ** Surprisingly, though, the OS doesn't do the translation
168 ** for you, so we have to do it manually here.
170 if (*np == '&') { /* blech! */
171 strcpy(cp, pw->pw_name);
182 strncpy(username, pw->pw_name, sizeof(username));
185 ** The $SIGNATURE environment variable overrides the GECOS field's
186 ** idea of your real name.
188 if ((cp = getenv("SIGNATURE")) && *cp)
189 strncpy(tmp, cp, sizeof(tmp));
191 /* quote the fullname as needed */
193 for (tp=tmp; *tp; tp++) {
195 case '(': case ')': case '<': case '>': case '[': case ']':
196 case ':': case ';': case '@': case '\\': case ',': case '.':
197 case '"': /* cf. RFC 5322 */
198 break; /* ... the switch */
200 continue; /* ... the loop */
202 /* we've found a special char */
210 for (tp=tmp; *tp; *cp++=*tp++) {
212 *cp++ = '\\'; /* prepend backslash */