2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)logwtmp.c 8.1 (Berkeley) 6/4/93
34 * $FreeBSD: src/lib/libutil/logwtmp.c,v 1.14 2000/01/15 03:26:54 shin Exp $
35 * $DragonFly: src/lib/libutil/logwtmp.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
53 /* wrapper for KAME-special getnameinfo() */
54 #ifndef NI_WITHSCOPEID
55 #define NI_WITHSCOPEID 0
59 trimdomain(char *fullhost, int hostsize)
61 static char domain[MAXHOSTNAMELEN];
69 if (gethostname(domain, sizeof(domain) - 1) == 0 &&
70 (s = strchr(domain, '.')))
71 memmove(domain, s + 1, strlen(s + 1) + 1);
74 dlen = strlen(domain);
77 if (domain[0] != '\0') {
79 end = s + hostsize + 1;
80 for (; (s = memchr(s, '.', end - s)) != NULL; s++)
81 if (!strncasecmp(s + 1, domain, dlen)) {
82 if (s[dlen + 1] == '\0') {
83 *s = '\0'; /* Found - lose the domain */
85 } else if (s[dlen + 1] == ':') { /* $DISPLAY ? */
87 spn = strspn(s + ok, "0123456789");
88 if (spn > 0 && ok + spn - dlen <= end - s) {
92 memmove(s, s + dlen + 1, ok - dlen);
94 } else if (s[ok] == '.') {
96 spn = strspn(s + ok, "0123456789");
97 if (spn > 0 && s[ok + spn] == '\0' &&
98 ok + spn - dlen <= end - s) {
99 /* host.domain:nn.nn */
100 memmove(s, s + dlen + 1, ok + spn - dlen);
113 logwtmp(line, name, host)
120 char fullhost[MAXHOSTNAMELEN];
123 strncpy(fullhost, host, sizeof(fullhost) - 1);
124 fullhost[sizeof(fullhost) - 1] = '\0';
125 trimdomain(fullhost, UT_HOSTSIZE);
128 if (strlen(host) > UT_HOSTSIZE) {
130 struct addrinfo hints, *res;
132 bzero(&hints, sizeof(struct addrinfo));
133 hints.ai_family = AF_UNSPEC;
134 hints.ai_flags = AI_CANONNAME;
135 error = getaddrinfo(host, NULL, &hints, &res);
136 if (error != 0 || res->ai_addr == NULL)
137 host = "invalid hostname";
139 error = getnameinfo(res->ai_addr, res->ai_addrlen,
140 fullhost, strlen(fullhost), NULL, 0,
141 NI_NUMERICHOST|NI_WITHSCOPEID);
143 fprintf(stderr, "%d", error);
144 host = "invalid hostname";
149 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
151 if (fstat(fd, &buf) == 0) {
152 (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
153 (void) strncpy(ut.ut_name, name, sizeof(ut.ut_name));
154 (void) strncpy(ut.ut_host, host, sizeof(ut.ut_host));
155 (void) time(&ut.ut_time);
156 if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
158 (void) ftruncate(fd, buf.st_size);