1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Main functions of the program.
5 * Copyright (C) 2007-2008 David Härdeman <david@hardeman.nu>
6 * Copyright (C) 2015-2018 Przemyslaw Pawelczyk <przemoc@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define _DEFAULT_SOURCE
31 #include <sys/types.h>
37 /* Controls the verbosity level for msg() */
38 static int verbosity
= 0;
40 /* Adjusts the verbosity level for msg() */
42 adjust_verbosity(int adj
)
48 * Prints messages to console according to the current verbosity
49 * - see utils.h for level defines
52 msg(int level
, const char *fmt
, ...)
57 if (level
> verbosity
)
62 if (level
< MSG_QUIET
)
63 ret
= vfprintf(stderr
, fmt
, ap
);
65 ret
= vfprintf(stdout
, fmt
, ap
);
71 /* Malloc which either succeeds or exits */
75 void *result
= malloc(size
);
77 msg(MSG_CRITICAL
, "Failed to malloc %zu bytes\n", size
);
83 /* Ditto for strdup */
85 xstrdup(const char *s
)
87 char *result
= strdup(s
);
89 msg(MSG_CRITICAL
, "Failed to strdup %zu bytes\n", strlen(s
));
95 /* Human-readable printout of binary data */
97 binary_print(const char *s
, ssize_t len
)
101 for (i
= 0; i
< len
; i
++) {
103 msg(MSG_DEBUG
, "%c", s
[i
]);
105 msg(MSG_DEBUG
, "0x%02X", (int)s
[i
]);
109 /* Writes data to a file or exits on failure */
111 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
113 if (size
&& fwrite(ptr
, size
, 1, stream
) != 1) {
114 msg(MSG_CRITICAL
, "Failed to write to file: %s\n",
120 /* Writes an int to a file, using len bytes, in little-endian order */
122 write_int(uint64_t value
, size_t len
, FILE *to
)
124 char buf
[sizeof(value
)];
127 for (i
= 0; i
< len
; i
++)
128 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
129 xfwrite(buf
, len
, to
);
132 /* Writes a binary string to a file */
134 write_binary_string(const char *string
, size_t len
, FILE *to
)
136 xfwrite(string
, len
, to
);
139 /* Writes a normal C string to a file */
141 write_string(const char *string
, FILE *to
)
143 xfwrite(string
, strlen(string
) + 1, to
);
146 /* Reads an int from a file, using len bytes, in little-endian order */
148 read_int(char **from
, size_t len
, const char *max
)
153 if (*from
+ len
> max
) {
155 "Attempt to read beyond end of file, corrupt file?\n");
159 for (i
= 0; i
< len
; i
++)
160 result
+= (((uint64_t)(*from
)[i
] & 0xff) << (8 * i
));
165 /* Reads a binary string from a file */
167 read_binary_string(char **from
, size_t len
, const char *max
)
171 if (*from
+ len
> max
) {
173 "Attempt to read beyond end of file, corrupt file?\n");
177 result
= xmalloc(len
);
178 memcpy(result
, *from
, len
);
183 /* Reads a normal C string from a file */
185 read_string(char **from
, const char *max
)
187 return read_binary_string(from
, strlen(*from
) + 1, max
);
190 /* For group caching */
191 static struct group
*gtable
= NULL
;
193 /* Initial setup of the gid table */
195 create_group_table(void)
200 for (count
= 0; getgrent(); count
++) /* Do nothing */;
202 gtable
= xmalloc(sizeof(struct group
) * (count
+ 1));
203 memset(gtable
, 0, sizeof(struct group
) * (count
+ 1));
206 for (index
= 0; (tmp
= getgrent()) && index
< count
; index
++) {
207 gtable
[index
].gr_gid
= tmp
->gr_gid
;
208 gtable
[index
].gr_name
= xstrdup(tmp
->gr_name
);
214 /* Caching version of getgrnam */
216 xgetgrnam(const char *name
)
221 create_group_table();
223 for (i
= 0; gtable
[i
].gr_name
; i
++) {
224 if (!strcmp(name
, gtable
[i
].gr_name
))
231 /* Caching version of getgrgid */
238 create_group_table();
240 for (i
= 0; gtable
[i
].gr_name
; i
++) {
241 if (gtable
[i
].gr_gid
== gid
)
248 /* For user caching */
249 static struct passwd
*ptable
= NULL
;
251 /* Initial setup of the passwd table */
253 create_passwd_table(void)
258 for (count
= 0; getpwent(); count
++) /* Do nothing */;
260 ptable
= xmalloc(sizeof(struct passwd
) * (count
+ 1));
261 memset(ptable
, 0, sizeof(struct passwd
) * (count
+ 1));
264 for (index
= 0; (tmp
= getpwent()) && index
< count
; index
++) {
265 ptable
[index
].pw_uid
= tmp
->pw_uid
;
266 ptable
[index
].pw_name
= xstrdup(tmp
->pw_name
);
272 /* Caching version of getpwnam */
274 xgetpwnam(const char *name
)
279 create_passwd_table();
281 for (i
= 0; ptable
[i
].pw_name
; i
++) {
282 if (!strcmp(name
, ptable
[i
].pw_name
))
289 /* Caching version of getpwuid */
296 create_passwd_table();
298 for (i
= 0; ptable
[i
].pw_name
; i
++) {
299 if (ptable
[i
].pw_uid
== uid
)