libsubid: don't print error messages on stderr by default
Closes #325 Add a new subid_init() function which can be used to specify the stream on which error messages should be printed. (If you want to get fancy you can redirect that to memory :) If subid_init() is not called, use stderr. If NULL is passed, then /dev/null will be used. This patch also fixes up the 'Prog', which previously had to be defined by any program linking against libsubid. Now, by default in libsubid it will show (subid). Once subid_init() is called, it will use the first variable passed to subid_init(). Signed-off-by: Serge Hallyn <serge@hallyn.com>
This commit is contained in:
@@ -47,19 +47,19 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
|
||||
int idx, argidx;
|
||||
|
||||
if (ranges < 0 || argc < 0) {
|
||||
fprintf(stderr, "%s: error calculating number of arguments\n", Prog);
|
||||
fprintf(shadow_logfd, "%s: error calculating number of arguments\n", Prog);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ranges != ((argc + 2) / 3)) {
|
||||
fprintf(stderr, "%s: ranges: %u is wrong for argc: %d\n", Prog, ranges, argc);
|
||||
fprintf(shadow_logfd, "%s: ranges: %u is wrong for argc: %d\n", Prog, ranges, argc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ranges * 3) > argc) {
|
||||
fprintf(stderr, "ranges: %u argc: %d\n",
|
||||
fprintf(shadow_logfd, "ranges: %u argc: %d\n",
|
||||
ranges, argc);
|
||||
fprintf(stderr,
|
||||
fprintf(shadow_logfd,
|
||||
_( "%s: Not enough arguments to form %u mappings\n"),
|
||||
Prog, ranges);
|
||||
return NULL;
|
||||
@@ -67,7 +67,7 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
|
||||
|
||||
mappings = calloc(ranges, sizeof(*mappings));
|
||||
if (!mappings) {
|
||||
fprintf(stderr, _( "%s: Memory allocation failure\n"),
|
||||
fprintf(shadow_logfd, _( "%s: Memory allocation failure\n"),
|
||||
Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -88,24 +88,24 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
|
||||
return NULL;
|
||||
}
|
||||
if (ULONG_MAX - mapping->upper <= mapping->count || ULONG_MAX - mapping->lower <= mapping->count) {
|
||||
fprintf(stderr, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
fprintf(shadow_logfd, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (mapping->upper > UINT_MAX ||
|
||||
mapping->lower > UINT_MAX ||
|
||||
mapping->count > UINT_MAX) {
|
||||
fprintf(stderr, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
fprintf(shadow_logfd, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (mapping->lower + mapping->count > UINT_MAX ||
|
||||
mapping->upper + mapping->count > UINT_MAX) {
|
||||
fprintf(stderr, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
fprintf(shadow_logfd, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (mapping->lower + mapping->count < mapping->lower ||
|
||||
mapping->upper + mapping->count < mapping->upper) {
|
||||
/* this one really shouldn't be possible given previous checks */
|
||||
fprintf(stderr, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
fprintf(shadow_logfd, _( "%s: subuid overflow detected.\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -176,19 +176,19 @@ void write_mapping(int proc_dir_fd, int ranges, struct map_range *mappings,
|
||||
} else if (strcmp(map_file, "gid_map") == 0) {
|
||||
cap = CAP_SETGID;
|
||||
} else {
|
||||
fprintf(stderr, _("%s: Invalid map file %s specified\n"), Prog, map_file);
|
||||
fprintf(shadow_logfd, _("%s: Invalid map file %s specified\n"), Prog, map_file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Align setuid- and fscaps-based new{g,u}idmap behavior. */
|
||||
if (geteuid() == 0 && geteuid() != ruid) {
|
||||
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
|
||||
fprintf(stderr, _("%s: Could not prctl(PR_SET_KEEPCAPS)\n"), Prog);
|
||||
fprintf(shadow_logfd, _("%s: Could not prctl(PR_SET_KEEPCAPS)\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (seteuid(ruid) < 0) {
|
||||
fprintf(stderr, _("%s: Could not seteuid to %d\n"), Prog, ruid);
|
||||
fprintf(shadow_logfd, _("%s: Could not seteuid to %d\n"), Prog, ruid);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,7 @@ void write_mapping(int proc_dir_fd, int ranges, struct map_range *mappings,
|
||||
data[0].effective |= CAP_TO_MASK(CAP_SETFCAP);
|
||||
data[0].permitted = data[0].effective;
|
||||
if (capset(&hdr, data) < 0) {
|
||||
fprintf(stderr, _("%s: Could not set caps\n"), Prog);
|
||||
fprintf(shadow_logfd, _("%s: Could not set caps\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
@@ -222,7 +222,7 @@ void write_mapping(int proc_dir_fd, int ranges, struct map_range *mappings,
|
||||
mapping->lower,
|
||||
mapping->count);
|
||||
if ((written <= 0) || (written >= (bufsize - (pos - buf)))) {
|
||||
fprintf(stderr, _("%s: snprintf failed!\n"), Prog);
|
||||
fprintf(shadow_logfd, _("%s: snprintf failed!\n"), Prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
pos += written;
|
||||
@@ -231,12 +231,12 @@ void write_mapping(int proc_dir_fd, int ranges, struct map_range *mappings,
|
||||
/* Write the mapping to the mapping file */
|
||||
fd = openat(proc_dir_fd, map_file, O_WRONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, _("%s: open of %s failed: %s\n"),
|
||||
fprintf(shadow_logfd, _("%s: open of %s failed: %s\n"),
|
||||
Prog, map_file, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (write(fd, buf, pos - buf) != (pos - buf)) {
|
||||
fprintf(stderr, _("%s: write to %s failed: %s\n"),
|
||||
fprintf(shadow_logfd, _("%s: write to %s failed: %s\n"),
|
||||
Prog, map_file, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user