1 /* $NetBSD: compat_utmpx.c,v 1.4 2011/07/01 01:08:59 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: compat_utmpx.c,v 1.4 2011/07/01 01:08:59 joerg Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 #include "namespace.h"
38 #include <sys/types.h>
39 #include <sys/param.h>
40
41 #define __LIBC12_SOURCE__
42 #include <assert.h>
43 #include <sys/time.h>
44 #include <string.h>
45 #include <compat/sys/time.h>
46 #include <utmpx.h>
47 #include <compat/include/utmpx.h>
48
49 __warn_references(getutxent,
50 "warning: reference to compatibility getutxent(); include <utmpx.h> for correct reference")
51 __warn_references(getutxid,
52 "warning: reference to compatibility getutxid(); include <utmpx.h> for correct reference")
53 __warn_references(getutxline,
54 "warning: reference to compatibility getutxline(); include <utmpx.h> for correct reference")
55 __warn_references(pututxline,
56 "warning: reference to compatibility pututxline(); include <utmpx.h> for correct reference")
57 __warn_references(updwtmpx,
58 "warning: reference to compatibility updwtmpx(); include <utmpx.h> for correct reference")
59 __warn_references(getlastlogx,
60 "warning: reference to compatibility getlastlogx(); include <utmpx.h> for correct reference")
61 __warn_references(updlastlogx,
62 "warning: reference to compatibility updlastlogx(); include <utmpx.h> for correct reference")
63 __warn_references(getutmp,
64 "warning: reference to compatibility getutmp(); include <utmpx.h> for correct reference")
65 __warn_references(getutmpx,
66 "warning: reference to compatibility getutmpx(); include <utmpx.h> for correct reference")
67
68 static struct utmpx50 *
cvt(struct utmpx * ut)69 cvt(struct utmpx *ut)
70 {
71 if (ut == NULL)
72 return NULL;
73 timeval_to_timeval50(&ut->ut_tv, (void *)&ut->ut_tv);
74 return (void *)ut;
75 }
76
77 static void
lastlogx50_to_lastlogx(const struct lastlogx50 * ll50,struct lastlogx * ll)78 lastlogx50_to_lastlogx(const struct lastlogx50 *ll50, struct lastlogx *ll)
79 {
80 (void)memcpy(ll->ll_line, ll50->ll_line, sizeof(ll->ll_line));
81 (void)memcpy(ll->ll_host, ll50->ll_host, sizeof(ll->ll_host));
82 (void)memcpy(&ll->ll_ss, &ll50->ll_ss, sizeof(ll->ll_ss));
83 timeval50_to_timeval(&ll50->ll_tv, &ll->ll_tv);
84 }
85
86 static void
lastlogx_to_lastlogx50(const struct lastlogx * ll,struct lastlogx50 * ll50)87 lastlogx_to_lastlogx50(const struct lastlogx *ll, struct lastlogx50 *ll50)
88 {
89 (void)memcpy(ll50->ll_line, ll->ll_line, sizeof(ll50->ll_line));
90 (void)memcpy(ll50->ll_host, ll->ll_host, sizeof(ll50->ll_host));
91 (void)memcpy(&ll50->ll_ss, &ll->ll_ss, sizeof(ll50->ll_ss));
92 timeval_to_timeval50(&ll->ll_tv, &ll50->ll_tv);
93 }
94
95 struct utmpx50 *
getutxent(void)96 getutxent(void)
97 {
98 return cvt(__getutxent50());
99 }
100
101 struct utmpx50 *
getutxid(const struct utmpx50 * ut50)102 getutxid(const struct utmpx50 *ut50)
103 {
104 struct utmpx ut;
105 utmpx50_to_utmpx(ut50, &ut);
106 return cvt(__getutxid50(&ut));
107 }
108
109 struct utmpx50 *
getutxline(const struct utmpx50 * ut50)110 getutxline(const struct utmpx50 *ut50)
111 {
112 struct utmpx ut;
113 utmpx50_to_utmpx(ut50, &ut);
114 return cvt(__getutxline50(&ut));
115 }
116
117 struct utmpx50 *
pututxline(const struct utmpx50 * ut50)118 pututxline(const struct utmpx50 *ut50)
119 {
120 struct utmpx ut;
121 utmpx50_to_utmpx(ut50, &ut);
122 return cvt(__pututxline50(&ut));
123 }
124
125 int
updwtmpx(const char * fname,const struct utmpx50 * ut50)126 updwtmpx(const char *fname, const struct utmpx50 *ut50)
127 {
128 struct utmpx ut;
129 utmpx50_to_utmpx(ut50, &ut);
130 return __updwtmpx50(fname, &ut);
131 }
132
133 struct lastlogx50 *
__getlastlogx13(const char * fname,uid_t uid,struct lastlogx50 * ll50)134 __getlastlogx13(const char *fname, uid_t uid, struct lastlogx50 *ll50)
135 {
136 struct lastlogx ll;
137 if (__getlastlogx50(fname, uid, &ll) == NULL)
138 return NULL;
139 lastlogx_to_lastlogx50(&ll, ll50);
140 return ll50;
141 }
142
143 static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
144
145 int
lastlogxname(const char * fname)146 lastlogxname(const char *fname)
147 {
148 size_t len;
149
150 _DIAGASSERT(fname != NULL);
151
152 len = strlen(fname);
153
154 if (len >= sizeof(llfile))
155 return 0;
156
157 /* must end in x! */
158 if (fname[len - 1] != 'x')
159 return 0;
160
161 (void)strlcpy(llfile, fname, sizeof(llfile));
162 return 1;
163 }
164
165 struct lastlogx50 *
getlastlogx(uid_t uid,struct lastlogx50 * ll)166 getlastlogx(uid_t uid, struct lastlogx50 *ll)
167 {
168
169 return __getlastlogx13(llfile, uid, ll);
170 }
171
172 int
updlastlogx(const char * fname,uid_t uid,struct lastlogx50 * ll50)173 updlastlogx(const char *fname, uid_t uid, struct lastlogx50 *ll50)
174 {
175 struct lastlogx ll;
176 lastlogx50_to_lastlogx(ll50, &ll);
177 return __updlastlogx50(fname, uid, &ll);
178 }
179
180 void
getutmp(const struct utmpx50 * utx50,struct utmp * ut)181 getutmp(const struct utmpx50 *utx50, struct utmp *ut)
182 {
183 struct utmpx utx;
184 utmpx50_to_utmpx(utx50, &utx);
185 __getutmp50(&utx, ut);
186 }
187
188 void
getutmpx(const struct utmp * ut,struct utmpx50 * utx50)189 getutmpx(const struct utmp *ut, struct utmpx50 *utx50)
190 {
191 struct utmpx utx;
192 __getutmpx50(ut, &utx);
193 utmpx_to_utmpx50(&utx, utx50);
194 }
195