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 ** Get the username of current user
79 if (username[0] == '\0')
87 ** Get full name of current user (typically from GECOS
88 ** field of password file).
93 if (username[0] == '\0')
101 ** Find the user's username and full name, and cache them.
113 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
114 strncpy(username, "unknown", sizeof(username));
115 snprintf(fullname, sizeof(fullname),
116 "The Unknown User-ID (%d)", (int)getuid());
123 ** Get the user's real name from the GECOS field. Stop once
124 ** we hit a ',', which some OSes use to separate other 'finger'
125 ** information in the GECOS field, like phone number.
127 for (cp = tmp; *np != '\0' && *np != ',';) {
132 ** On BSD(-derived) systems, the system utilities that
133 ** deal with the GECOS field (finger, mail, sendmail,
134 ** etc.) translate any '&' character in it to the login name,
135 ** with the first letter capitalized. So, for instance,
136 ** fingering a user "bob" with the GECOS field "& Jones"
137 ** would reveal him to be "In real life: Bob Jones".
138 ** Surprisingly, though, the OS doesn't do the translation
139 ** for you, so we have to do it manually here.
141 if (*np == '&') { /* blech! */
142 strcpy(cp, pw->pw_name);
153 strncpy(username, pw->pw_name, sizeof(username));
156 ** The $SIGNATURE environment variable overrides the GECOS field's
157 ** idea of your real name.
159 if ((cp = getenv("SIGNATURE")) && *cp)
160 strncpy(tmp, cp, sizeof(tmp));
162 /* quote the fullname as needed */
164 for (tp=tmp; *tp; tp++) {
166 case '(': case ')': case '<': case '>': case '[': case ']':
167 case ':': case ';': case '@': case '\\': case ',': case '.':
168 case '"': /* cf. RFC 5322 */
169 break; /* ... the switch */
171 continue; /* ... the loop */
173 /* we've found a special char */
181 for (tp=tmp; *tp; *cp++=*tp++) {
183 *cp++ = '\\'; /* prepend backslash */