xref: /freebsd/crypto/heimdal/appl/login/utmp_login.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "login_locl.h"
35b528cefcSMark Murray 
36*ae771770SStanislav Sedov RCSID("$Id$");
37adb0ddaeSAssar Westerlund 
38adb0ddaeSAssar Westerlund /* try to put something useful from hostname into dst, dst_sz:
39adb0ddaeSAssar Westerlund  * full name, first component or address */
40adb0ddaeSAssar Westerlund 
41adb0ddaeSAssar Westerlund void
shrink_hostname(const char * hostname,char * dst,size_t dst_sz)42adb0ddaeSAssar Westerlund shrink_hostname (const char *hostname,
43adb0ddaeSAssar Westerlund 		 char *dst, size_t dst_sz)
44adb0ddaeSAssar Westerlund {
45adb0ddaeSAssar Westerlund     char local_hostname[MaxHostNameLen];
46adb0ddaeSAssar Westerlund     char *ld, *hd;
47adb0ddaeSAssar Westerlund     int ret;
48adb0ddaeSAssar Westerlund     struct addrinfo *ai;
49adb0ddaeSAssar Westerlund 
50adb0ddaeSAssar Westerlund     if (strlen(hostname) < dst_sz) {
51adb0ddaeSAssar Westerlund 	strlcpy (dst, hostname, dst_sz);
52adb0ddaeSAssar Westerlund 	return;
53adb0ddaeSAssar Westerlund     }
54adb0ddaeSAssar Westerlund     gethostname (local_hostname, sizeof(local_hostname));
55adb0ddaeSAssar Westerlund     hd = strchr (hostname, '.');
56adb0ddaeSAssar Westerlund     ld = strchr (local_hostname, '.');
57adb0ddaeSAssar Westerlund     if (hd != NULL && ld != NULL && strcmp(hd, ld) == 0
58adb0ddaeSAssar Westerlund 	&& hd - hostname < dst_sz) {
59adb0ddaeSAssar Westerlund 	strlcpy (dst, hostname, dst_sz);
60adb0ddaeSAssar Westerlund 	dst[hd - hostname] = '\0';
61adb0ddaeSAssar Westerlund 	return;
62adb0ddaeSAssar Westerlund     }
63adb0ddaeSAssar Westerlund 
64adb0ddaeSAssar Westerlund     ret = getaddrinfo (hostname, NULL, NULL, &ai);
65adb0ddaeSAssar Westerlund     if (ret) {
66adb0ddaeSAssar Westerlund 	strncpy (dst, hostname, dst_sz);
67adb0ddaeSAssar Westerlund 	return;
68adb0ddaeSAssar Westerlund     }
69adb0ddaeSAssar Westerlund     ret = getnameinfo (ai->ai_addr, ai->ai_addrlen,
70adb0ddaeSAssar Westerlund 		       dst, dst_sz,
71adb0ddaeSAssar Westerlund 		       NULL, 0,
72adb0ddaeSAssar Westerlund 		       NI_NUMERICHOST);
73adb0ddaeSAssar Westerlund     freeaddrinfo (ai);
74adb0ddaeSAssar Westerlund     if (ret) {
75adb0ddaeSAssar Westerlund 	strncpy (dst, hostname, dst_sz);
76adb0ddaeSAssar Westerlund 	return;
77adb0ddaeSAssar Westerlund     }
78adb0ddaeSAssar Westerlund }
79b528cefcSMark Murray 
80*ae771770SStanislav Sedov /* update utmp and wtmp - the BSD way */
81*ae771770SStanislav Sedov 
82*ae771770SStanislav Sedov #if !defined(HAVE_UTMPX_H) || (defined(WTMP_FILE) && !defined(WTMPX_FILE))
83*ae771770SStanislav Sedov 
84b528cefcSMark Murray void
prepare_utmp(struct utmp * utmp,char * tty,const char * username,const char * hostname)85b528cefcSMark Murray prepare_utmp (struct utmp *utmp, char *tty,
86b528cefcSMark Murray 	      const char *username, const char *hostname)
87b528cefcSMark Murray {
88b528cefcSMark Murray     char *ttyx = clean_ttyname (tty);
89b528cefcSMark Murray 
90b528cefcSMark Murray     memset(utmp, 0, sizeof(*utmp));
91b528cefcSMark Murray     utmp->ut_time = time(NULL);
92b528cefcSMark Murray     strncpy(utmp->ut_line, ttyx, sizeof(utmp->ut_line));
93b528cefcSMark Murray     strncpy(utmp->ut_name, username, sizeof(utmp->ut_name));
94b528cefcSMark Murray 
95b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_USER
96b528cefcSMark Murray     strncpy(utmp->ut_user, username, sizeof(utmp->ut_user));
97b528cefcSMark Murray # endif
98b528cefcSMark Murray 
99b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_ADDR
100b528cefcSMark Murray     if (hostname[0]) {
101b528cefcSMark Murray         struct hostent *he;
102b528cefcSMark Murray 	if ((he = gethostbyname(hostname)))
103b528cefcSMark Murray 	    memcpy(&utmp->ut_addr, he->h_addr_list[0],
104b528cefcSMark Murray 		   sizeof(utmp->ut_addr));
105b528cefcSMark Murray     }
106b528cefcSMark Murray # endif
107b528cefcSMark Murray 
108b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_HOST
109adb0ddaeSAssar Westerlund     shrink_hostname (hostname, utmp->ut_host, sizeof(utmp->ut_host));
110b528cefcSMark Murray # endif
111b528cefcSMark Murray 
112b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_TYPE
113b528cefcSMark Murray     utmp->ut_type = USER_PROCESS;
114b528cefcSMark Murray # endif
115b528cefcSMark Murray 
116b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_PID
117b528cefcSMark Murray     utmp->ut_pid = getpid();
118b528cefcSMark Murray # endif
119b528cefcSMark Murray 
120b528cefcSMark Murray # ifdef HAVE_STRUCT_UTMP_UT_ID
121b528cefcSMark Murray     strncpy(utmp->ut_id, make_id(ttyx), sizeof(utmp->ut_id));
122b528cefcSMark Murray # endif
123b528cefcSMark Murray }
124*ae771770SStanislav Sedov #endif
125b528cefcSMark Murray 
126b528cefcSMark Murray #ifdef HAVE_UTMPX_H
utmp_login(char * tty,const char * username,const char * hostname)127b528cefcSMark Murray void utmp_login(char *tty, const char *username, const char *hostname)
128b528cefcSMark Murray {
129b528cefcSMark Murray     return;
130b528cefcSMark Murray }
131b528cefcSMark Murray #else
132b528cefcSMark Murray 
utmp_login(char * tty,const char * username,const char * hostname)133b528cefcSMark Murray void utmp_login(char *tty, const char *username, const char *hostname)
134b528cefcSMark Murray {
135b528cefcSMark Murray     struct utmp utmp;
136b528cefcSMark Murray     int fd;
137b528cefcSMark Murray 
138b528cefcSMark Murray     prepare_utmp (&utmp, tty, username, hostname);
139b528cefcSMark Murray 
140b528cefcSMark Murray #ifdef HAVE_SETUTENT
141b528cefcSMark Murray     utmpname(_PATH_UTMP);
142b528cefcSMark Murray     setutent();
143b528cefcSMark Murray     pututline(&utmp);
144b528cefcSMark Murray     endutent();
145b528cefcSMark Murray #else
146b528cefcSMark Murray 
147b528cefcSMark Murray #ifdef HAVE_TTYSLOT
148b528cefcSMark Murray     {
149b528cefcSMark Murray       int ttyno;
150b528cefcSMark Murray       ttyno = ttyslot();
151b528cefcSMark Murray       if (ttyno > 0 && (fd = open(_PATH_UTMP, O_WRONLY, 0)) >= 0) {
152b528cefcSMark Murray 	lseek(fd, (long)(ttyno * sizeof(struct utmp)), SEEK_SET);
153b528cefcSMark Murray 	write(fd, &utmp, sizeof(struct utmp));
154b528cefcSMark Murray 	close(fd);
155b528cefcSMark Murray       }
156b528cefcSMark Murray     }
157b528cefcSMark Murray #endif /* HAVE_TTYSLOT */
158b528cefcSMark Murray #endif /* HAVE_SETUTENT */
159b528cefcSMark Murray 
160b528cefcSMark Murray     if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
161b528cefcSMark Murray 	write(fd, &utmp, sizeof(struct utmp));
162b528cefcSMark Murray 	close(fd);
163b528cefcSMark Murray     }
164b528cefcSMark Murray }
165*ae771770SStanislav Sedov 
166b528cefcSMark Murray #endif /* !HAVE_UTMPX_H */
167