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() */
14 #include <sys/socket.h>
21 static void getuserinfo(void);
23 /* Cache the username and fullname of the user */
24 static char username[BUFSIZ];
25 static char fullname[BUFSIZ];
29 ** Get the fully qualified name of the local host.
34 static char buffer[BUFSIZ] = "";
35 struct addrinfo hints, *res;
37 /* check if we have cached the local name */
41 memset(buffer, 0, sizeof(buffer));
42 /* first get our local name */
43 gethostname(buffer, sizeof(buffer) - 1);
45 /* now fully qualify our name */
46 memset(&hints, 0, sizeof(hints));
47 hints.ai_flags = AI_CANONNAME;
48 hints.ai_family = PF_UNSPEC;
49 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
50 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
59 ** Get the username of current user
64 if (username[0] == '\0')
72 ** Get full name of current user (typically from GECOS
73 ** field of password file).
78 if (username[0] == '\0')
86 ** Find the user's username and full name, and cache them.
98 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
99 strncpy(username, "unknown", sizeof(username));
100 snprintf(fullname, sizeof(fullname),
101 "The Unknown User-ID (%d)", (int)getuid());
108 ** Get the user's real name from the GECOS field. Stop once
109 ** we hit a ',', which some OSes use to separate other 'finger'
110 ** information in the GECOS field, like phone number.
112 for (cp = tmp; *np != '\0' && *np != ',';) {
116 strncpy(username, pw->pw_name, sizeof(username));
119 ** The $SIGNATURE environment variable overrides the GECOS field's
120 ** idea of your real name.
122 if ((cp = getenv("SIGNATURE")) && *cp)
123 strncpy(tmp, cp, sizeof(tmp));
125 /* quote the fullname as needed */
127 for (tp=tmp; *tp; tp++) {
129 case '(': case ')': case '<': case '>': case '[': case ']':
130 case ':': case ';': case '@': case '\\': case ',': case '.':
131 case '"': /* cf. RFC 5322 */
132 break; /* ... the switch */
134 continue; /* ... the loop */
136 /* we've found a special char */
144 for (tp=tmp; *tp; *cp++=*tp++) {
146 *cp++ = '\\'; /* prepend backslash */