2 * Main functions of the program.
4 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <sys/types.h>
36 /* Controls the verbosity level for msg() */
37 static int verbosity
= 0;
39 /* Adjusts the verbosity level for msg() */
41 adjust_verbosity(int adj
)
47 * Prints messages to console according to the current verbosity
48 * - see utils.h for level defines
51 msg(int level
, const char *fmt
, ...)
56 if (level
> verbosity
)
61 if (level
< MSG_QUIET
)
62 ret
= vfprintf(stderr
, fmt
, ap
);
64 ret
= vfprintf(stdout
, fmt
, ap
);
70 /* Malloc which either succeeds or exits */
74 void *result
= malloc(size
);
76 msg(MSG_CRITICAL
, "Failed to malloc %zi bytes\n", size
);
82 /* Ditto for strdup */
84 xstrdup(const char *s
)
86 char *result
= strdup(s
);
88 msg(MSG_CRITICAL
, "Failed to strdup %zi bytes\n", strlen(s
));
94 /* Human-readable printout of binary data */
96 binary_print(const char *s
, ssize_t len
)
100 for (i
= 0; i
< len
; i
++) {
102 msg(MSG_DEBUG
, "%c", s
[i
]);
104 msg(MSG_DEBUG
, "0x%02X", (int)s
[i
]);
108 /* Writes data to a file or exits on failure */
110 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
112 if (size
&& fwrite(ptr
, size
, 1, stream
) != 1) {
113 msg(MSG_CRITICAL
, "Failed to write to file: %s\n",
119 /* Writes an int to a file, using len bytes, in bigendian order */
121 write_int(uint64_t value
, size_t len
, FILE *to
)
126 for (i
= 0; i
< len
; i
++)
127 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
128 xfwrite(buf
, len
, to
);
131 /* Writes a binary string to a file */
133 write_binary_string(const char *string
, size_t len
, FILE *to
)
135 xfwrite(string
, len
, to
);
138 /* Writes a normal C string to a file */
140 write_string(const char *string
, FILE *to
)
142 xfwrite(string
, strlen(string
) + 1, to
);
145 /* Reads an int from a file, using len bytes, in bigendian order */
147 read_int(char **from
, size_t len
, const char *max
)
152 if (*from
+ len
> max
) {
154 "Attempt to read beyond end of file, corrupt file?\n");
158 for (i
= 0; i
< len
; i
++)
159 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
164 /* Reads a binary string from a file */
166 read_binary_string(char **from
, size_t len
, const char *max
)
170 if (*from
+ len
> max
) {
172 "Attempt to read beyond end of file, corrupt file?\n");
176 result
= xmalloc(len
);
177 strncpy(result
, *from
, len
);
182 /* Reads a normal C string from a file */
184 read_string(char **from
, const char *max
)
186 return read_binary_string(from
, strlen(*from
) + 1, max
);
189 /* For group caching */
190 static struct group
*gtable
= NULL
;
192 /* Initial setup of the gid table */
199 for (count
= 0; getgrent(); count
++) /* Do nothing */;
201 gtable
= xmalloc(sizeof(struct group
) * (count
+ 1));
202 memset(gtable
, 0, sizeof(struct group
) * (count
+ 1));
205 for (index
= 0; (tmp
= getgrent()) && index
< count
; index
++) {
206 gtable
[index
].gr_gid
= tmp
->gr_gid
;
207 gtable
[index
].gr_name
= xstrdup(tmp
->gr_name
);
213 /* Caching version of getgrnam */
215 xgetgrnam(const char *name
)
220 create_group_table();
222 for (i
= 0; gtable
[i
].gr_name
; i
++) {
223 if (!strcmp(name
, gtable
[i
].gr_name
))
230 /* Caching version of getgrgid */
237 create_group_table();
239 for (i
= 0; gtable
[i
].gr_name
; i
++) {
240 if (gtable
[i
].gr_gid
== gid
)
247 /* For user caching */
248 static struct passwd
*ptable
= NULL
;
250 /* Initial setup of the passwd table */
252 create_passwd_table()
257 for (count
= 0; getpwent(); count
++) /* Do nothing */;
259 ptable
= xmalloc(sizeof(struct passwd
) * (count
+ 1));
260 memset(ptable
, 0, sizeof(struct passwd
) * (count
+ 1));
263 for (index
= 0; (tmp
= getpwent()) && index
< count
; index
++) {
264 ptable
[index
].pw_uid
= tmp
->pw_uid
;
265 ptable
[index
].pw_name
= xstrdup(tmp
->pw_name
);
271 /* Caching version of getpwnam */
273 xgetpwnam(const char *name
)
278 create_passwd_table();
280 for (i
= 0; ptable
[i
].pw_name
; i
++) {
281 if (!strcmp(name
, ptable
[i
].pw_name
))
288 /* Caching version of getpwuid */
295 create_passwd_table();
297 for (i
= 0; ptable
[i
].pw_name
; i
++) {
298 if (ptable
[i
].pw_uid
== uid
)