Rename sudo_strtoid() to sudo_strtoidx() and add simplified sudo_strtoid()

This commit is contained in:
Todd C. Miller
2019-10-20 10:21:29 -06:00
parent 6260bf60b4
commit 40bf4081be
21 changed files with 71 additions and 52 deletions

View File

@@ -252,7 +252,10 @@ __dso_public long long sudo_strtonum(const char *, long long, long long, const c
/* strtoid.c */
__dso_public id_t sudo_strtoid_v1(const char *str, const char *sep, char **endp, const char **errstr);
#define sudo_strtoid(_a, _b, _c, _d) sudo_strtoid_v1((_a), (_b), (_c), (_d))
__dso_public id_t sudo_strtoid_v2(const char *str, const char **errstr);
#define sudo_strtoid(_a, _b) sudo_strtoid_v2((_a), (_b))
__dso_public id_t sudo_strtoidx_v1(const char *str, const char *sep, char **endp, const char **errstr);
#define sudo_strtoidx(_a, _b, _c, _d) sudo_strtoidx_v1((_a), (_b), (_c), (_d))
/* strtomode.c */
__dso_public int sudo_strtomode_v1(const char *cp, const char **errstr);

View File

@@ -165,7 +165,7 @@ sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
groups[0] = basegid;
for (cp = strtok_r(grset, ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
gid = sudo_strtoid(cp, NULL, NULL, &errstr);
gid = sudo_strtoid(cp, &errstr);
if (errstr == NULL && gid != basegid) {
if (ngroups == grpsize)
goto done;
@@ -251,7 +251,7 @@ str2grp(const char *instr, int inlen, void *ent, char *buf, int buflen)
if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
return yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE;
*fieldsep++ = '\0';
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL) {
/*
* A range error is always a fatal error, but ignore garbage

View File

@@ -76,7 +76,7 @@ sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp
gids[ngids++] = *basegid;
cp = gidstr;
do {
gids[ngids] = (GETGROUPS_T) sudo_strtoid(cp, ",", &ep, &errstr);
gids[ngids] = (GETGROUPS_T) sudo_strtoidx(cp, ",", &ep, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), cp, U_(errstr));
free(gids);

View File

@@ -29,14 +29,14 @@
__dso_public int main(int argc, char *argv[]);
/* sudo_strtoid() tests */
static struct strtoid_data {
/* sudo_strtoidx() tests */
static struct strtoidx_data {
const char *idstr;
id_t id;
const char *sep;
const char *ep;
int errnum;
} strtoid_data[] = {
} strtoidx_data[] = {
{ "0,1", 0, ",", ",", 0 },
{ "10", 10, NULL, NULL, 0 },
{ "-1", 0, NULL, NULL, EINVAL },
@@ -52,12 +52,12 @@ static struct strtoid_data {
};
/*
* Simple tests for sudo_strtoid()
* Simple tests for sudo_strtoidx()
*/
int
main(int argc, char *argv[])
{
struct strtoid_data *d;
struct strtoidx_data *d;
const char *errstr;
char *ep;
int errors = 0;
@@ -66,10 +66,10 @@ main(int argc, char *argv[])
initprogname(argc > 0 ? argv[0] : "strtoid_test");
for (d = strtoid_data; d->idstr != NULL; d++) {
for (d = strtoidx_data; d->idstr != NULL; d++) {
ntests++;
errstr = "some error";
value = sudo_strtoid(d->idstr, d->sep, &ep, &errstr);
value = sudo_strtoidx(d->idstr, d->sep, &ep, &errstr);
if (d->errnum != 0) {
if (errstr == NULL) {
sudo_warnx_nodebug("FAIL: %s: missing errstr for errno %d",

View File

@@ -79,7 +79,7 @@ valid_separator(const char *p, const char *ep, const char *sep)
* On error, returns 0 and sets errstr.
*/
id_t
sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstrp)
sudo_strtoidx_v1(const char *p, const char *sep, char **endp, const char **errstrp)
{
const char *errstr;
char *ep;
@@ -104,3 +104,17 @@ sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstr
*endp = ep;
debug_return_id_t(ret);
}
/* Backwards compatibility */
id_t
sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstrp)
{
return sudo_strtoidx_v1(p, sep, endp, errstrp);
}
/* Simplified interface */
id_t
sudo_strtoid_v2(const char *p, const char **errstrp)
{
return sudo_strtoidx_v1(p, NULL, NULL, errstrp);
}

View File

@@ -98,6 +98,8 @@ sudo_setgroups_v1
sudo_strsplit_v1
sudo_strtobool_v1
sudo_strtoid_v1
sudo_strtoid_v2
sudo_strtoidx_v1
sudo_strtomode_v1
sudo_strtonum
sudo_term_cbreak_v1

View File

@@ -116,7 +116,7 @@ next_entry:
if ((colon = strchr(cp = colon, ':')) == NULL)
goto next_entry;
*colon++ = '\0';
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
goto next_entry;
gr.gr_gid = (gid_t)id;

View File

@@ -707,7 +707,7 @@ userlist_matches_filter(struct sudoers_parse_tree *parse_tree,
if (s->str[0] == '#') {
const char *errstr;
uid_t uid = sudo_strtoid(s->str + 1, NULL, NULL, &errstr);
uid_t uid = sudo_strtoid(s->str + 1, &errstr);
if (errstr == NULL)
pw = sudo_getpwuid(uid);
}

View File

@@ -372,7 +372,7 @@ print_member_json_int(FILE *fp, struct sudoers_parse_tree *parse_tree,
value.u.string++;
typestr = "nonunixgroup";
if (*value.u.string == '#') {
id = sudo_strtoid(value.u.string + 1, NULL, NULL, &errstr);
id = sudo_strtoid(value.u.string + 1, &errstr);
if (errstr != NULL) {
sudo_warnx("internal error: non-Unix group ID %s: \"%s\"",
errstr, value.u.string + 1);
@@ -385,7 +385,7 @@ print_member_json_int(FILE *fp, struct sudoers_parse_tree *parse_tree,
} else {
typestr = "usergroup";
if (*value.u.string == '#') {
id = sudo_strtoid(value.u.string + 1, NULL, NULL, &errstr);
id = sudo_strtoid(value.u.string + 1, &errstr);
if (errstr != NULL) {
sudo_warnx("internal error: group ID %s: \"%s\"",
errstr, value.u.string + 1);
@@ -424,7 +424,7 @@ print_member_json_int(FILE *fp, struct sudoers_parse_tree *parse_tree,
case TYPE_USERNAME:
typestr = "username";
if (*value.u.string == '#') {
id = sudo_strtoid(value.u.string + 1, NULL, NULL, &errstr);
id = sudo_strtoid(value.u.string + 1, &errstr);
if (errstr != NULL) {
sudo_warnx("internal error: user ID %s: \"%s\"",
errstr, name);

View File

@@ -106,7 +106,7 @@ cvtsudoers_make_pwitem(uid_t uid, const char *name)
if (s->str[0] != '#')
continue;
filter_uid = sudo_strtoid(s->str + 1, NULL, NULL, &errstr);
filter_uid = sudo_strtoid(s->str + 1, &errstr);
if (errstr == NULL) {
if (uid != filter_uid)
continue;
@@ -213,7 +213,7 @@ cvtsudoers_make_gritem(gid_t gid, const char *name)
if (s->str[0] != '#')
continue;
filter_gid = sudo_strtoid(s->str + 1, NULL, NULL, &errstr);
filter_gid = sudo_strtoid(s->str + 1, &errstr);
if (errstr == NULL) {
if (gid != filter_gid)
continue;
@@ -335,7 +335,7 @@ cvtsudoers_make_gidlist_item(const struct passwd *pw, char * const *unused1,
STAILQ_FOREACH(s, &filters->groups, entries) {
if (s->str[0] == '#') {
const char *errstr;
gid_t gid = sudo_strtoid(s->str + 1, NULL, NULL, &errstr);
gid_t gid = sudo_strtoid(s->str + 1, &errstr);
if (errstr == NULL) {
/* Valid gid. */
gids[ngids++] = gid;
@@ -462,7 +462,7 @@ again:
STAILQ_FOREACH(s, &filters->groups, entries) {
if (s->str[0] == '#') {
const char *errstr;
sudo_strtoid(s->str + 1, NULL, NULL, &errstr);
sudo_strtoid(s->str + 1, &errstr);
if (errstr == NULL) {
/* Group ID not name, ignore it. */
continue;

View File

@@ -742,7 +742,7 @@ iolog_deserialize_info(struct iolog_details *details, char * const user_info[],
if (runas_euid_str != NULL)
runas_uid_str = runas_euid_str;
if (runas_uid_str != NULL) {
id = sudo_strtoid(runas_uid_str, NULL, NULL, &errstr);
id = sudo_strtoid(runas_uid_str, &errstr);
if (errstr != NULL)
sudo_warnx("runas uid %s: %s", runas_uid_str, U_(errstr));
else
@@ -751,7 +751,7 @@ iolog_deserialize_info(struct iolog_details *details, char * const user_info[],
if (runas_egid_str != NULL)
runas_gid_str = runas_egid_str;
if (runas_gid_str != NULL) {
id = sudo_strtoid(runas_gid_str, NULL, NULL, &errstr);
id = sudo_strtoid(runas_gid_str, &errstr);
if (errstr != NULL)
sudo_warnx("runas gid %s: %s", runas_gid_str, U_(errstr));
else

View File

@@ -453,7 +453,7 @@ userpw_matches(const char *sudoers_user, const char *user, const struct passwd *
debug_decl(userpw_matches, SUDOERS_DEBUG_MATCH)
if (pw != NULL && *sudoers_user == '#') {
uid = (uid_t) sudo_strtoid(sudoers_user + 1, NULL, NULL, &errstr);
uid = (uid_t) sudo_strtoid(sudoers_user + 1, &errstr);
if (errstr == NULL && uid == pw->pw_uid) {
rc = true;
goto done;
@@ -483,7 +483,7 @@ group_matches(const char *sudoers_group, const struct group *gr)
debug_decl(group_matches, SUDOERS_DEBUG_MATCH)
if (*sudoers_group == '#') {
gid = (gid_t) sudo_strtoid(sudoers_group + 1, NULL, NULL, &errstr);
gid = (gid_t) sudo_strtoid(sudoers_group + 1, &errstr);
if (errstr == NULL && gid == gr->gr_gid) {
rc = true;
goto done;

View File

@@ -129,7 +129,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
}
if (MATCHES(*cur, "sudoers_uid=")) {
p = *cur + sizeof("sudoers_uid=") - 1;
sudoers_uid = (uid_t) sudo_strtoid(p, NULL, NULL, &errstr);
sudoers_uid = (uid_t) sudo_strtoid(p, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
goto bad;
@@ -138,7 +138,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
}
if (MATCHES(*cur, "sudoers_gid=")) {
p = *cur + sizeof("sudoers_gid=") - 1;
sudoers_gid = (gid_t) sudo_strtoid(p, NULL, NULL, &errstr);
sudoers_gid = (gid_t) sudo_strtoid(p, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
goto bad;
@@ -343,7 +343,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
}
if (MATCHES(*cur, "uid=")) {
p = *cur + sizeof("uid=") - 1;
user_uid = (uid_t) sudo_strtoid(p, NULL, NULL, &errstr);
user_uid = (uid_t) sudo_strtoid(p, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
goto bad;
@@ -353,7 +353,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
}
if (MATCHES(*cur, "gid=")) {
p = *cur + sizeof("gid=") - 1;
user_gid = (gid_t) sudo_strtoid(p, NULL, NULL, &errstr);
user_gid = (gid_t) sudo_strtoid(p, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
goto bad;
@@ -416,7 +416,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
}
if (MATCHES(*cur, "sid=")) {
p = *cur + sizeof("sid=") - 1;
user_sid = (pid_t) sudo_strtoid(p, NULL, NULL, &errstr);
user_sid = (pid_t) sudo_strtoid(p, &errstr);
if (errstr != NULL) {
sudo_warnx(U_("%s: %s"), *cur, U_(errstr));
goto bad;

View File

@@ -436,7 +436,7 @@ sudo_fakepwnam(const char *user, gid_t gid)
uid_t uid;
debug_decl(sudo_fakepwnam, SUDOERS_DEBUG_NSS)
uid = (uid_t) sudo_strtoid(user + 1, NULL, NULL, &errstr);
uid = (uid_t) sudo_strtoid(user + 1, &errstr);
if (errstr != NULL) {
sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
"uid %s %s", user, errstr);
@@ -671,7 +671,7 @@ sudo_fakegrnam(const char *group)
debug_return_ptr(NULL);
}
gr = &gritem->gr;
gr->gr_gid = (gid_t) sudo_strtoid(group + 1, NULL, NULL, &errstr);
gr->gr_gid = (gid_t) sudo_strtoid(group + 1, &errstr);
gr->gr_name = (char *)(gritem + 1);
memcpy(gr->gr_name, group, name_len + 1);
if (errstr != NULL) {
@@ -1019,7 +1019,7 @@ user_in_group(const struct passwd *pw, const char *group)
*/
if (group[0] == '#') {
const char *errstr;
gid_t gid = (gid_t) sudo_strtoid(group + 1, NULL, NULL, &errstr);
gid_t gid = (gid_t) sudo_strtoid(group + 1, &errstr);
if (errstr != NULL) {
sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
"gid %s %s", group, errstr);

View File

@@ -146,7 +146,7 @@ main(int argc, char *argv[])
user_name = strdup(line);
break;
case 2:
user_gid = (gid_t)sudo_strtoid(line, NULL, NULL, &errstr);
user_gid = (gid_t)sudo_strtoid(line, &errstr);
if (errstr != NULL)
sudo_fatalx("group ID %s: %s", line, errstr);
break;
@@ -156,7 +156,7 @@ main(int argc, char *argv[])
runas_pw->pw_name = strdup(line);
break;
case 4:
runas_pw->pw_gid = (gid_t)sudo_strtoid(line, NULL, NULL, &errstr);
runas_pw->pw_gid = (gid_t)sudo_strtoid(line, &errstr);
if (errstr != NULL)
sudo_fatalx("group ID %s: %s", line, errstr);
break;

View File

@@ -344,7 +344,7 @@ sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
if (*def_timestampowner == '#') {
const char *errstr;
uid_t uid = sudo_strtoid(def_timestampowner + 1, NULL, NULL, &errstr);
uid_t uid = sudo_strtoid(def_timestampowner + 1, &errstr);
if (errstr == NULL)
pw = sudo_getpwuid(uid);
}
@@ -1151,7 +1151,7 @@ set_runaspw(const char *user, bool quiet)
if (*user == '#') {
const char *errstr;
uid_t uid = sudo_strtoid(user + 1, NULL, NULL, &errstr);
uid_t uid = sudo_strtoid(user + 1, &errstr);
if (errstr == NULL) {
if ((pw = sudo_getpwuid(uid)) == NULL)
pw = sudo_fakepwnam(user, user_gid);
@@ -1182,7 +1182,7 @@ set_runasgr(const char *group, bool quiet)
if (*group == '#') {
const char *errstr;
gid_t gid = sudo_strtoid(group + 1, NULL, NULL, &errstr);
gid_t gid = sudo_strtoid(group + 1, &errstr);
if (errstr == NULL) {
if ((gr = sudo_getgrgid(gid)) == NULL)
gr = sudo_fakegrnam(group);

View File

@@ -155,7 +155,7 @@ main(int argc, char *argv[])
dflag = 1;
break;
case 'G':
sudoers_gid = (gid_t)sudo_strtoid(optarg, NULL, NULL, &errstr);
sudoers_gid = (gid_t)sudo_strtoid(optarg, &errstr);
if (errstr != NULL)
sudo_fatalx("group ID %s: %s", optarg, errstr);
break;
@@ -186,7 +186,7 @@ main(int argc, char *argv[])
trace_print = testsudoers_error;
break;
case 'U':
sudoers_uid = (uid_t)sudo_strtoid(optarg, NULL, NULL, &errstr);
sudoers_uid = (uid_t)sudo_strtoid(optarg, &errstr);
if (errstr != NULL)
sudo_fatalx("user ID %s: %s", optarg, errstr);
break;
@@ -389,7 +389,7 @@ set_runaspw(const char *user)
if (*user == '#') {
const char *errstr;
uid_t uid = sudo_strtoid(user + 1, NULL, NULL, &errstr);
uid_t uid = sudo_strtoid(user + 1, &errstr);
if (errstr == NULL) {
if ((pw = sudo_getpwuid(uid)) == NULL)
pw = sudo_fakepwnam(user, user_gid);
@@ -413,7 +413,7 @@ set_runasgr(const char *group)
if (*group == '#') {
const char *errstr;
gid_t gid = sudo_strtoid(group + 1, NULL, NULL, &errstr);
gid_t gid = sudo_strtoid(group + 1, &errstr);
if (errstr == NULL) {
if ((gr = sudo_getgrgid(gid)) == NULL)
gr = sudo_fakegrnam(group);

View File

@@ -137,14 +137,14 @@ next_entry:
if ((colon = strchr(cp = colon, ':')) == NULL)
goto next_entry;
*colon++ = '\0';
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
goto next_entry;
pw.pw_uid = (uid_t)id;
if ((colon = strchr(cp = colon, ':')) == NULL)
goto next_entry;
*colon++ = '\0';
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
goto next_entry;
pw.pw_gid = (gid_t)id;
@@ -267,7 +267,7 @@ next_entry:
if ((colon = strchr(cp = colon, ':')) == NULL)
goto next_entry;
*colon++ = '\0';
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
goto next_entry;
gr.gr_gid = (gid_t)id;

View File

@@ -1210,14 +1210,14 @@ parse_sudoers_options(void)
}
if (MATCHES(*cur, "sudoers_uid=")) {
p = *cur + sizeof("sudoers_uid=") - 1;
id = sudo_strtoid(p, NULL, NULL, &errstr);
id = sudo_strtoid(p, &errstr);
if (errstr == NULL)
sudoers_uid = (uid_t) id;
continue;
}
if (MATCHES(*cur, "sudoers_gid=")) {
p = *cur + sizeof("sudoers_gid=") - 1;
id = sudo_strtoid(p, NULL, NULL, &errstr);
id = sudo_strtoid(p, &errstr);
if (errstr == NULL)
sudoers_gid = (gid_t) id;
continue;

View File

@@ -130,7 +130,7 @@ sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
grp = sysgroup_getgrnam(group);
if (grp == NULL && group[0] == '#' && group[1] != '\0') {
const char *errstr;
gid_t gid = sudo_strtoid(group + 1, NULL, NULL, &errstr);
gid_t gid = sudo_strtoid(group + 1, &errstr);
if (errstr == NULL)
grp = sysgroup_getgrgid(gid);
}

View File

@@ -724,7 +724,7 @@ command_info_to_details(char * const info[], struct command_details *details)
case 'r':
if (strncmp("runas_egid=", info[i], sizeof("runas_egid=") - 1) == 0) {
cp = info[i] + sizeof("runas_egid=") - 1;
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
sudo_fatalx(U_("%s: %s"), info[i], U_(errstr));
details->egid = (gid_t)id;
@@ -733,7 +733,7 @@ command_info_to_details(char * const info[], struct command_details *details)
}
if (strncmp("runas_euid=", info[i], sizeof("runas_euid=") - 1) == 0) {
cp = info[i] + sizeof("runas_euid=") - 1;
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
sudo_fatalx(U_("%s: %s"), info[i], U_(errstr));
details->euid = (uid_t)id;
@@ -742,7 +742,7 @@ command_info_to_details(char * const info[], struct command_details *details)
}
if (strncmp("runas_gid=", info[i], sizeof("runas_gid=") - 1) == 0) {
cp = info[i] + sizeof("runas_gid=") - 1;
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
sudo_fatalx(U_("%s: %s"), info[i], U_(errstr));
details->gid = (gid_t)id;
@@ -759,7 +759,7 @@ command_info_to_details(char * const info[], struct command_details *details)
}
if (strncmp("runas_uid=", info[i], sizeof("runas_uid=") - 1) == 0) {
cp = info[i] + sizeof("runas_uid=") - 1;
id = sudo_strtoid(cp, NULL, NULL, &errstr);
id = sudo_strtoid(cp, &errstr);
if (errstr != NULL)
sudo_fatalx(U_("%s: %s"), info[i], U_(errstr));
details->uid = (uid_t)id;