1/*	$NetBSD: compat_utmpx.c,v 1.3 2009/01/11 02:46:25 christos 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.3 2009/01/11 02:46:25 christos 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
68static struct utmpx50 *
69cvt(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
77static void
78lastlogx50_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
86static void
87lastlogx_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
95struct utmpx50 *
96getutxent(void)
97{
98	return cvt(__getutxent50());
99}
100
101struct utmpx50 *
102getutxid(const struct utmpx50 *ut50)
103{
104	struct utmpx ut;
105	utmpx50_to_utmpx(ut50, &ut);
106	return cvt(__getutxid50(&ut));
107}
108
109struct utmpx50 *
110getutxline(const struct utmpx50 *ut50)
111{
112	struct utmpx ut;
113	utmpx50_to_utmpx(ut50, &ut);
114	return cvt(__getutxline50(&ut));
115}
116
117struct utmpx50 *
118pututxline(const struct utmpx50 *ut50)
119{
120	struct utmpx ut;
121	utmpx50_to_utmpx(ut50, &ut);
122	return cvt(__pututxline50(&ut));
123}
124
125int
126updwtmpx(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
133struct lastlogx50 *
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
143static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
144
145int
146lastlogxname(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
165struct lastlogx50 *
166getlastlogx(uid_t uid, struct lastlogx50 *ll)
167{
168
169	return __getlastlogx13(llfile, uid, ll);
170}
171
172int
173updlastlogx(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
180void
181getutmp(const struct utmpx50 *utx50, struct utmp *ut)
182{
183	struct utmpx utx;
184	utmpx50_to_utmpx(utx50, &utx);
185	__getutmp50(&utx, ut);
186}
187
188void
189getutmpx(const struct utmp *ut, struct utmpx50 *utx50)
190{
191	struct utmpx utx;
192	__getutmpx50(ut, &utx);
193	utmpx_to_utmpx50(&utx, utx50);
194}
195