Use atoid() when parsing user/group IDs and print them as unsigned int.

This commit is contained in:
Todd C. Miller
2013-12-06 14:10:03 -07:00
parent e964aa4d8c
commit acdff2d998

View File

@@ -56,6 +56,7 @@ extern bool parse_error;
*/ */
enum json_value_type { enum json_value_type {
JSON_STRING, JSON_STRING,
JSON_ID,
JSON_NUMBER, JSON_NUMBER,
JSON_OBJECT, JSON_OBJECT,
JSON_ARRAY, JSON_ARRAY,
@@ -72,6 +73,7 @@ struct json_value {
union { union {
char *string; char *string;
int number; int number;
id_t id;
bool boolean; bool boolean;
} u; } u;
}; };
@@ -198,6 +200,9 @@ print_pair_json(const char *pre, const char *name,
case JSON_STRING: case JSON_STRING:
print_string_json(value->u.string); print_string_json(value->u.string);
break; break;
case JSON_ID:
printf("%u", (unsigned int)value->u.id);
break;
case JSON_NUMBER: case JSON_NUMBER:
printf("%d", value->u.number); printf("%d", value->u.number);
break; break;
@@ -345,6 +350,8 @@ print_member_json(struct member *m, enum word_type word_type, bool last_one,
{ {
struct json_value value; struct json_value value;
const char *typestr; const char *typestr;
const char *errstr;
id_t id;
debug_decl(print_member_json, SUDO_DEBUG_UTIL) debug_decl(print_member_json, SUDO_DEBUG_UTIL)
/* Most of the time we print a string. */ /* Most of the time we print a string. */
@@ -356,20 +363,30 @@ print_member_json(struct member *m, enum word_type word_type, bool last_one,
value.u.string++; /* skip leading '%' */ value.u.string++; /* skip leading '%' */
if (*value.u.string == ':') { if (*value.u.string == ':') {
value.u.string++; value.u.string++;
typestr = "nonunixgroup";
if (*value.u.string == '#') { if (*value.u.string == '#') {
value.type = JSON_NUMBER; id = atoid(m->name + 3, NULL, NULL, &errstr);
value.u.number = atoi(m->name + 3); /* XXX - use atoid? */ if (errstr != NULL) {
typestr = "nonunixgid"; warningx("internal error: non-Unix group ID %s: \"%s\"",
} else { errstr, m->name);
typestr = "nonunixgroup"; } else {
value.type = JSON_ID;
value.u.id = id;
typestr = "nonunixgid";
}
} }
} else { } else {
typestr = "usergroup";
if (*value.u.string == '#') { if (*value.u.string == '#') {
value.type = JSON_NUMBER; id = atoid(m->name + 2, NULL, NULL, &errstr);
value.u.number = atoi(m->name + 2); /* XXX - use atoid? */ if (errstr != NULL) {
typestr = "usergid"; warningx("internal error: group ID %s: \"%s\"",
} else { errstr, m->name);
typestr = "usergroup"; } else {
value.type = JSON_ID;
value.u.id = id;
typestr = "usergid";
}
} }
} }
break; break;
@@ -393,12 +410,17 @@ print_member_json(struct member *m, enum word_type word_type, bool last_one,
break; break;
case TYPE_RUNASUSER: case TYPE_RUNASUSER:
case TYPE_USERNAME: case TYPE_USERNAME:
typestr = "username";
if (*value.u.string == '#') { if (*value.u.string == '#') {
value.type = JSON_NUMBER; id = atoid(m->name + 1, NULL, NULL, &errstr);
value.u.number = atoi(m->name + 1); /* XXX - use atoid? */ if (errstr != NULL) {
typestr = "userid"; warningx("internal error: user ID %s: \"%s\"",
} else { errstr, m->name);
typestr = "username"; } else {
value.type = JSON_ID;
value.u.id = id;
typestr = "userid";
}
} }
break; break;
default: default: