1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Main functions of the program.
*
* Copyright (C) 2007-2008 David Härdeman <david@hardeman.nu>
* Copyright (C) 2015-2018 Przemyslaw Pawelczyk <przemoc@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include "utils.h"
/* Controls the verbosity level for msg() */
static int verbosity = 0;
/* Adjusts the verbosity level for msg() */
void
adjust_verbosity(int adj)
{
verbosity += adj;
}
/*
* Prints messages to console according to the current verbosity
* - see utils.h for level defines
*/
int
msg(int level, const char *fmt, ...)
{
int ret;
va_list ap;
if (level > verbosity)
return 0;
va_start(ap, fmt);
if (level < MSG_QUIET)
ret = vfprintf(stderr, fmt, ap);
else
ret = vfprintf(stdout, fmt, ap);
va_end(ap);
return ret;
}
/* Malloc which either succeeds or exits */
void *
xmalloc(size_t size)
{
void *result = malloc(size);
if (!result) {
msg(MSG_CRITICAL, "Failed to malloc %zu bytes\n", size);
exit(EXIT_FAILURE);
}
return result;
}
/* Ditto for realloc */
void *
xrealloc(void *ptr, size_t size)
{
void *result = realloc(ptr, size);
if (!result) {
msg(MSG_CRITICAL, "Failed to realloc %zu bytes\n", size);
exit(EXIT_FAILURE);
}
return result;
}
/* Ditto for strdup */
char *
xstrdup(const char *s)
{
char *result = strdup(s);
if (!result) {
msg(MSG_CRITICAL, "Failed to strdup %zu bytes\n", strlen(s));
exit(EXIT_FAILURE);
}
return result;
}
/* Human-readable printout of binary data */
void
binary_print(const char *s, ssize_t len)
{
ssize_t i;
for (i = 0; i < len; i++) {
if (isprint(s[i]))
msg(MSG_DEBUG, "%c", s[i]);
else
msg(MSG_DEBUG, "0x%02X", (int)s[i]);
}
}
/* Writes data to a file or exits on failure */
void
xfwrite(const void *ptr, size_t size, FILE *stream)
{
if (size && fwrite(ptr, size, 1, stream) != 1) {
msg(MSG_CRITICAL, "Failed to write to file: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
/* Writes an int to a file, using len bytes, in little-endian order */
void
write_int(uint64_t value, size_t len, FILE *to)
{
char buf[sizeof(value)];
size_t i;
for (i = 0; i < len; i++)
buf[i] = ((value >> (8 * i)) & 0xff);
xfwrite(buf, len, to);
}
/* Writes a binary string to a file */
void
write_binary_string(const char *string, size_t len, FILE *to)
{
xfwrite(string, len, to);
}
/* Writes a normal C string to a file */
void
write_string(const char *string, FILE *to)
{
xfwrite(string, strlen(string) + 1, to);
}
/* Get the value of a hex digit */
static int
xdigitval(int c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
return -1;
}
/* Check if a character being put into a URL must be encoded */
static int
char_url_mustencode(int c)
{
return (c >= 0 && c <= 0x20)
|| c == '%'
|| c == '\x7F';
}
/* Writes a single character to a file, URL-encoding if necessary */
void
write_char_url(int c, FILE *to)
{
int ok;
if (c < -127 || c > 255)
ok = 0;
else if (char_url_mustencode(c))
ok = fprintf(to, "%%%.2X", (unsigned char) c) == 3;
else
ok = fputc(c, to) != EOF;
if (!ok) {
msg(MSG_CRITICAL, "Failed to write to file: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
/* Writes binary data to a file, URL-encoding any bytes if necessary */
void
write_binary_url(const char *string, size_t len, FILE *to)
{
size_t i;
for (i = 0; i < len; ++i)
write_char_url(string[i], to);
}
/* Writes a normal C string to a file, URL-encoding any chars if necessary */
void
write_string_url(const char *string, FILE *to)
{
int c;
while ((c = *string++) != '\0')
write_char_url(c, to);
}
/* Reads a single char from a file */
int
read_char(const char **from, const char *max)
{
if (*from >= max)
return EOF;
return *((*from)++);
}
/* Reads an int from a file, using len bytes, in little-endian order */
uint64_t
read_int(const char **from, size_t len, const char *max)
{
uint64_t result = 0;
size_t i;
if (*from + len > max) {
msg(MSG_CRITICAL,
"Attempt to read beyond end of file, corrupt file?\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < len; i++)
result += (((*from)[i] & 0xff) << (8 * i));
*from += len;
return result;
}
/* Reads an int from a file, stored as an ASCII string, to the first non-digit */
long
read_int_string(const char **from, const char *max, int base)
{
long result = 0;
int sign = 1, i;
if (*from < max && **from == '-') {
sign = -1;
*from += 1;
}
while (*from < max && (i = xdigitval(**from)) >= 0 && i < base) {
result *= base;
result += i;
*from += 1;
}
return result * sign;
}
/* Reads a binary string from a file */
char *
read_binary_string(const char **from, size_t len, const char *max)
{
char *result;
if (*from + len > max) {
msg(MSG_CRITICAL,
"Attempt to read beyond end of file, corrupt file?\n");
exit(EXIT_FAILURE);
}
result = xmalloc(len);
memcpy(result, *from, len);
*from += len;
return result;
}
/* Reads binary data from a file, which was stored URL-encoded */
char *
read_binary_url(const char **pfrom, const char *max, ssize_t *plen)
{
const char *from;
size_t bufsize, len;
char * buf;
int hi, lo;
from = *pfrom;
bufsize = 32;
buf = xmalloc(bufsize);
len = 0;
while (from < max) {
if (len == bufsize) {
bufsize *= 2;
buf = xrealloc(buf, bufsize);
}
if (*from == '%' && (max - from) >= 3) {
/* URL-encoded char - decode */
from++;
hi = xdigitval(*from++);
lo = xdigitval(*from++);
if (hi < 0 || lo < 0) {
/* Invalid encoding */
free(buf);
*pfrom = from;
return NULL;
}
if (hi == 0 && lo == 0 && !plen) {
/* NUL in C string? Stop here */
break;
}
buf[len++] = (hi << 4) + lo;
}
else if (char_url_mustencode(*from)) {
/* Un-encoded char. Stop here */
break;
}
else {
buf[len++] = *from++;
}
}
if (plen) {
/* plen is non-NULL means read binary data - return length */
*plen = len;
}
else {
/* plen is NULL actually means read C string - NUL terminate */
if (len == bufsize) {
bufsize *= 2;
buf = xrealloc(buf, bufsize);
}
buf[len++] = '\0';
}
*pfrom = from;
return buf;
}
/* Reads a normal C string from a file */
char *
read_string(const char **from, const char *max)
{
return read_binary_string(from, strlen(*from) + 1, max);
}
/* Reads a normal C string from a file, which was stored URL-encoded */
char *
read_string_url(const char **from, const char *max)
{
return read_binary_url(from, max, NULL);
}
/* For group caching */
static struct group *gtable = NULL;
/* Initial setup of the gid table */
static void
create_group_table(void)
{
struct group *tmp;
int count, index;
for (count = 0; getgrent(); count++) /* Do nothing */;
gtable = xmalloc(sizeof(struct group) * (count + 1));
memset(gtable, 0, sizeof(struct group) * (count + 1));
setgrent();
for (index = 0; (tmp = getgrent()) && index < count; index++) {
gtable[index].gr_gid = tmp->gr_gid;
gtable[index].gr_name = xstrdup(tmp->gr_name);
}
endgrent();
}
/* Caching version of getgrnam */
struct group *
xgetgrnam(const char *name)
{
int i;
if (!gtable)
create_group_table();
for (i = 0; gtable[i].gr_name; i++) {
if (!strcmp(name, gtable[i].gr_name))
return &(gtable[i]);
}
return NULL;
}
/* Caching version of getgrgid */
struct group *
xgetgrgid(gid_t gid)
{
int i;
if (!gtable)
create_group_table();
for (i = 0; gtable[i].gr_name; i++) {
if (gtable[i].gr_gid == gid)
return &(gtable[i]);
}
return NULL;
}
/* For user caching */
static struct passwd *ptable = NULL;
/* Initial setup of the passwd table */
static void
create_passwd_table(void)
{
struct passwd *tmp;
int count, index;
for (count = 0; getpwent(); count++) /* Do nothing */;
ptable = xmalloc(sizeof(struct passwd) * (count + 1));
memset(ptable, 0, sizeof(struct passwd) * (count + 1));
setpwent();
for (index = 0; (tmp = getpwent()) && index < count; index++) {
ptable[index].pw_uid = tmp->pw_uid;
ptable[index].pw_name = xstrdup(tmp->pw_name);
}
endpwent();
}
/* Caching version of getpwnam */
struct passwd *
xgetpwnam(const char *name)
{
int i;
if (!ptable)
create_passwd_table();
for (i = 0; ptable[i].pw_name; i++) {
if (!strcmp(name, ptable[i].pw_name))
return &(ptable[i]);
}
return NULL;
}
/* Caching version of getpwuid */
struct passwd *
xgetpwuid(uid_t uid)
{
int i;
if (!ptable)
create_passwd_table();
for (i = 0; ptable[i].pw_name; i++) {
if (ptable[i].pw_uid == uid)
return &(ptable[i]);
}
return NULL;
}
|