1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "namespace.h"
30 #include <sys/endian.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <utmpx.h>
36 #include "utxdb.h"
37 #include "un-namespace.h"
38
39 #define UTOF_STRING(ut, fu, field) do { \
40 strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \
41 MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \
42 } while (0)
43 #define UTOF_ID(ut, fu) do { \
44 memcpy((fu)->fu_id, (ut)->ut_id, \
45 MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \
46 } while (0)
47 #define UTOF_PID(ut, fu) do { \
48 (fu)->fu_pid = htobe32((ut)->ut_pid); \
49 } while (0)
50 #define UTOF_TYPE(ut, fu) do { \
51 (fu)->fu_type = (ut)->ut_type; \
52 } while (0)
53 #define UTOF_TV(fu) do { \
54 struct timeval tv; \
55 gettimeofday(&tv, NULL); \
56 (fu)->fu_tv = htobe64((uint64_t)tv.tv_sec * 1000000 + \
57 (uint64_t)tv.tv_usec); \
58 } while (0)
59
60 void
utx_to_futx(const struct utmpx * ut,struct futx * fu)61 utx_to_futx(const struct utmpx *ut, struct futx *fu)
62 {
63
64 memset(fu, 0, sizeof *fu);
65
66 switch (ut->ut_type) {
67 case BOOT_TIME:
68 case OLD_TIME:
69 case NEW_TIME:
70 /* Extension: shutdown time. */
71 case SHUTDOWN_TIME:
72 break;
73 case USER_PROCESS:
74 UTOF_ID(ut, fu);
75 UTOF_STRING(ut, fu, user);
76 UTOF_STRING(ut, fu, line);
77 /* Extension: host name. */
78 UTOF_STRING(ut, fu, host);
79 UTOF_PID(ut, fu);
80 break;
81 case INIT_PROCESS:
82 UTOF_ID(ut, fu);
83 UTOF_PID(ut, fu);
84 break;
85 case LOGIN_PROCESS:
86 UTOF_ID(ut, fu);
87 UTOF_STRING(ut, fu, user);
88 UTOF_STRING(ut, fu, line);
89 UTOF_PID(ut, fu);
90 break;
91 case DEAD_PROCESS:
92 UTOF_ID(ut, fu);
93 UTOF_PID(ut, fu);
94 break;
95 default:
96 fu->fu_type = EMPTY;
97 return;
98 }
99
100 UTOF_TYPE(ut, fu);
101 UTOF_TV(fu);
102 }
103
104 #define FTOU_STRING(fu, ut, field) do { \
105 strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \
106 MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \
107 } while (0)
108 #define FTOU_ID(fu, ut) do { \
109 memcpy((ut)->ut_id, (fu)->fu_id, \
110 MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \
111 } while (0)
112 #define FTOU_PID(fu, ut) do { \
113 (ut)->ut_pid = be32toh((fu)->fu_pid); \
114 } while (0)
115 #define FTOU_TYPE(fu, ut) do { \
116 (ut)->ut_type = (fu)->fu_type; \
117 } while (0)
118 #define FTOU_TV(fu, ut) do { \
119 uint64_t t; \
120 t = be64toh((fu)->fu_tv); \
121 (ut)->ut_tv.tv_sec = t / 1000000; \
122 (ut)->ut_tv.tv_usec = t % 1000000; \
123 } while (0)
124
125 struct utmpx *
futx_to_utx(const struct futx * fu)126 futx_to_utx(const struct futx *fu)
127 {
128 static _Thread_local struct utmpx *ut;
129
130 if (ut == NULL) {
131 ut = calloc(1, sizeof *ut);
132 if (ut == NULL)
133 return (NULL);
134 } else
135 memset(ut, 0, sizeof *ut);
136
137 switch (fu->fu_type) {
138 case BOOT_TIME:
139 case OLD_TIME:
140 case NEW_TIME:
141 /* Extension: shutdown time. */
142 case SHUTDOWN_TIME:
143 break;
144 case USER_PROCESS:
145 FTOU_ID(fu, ut);
146 FTOU_STRING(fu, ut, user);
147 FTOU_STRING(fu, ut, line);
148 /* Extension: host name. */
149 FTOU_STRING(fu, ut, host);
150 FTOU_PID(fu, ut);
151 break;
152 case INIT_PROCESS:
153 FTOU_ID(fu, ut);
154 FTOU_PID(fu, ut);
155 break;
156 case LOGIN_PROCESS:
157 FTOU_ID(fu, ut);
158 FTOU_STRING(fu, ut, user);
159 FTOU_STRING(fu, ut, line);
160 FTOU_PID(fu, ut);
161 break;
162 case DEAD_PROCESS:
163 FTOU_ID(fu, ut);
164 FTOU_PID(fu, ut);
165 break;
166 default:
167 ut->ut_type = EMPTY;
168 return (ut);
169 }
170
171 FTOU_TYPE(fu, ut);
172 FTOU_TV(fu, ut);
173 return (ut);
174 }
175