1 /* Copyright (C) 2008-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <sys/types.h>
19 #include <utmp.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 
23 #include "utmp32.h"
24 #include "utmp-convert.h"
25 
26 #include "utmpx32.h"
27 #include "utmpx-convert.h"
28 
29 /* Allocate a static buffer to be returned to the caller.  As well as
30    with the existing version of these functions the caller has to be
31    aware that the contents of this buffer will change with subsequent
32    calls.  */
33 #define ALLOCATE_UTMPX32_OUT(OUT)			\
34   static struct utmpx32 *OUT = NULL;			\
35 							\
36   if (OUT == NULL)					\
37     {							\
38       OUT = malloc (sizeof (struct utmpx32));		\
39       if (OUT == NULL)					\
40 	return NULL;					\
41     }
42 
43 /* Perform a lookup for a utmpx entry matching FIELD using function
44    FUNC.  FIELD is converted to a 64 bit utmpx and the result is
45    converted back to 32 bit utmpx.  */
46 #define ACCESS_UTMPX_ENTRY(FUNC, FIELD)			\
47   struct utmpx in64;					\
48   struct utmpx *out64;					\
49   ALLOCATE_UTMPX32_OUT (out32);				\
50 							\
51   utmpx_convert32to64 (FIELD, &in64);			\
52   out64 = FUNC (&in64);					\
53 							\
54   if (out64 == NULL)					\
55     return NULL;					\
56 							\
57   utmpx_convert64to32 (out64, out32);			\
58 							\
59   return out32;
60 
61 
62 /* Get the next entry from the user accounting database.  */
63 struct utmpx32 *
getutxent32(void)64 getutxent32 (void)
65 {
66   struct utmpx *out64;
67   ALLOCATE_UTMPX32_OUT (out32);
68 
69   out64 = __getutxent ();
70   if (!out64)
71     return NULL;
72 
73   utmpx_convert64to32 (out64, out32);
74   return out32;
75 
76 }
77 symbol_version (getutxent32, getutxent, GLIBC_2.1);
78 
79 /* Get the user accounting database entry corresponding to ID.  */
80 struct utmpx32 *
getutxid32(const struct utmpx32 * id)81 getutxid32 (const struct utmpx32 *id)
82 {
83   ACCESS_UTMPX_ENTRY (__getutxid, id);
84 }
85 symbol_version (getutxid32, getutxid, GLIBC_2.1);
86 
87 /* Get the user accounting database entry corresponding to LINE.  */
88 struct utmpx32 *
getutxline32(const struct utmpx32 * line)89 getutxline32 (const struct utmpx32 *line)
90 {
91   ACCESS_UTMPX_ENTRY (__getutxline, line);
92 }
93 symbol_version (getutxline32, getutxline, GLIBC_2.1);
94 
95 /* Write the entry UTMPX into the user accounting database.  */
96 struct utmpx32 *
pututxline32(const struct utmpx32 * utmpx)97 pututxline32 (const struct utmpx32 *utmpx)
98 {
99   ACCESS_UTMPX_ENTRY (__pututxline, utmpx);
100 }
101 symbol_version (pututxline32, pututxline, GLIBC_2.1);
102 
103 /* Append entry UTMP to the wtmpx-like file WTMPX_FILE.  */
104 void
updwtmpx32(const char * wtmpx_file,const struct utmpx32 * utmpx)105 updwtmpx32 (const char *wtmpx_file, const struct utmpx32 *utmpx)
106 {
107   struct utmpx in64;
108 
109   utmpx_convert32to64 (utmpx, &in64);
110   __updwtmpx (wtmpx_file, &in64);
111 }
112 symbol_version (updwtmpx32, updwtmpx, GLIBC_2.1);
113 
114 /* Copy the information in UTMPX to UTMP.  */
115 void
getutmp32(const struct utmpx32 * utmpx,struct utmp32 * utmp)116 getutmp32 (const struct utmpx32 *utmpx, struct utmp32 *utmp)
117 {
118   struct utmpx in64;
119   struct utmp out64;
120 
121   utmpx_convert32to64 (utmpx, &in64);
122   __getutmp (&in64, &out64);
123   utmp_convert64to32 (&out64, utmp);
124 }
125 symbol_version (getutmp32, getutmp, GLIBC_2.1.1);
126 
127 /* Copy the information in UTMP to UTMPX.  */
128 void
getutmpx32(const struct utmp32 * utmp,struct utmpx32 * utmpx)129 getutmpx32 (const struct utmp32 *utmp, struct utmpx32 *utmpx)
130 {
131   struct utmp in64;
132   struct utmpx out64;
133 
134   utmp_convert32to64 (utmp, &in64);
135   __getutmpx (&in64, &out64);
136   utmpx_convert64to32 (&out64, utmpx);
137 }
138 symbol_version (getutmpx32, getutmpx, GLIBC_2.1.1);
139