Merge from vendor branch DIFFUTILS:
[dragonfly.git] / lib / libutil / logwtmp.c
1 /*-
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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 $
36  */
37
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45
46 #include <libutil.h>
47 #include <netdb.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 #include <utmp.h>
52
53 /* wrapper for KAME-special getnameinfo() */
54 #ifndef NI_WITHSCOPEID
55 #define NI_WITHSCOPEID  0
56 #endif
57
58 void
59 trimdomain(char *fullhost, int hostsize)
60 {
61     static char domain[MAXHOSTNAMELEN];
62     static int first = 1;
63     static size_t dlen;
64     char *s, *end;
65     int spn, ok;
66
67     if (first) {
68         first = 0;
69         if (gethostname(domain, sizeof(domain) - 1) == 0 &&
70             (s = strchr(domain, '.')))
71             memmove(domain, s + 1, strlen(s + 1) + 1);
72         else
73             domain[0] = '\0';
74         dlen = strlen(domain);
75     }
76
77     if (domain[0] != '\0') {
78         s = fullhost;
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 */
84                     break;
85                 } else if (s[dlen + 1] == ':') {        /* $DISPLAY ? */
86                     ok = dlen + 2;
87                     spn = strspn(s + ok, "0123456789");
88                     if (spn > 0 && ok + spn - dlen <= end - s) {
89                         ok += spn;
90                         if (s[ok] == '\0') {
91                             /* host.domain:nn */
92                             memmove(s, s + dlen + 1, ok - dlen);
93                             break;
94                         } else if (s[ok] == '.') {
95                             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);
101                                 break;
102                             }
103                         }
104                     }
105                 }
106             }
107     }
108 }
109
110 #include <stdio.h>
111
112 void
113 logwtmp(line, name, host)
114         const char *line;
115         const char *name;
116         const char *host;
117 {
118         struct utmp ut;
119         struct stat buf;
120         char   fullhost[MAXHOSTNAMELEN];
121         int fd;
122         
123         strncpy(fullhost, host, sizeof(fullhost) - 1);  
124         fullhost[sizeof(fullhost) - 1] = '\0';
125         trimdomain(fullhost, UT_HOSTSIZE);
126         host = fullhost;
127
128         if (strlen(host) > UT_HOSTSIZE) {
129                 int error;
130                 struct addrinfo hints, *res;
131
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";
138                 else {
139                         error = getnameinfo(res->ai_addr, res->ai_addrlen,
140                                           fullhost, strlen(fullhost), NULL, 0,
141                                           NI_NUMERICHOST|NI_WITHSCOPEID);
142                         if (error != 0) {
143                           fprintf(stderr, "%d", error);
144                                 host = "invalid hostname";
145                         }
146                 }
147         }
148
149         if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
150                 return;
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)) !=
157                     sizeof(struct utmp))
158                         (void) ftruncate(fd, buf.st_size);
159         }
160         (void) close(fd);
161 }