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; only version 2 of the License is applicable.
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.
29 #include <sys/types.h>
35 /* Controls the verbosity level for msg() */
36 static int verbosity
= 0;
38 /* Adjusts the verbosity level for msg() */
40 adjust_verbosity(int adj
)
46 * Prints messages to console according to the current verbosity
47 * - see utils.h for level defines
50 msg(int level
, const char *fmt
, ...)
55 if (level
> verbosity
)
60 if (level
< MSG_QUIET
)
61 ret
= vfprintf(stderr
, fmt
, ap
);
63 ret
= vfprintf(stdout
, fmt
, ap
);
69 /* Malloc which either succeeds or exits */
73 void *result
= malloc(size
);
75 msg(MSG_CRITICAL
, "Failed to malloc %zi bytes\n", size
);
81 /* Ditto for strdup */
83 xstrdup(const char *s
)
85 char *result
= strdup(s
);
87 msg(MSG_CRITICAL
, "Failed to strdup %zi bytes\n", strlen(s
));
93 /* Human-readable printout of binary data */
95 binary_print(const char *s
, ssize_t len
)
99 for (i
= 0; i
< len
; i
++) {
101 msg(MSG_DEBUG
, "%c", s
[i
]);
103 msg(MSG_DEBUG
, "0x%02X", (int)s
[i
]);
107 /* Writes data to a file or exits on failure */
109 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
111 if (size
&& fwrite(ptr
, size
, 1, stream
) != 1) {
112 msg(MSG_CRITICAL
, "Failed to write to file: %s\n",
118 /* Writes an int to a file, using len bytes, in bigendian order */
120 write_int(uint64_t value
, size_t len
, FILE *to
)
125 for (i
= 0; i
< len
; i
++)
126 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
127 xfwrite(buf
, len
, to
);
130 /* Writes a binary string to a file */
132 write_binary_string(const char *string
, size_t len
, FILE *to
)
134 xfwrite(string
, len
, to
);
137 /* Writes a normal C string to a file */
139 write_string(const char *string
, FILE *to
)
141 xfwrite(string
, strlen(string
) + 1, to
);
144 /* Reads an int from a file, using len bytes, in bigendian order */
146 read_int(char **from
, size_t len
, const char *max
)
151 if (*from
+ len
> max
) {
153 "Attempt to read beyond end of file, corrupt file?\n");
157 for (i
= 0; i
< len
; i
++)
158 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
163 /* Reads a binary string from a file */
165 read_binary_string(char **from
, size_t len
, const char *max
)
169 if (*from
+ len
> max
) {
171 "Attempt to read beyond end of file, corrupt file?\n");
175 result
= xmalloc(len
);
176 memcpy(result
, *from
, len
);
181 /* Reads a normal C string from a file */
183 read_string(char **from
, const char *max
)
185 return read_binary_string(from
, strlen(*from
) + 1, max
);
188 /* For group caching */
189 static struct group
*gtable
= NULL
;
191 /* Initial setup of the gid table */
198 for (count
= 0; getgrent(); count
++) /* Do nothing */;
200 gtable
= xmalloc(sizeof(struct group
) * (count
+ 1));
201 memset(gtable
, 0, sizeof(struct group
) * (count
+ 1));
204 for (index
= 0; (tmp
= getgrent()) && index
< count
; index
++) {
205 gtable
[index
].gr_gid
= tmp
->gr_gid
;
206 gtable
[index
].gr_name
= xstrdup(tmp
->gr_name
);
212 /* Caching version of getgrnam */
214 xgetgrnam(const char *name
)
219 create_group_table();
221 for (i
= 0; gtable
[i
].gr_name
; i
++) {
222 if (!strcmp(name
, gtable
[i
].gr_name
))
229 /* Caching version of getgrgid */
236 create_group_table();
238 for (i
= 0; gtable
[i
].gr_name
; i
++) {
239 if (gtable
[i
].gr_gid
== gid
)
246 /* For user caching */
247 static struct passwd
*ptable
= NULL
;
249 /* Initial setup of the passwd table */
251 create_passwd_table()
256 for (count
= 0; getpwent(); count
++) /* Do nothing */;
258 ptable
= xmalloc(sizeof(struct passwd
) * (count
+ 1));
259 memset(ptable
, 0, sizeof(struct passwd
) * (count
+ 1));
262 for (index
= 0; (tmp
= getpwent()) && index
< count
; index
++) {
263 ptable
[index
].pw_uid
= tmp
->pw_uid
;
264 ptable
[index
].pw_name
= xstrdup(tmp
->pw_name
);
270 /* Caching version of getpwnam */
272 xgetpwnam(const char *name
)
277 create_passwd_table();
279 for (i
= 0; ptable
[i
].pw_name
; i
++) {
280 if (!strcmp(name
, ptable
[i
].pw_name
))
287 /* Caching version of getpwuid */
294 create_passwd_table();
296 for (i
= 0; ptable
[i
].pw_name
; i
++) {
297 if (ptable
[i
].pw_uid
== uid
)