Add endpointer and separator args to atoid()

This commit is contained in:
Todd C. Miller
2013-08-08 06:11:52 -06:00
parent 39cbfcd406
commit fe23e7c038
7 changed files with 44 additions and 20 deletions

View File

@@ -32,6 +32,11 @@
#elif defined(HAVE_INTTYPES_H) #elif defined(HAVE_INTTYPES_H)
# include <inttypes.h> # include <inttypes.h>
#endif #endif
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
@@ -42,16 +47,26 @@
#include "sudo_debug.h" #include "sudo_debug.h"
id_t id_t
atoid(const char *p, const char **errstr) atoid(const char *p, const char *sep, char **endp, const char **errstr)
{ {
char *ep; char *ep;
id_t rval = 0; id_t rval = 0;
bool valid = false;
debug_decl(atoid, SUDO_DEBUG_UTIL) debug_decl(atoid, SUDO_DEBUG_UTIL)
if (sep == NULL)
sep = "";
errno = 0; errno = 0;
if (*p == '-') { if (*p == '-') {
long lval = strtol(p, &ep, 10); long lval = strtol(p, &ep, 10);
if (ep == p || *ep != '\0') { if (ep != p) {
/* check for valid separator (including '\0') */
do {
if (*ep == *sep)
valid = true;
} while (*sep++ != '\0');
}
if (!valid) {
*errstr = N_("invalid value"); *errstr = N_("invalid value");
errno = EINVAL; errno = EINVAL;
goto done; goto done;
@@ -66,7 +81,14 @@ atoid(const char *p, const char **errstr)
*errstr = NULL; *errstr = NULL;
} else { } else {
unsigned long ulval = strtoul(p, &ep, 10); unsigned long ulval = strtoul(p, &ep, 10);
if (ep == p || *ep != '\0') { if (ep != p) {
/* check for valid separator (including '\0') */
do {
if (*ep == *sep)
valid = true;
} while (*sep++ != '\0');
}
if (!valid) {
*errstr = N_("invalid value"); *errstr = N_("invalid value");
errno = EINVAL; errno = EINVAL;
goto done; goto done;
@@ -79,6 +101,8 @@ atoid(const char *p, const char **errstr)
rval = (id_t)ulval; rval = (id_t)ulval;
*errstr = NULL; *errstr = NULL;
} }
if (endp != NULL)
*endp = ep;
done: done:
debug_return_int(rval); debug_return_int(rval);
} }

View File

@@ -471,7 +471,7 @@ iolog_deserialize_info(struct iolog_details *details, char * const user_info[],
if (runas_euid_str != NULL) if (runas_euid_str != NULL)
runas_uid_str = runas_euid_str; runas_uid_str = runas_euid_str;
if (runas_uid_str != NULL) { if (runas_uid_str != NULL) {
id = atoid(runas_uid_str, &errstr); id = atoid(runas_uid_str, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
warningx("runas uid %s: %s", runas_uid_str, _(errstr)); warningx("runas uid %s: %s", runas_uid_str, _(errstr));
else else
@@ -480,7 +480,7 @@ iolog_deserialize_info(struct iolog_details *details, char * const user_info[],
if (runas_egid_str != NULL) if (runas_egid_str != NULL)
runas_gid_str = runas_egid_str; runas_gid_str = runas_egid_str;
if (runas_gid_str != NULL) { if (runas_gid_str != NULL) {
id = atoid(runas_gid_str, &errstr); id = atoid(runas_gid_str, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
warningx("runas gid %s: %s", runas_gid_str, _(errstr)); warningx("runas gid %s: %s", runas_gid_str, _(errstr));
else else

View File

@@ -103,14 +103,14 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
} }
if (MATCHES(*cur, "sudoers_uid=")) { if (MATCHES(*cur, "sudoers_uid=")) {
p = *cur + sizeof("sudoers_uid=") - 1; p = *cur + sizeof("sudoers_uid=") - 1;
sudoers_uid = (uid_t) atoid(p, &errstr); sudoers_uid = (uid_t) atoid(p, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
fatalx("%s: %s", *cur, _(errstr)); fatalx("%s: %s", *cur, _(errstr));
continue; continue;
} }
if (MATCHES(*cur, "sudoers_gid=")) { if (MATCHES(*cur, "sudoers_gid=")) {
p = *cur + sizeof("sudoers_gid=") - 1; p = *cur + sizeof("sudoers_gid=") - 1;
sudoers_gid = (gid_t) atoid(p, &errstr); sudoers_gid = (gid_t) atoid(p, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
fatalx("%s: %s", *cur, _(errstr)); fatalx("%s: %s", *cur, _(errstr));
continue; continue;
@@ -261,14 +261,14 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
} }
if (MATCHES(*cur, "uid=")) { if (MATCHES(*cur, "uid=")) {
p = *cur + sizeof("uid=") - 1; p = *cur + sizeof("uid=") - 1;
user_uid = (uid_t) atoid(p, &errstr); user_uid = (uid_t) atoid(p, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
fatalx("%s: %s", *cur, _(errstr)); fatalx("%s: %s", *cur, _(errstr));
continue; continue;
} }
if (MATCHES(*cur, "gid=")) { if (MATCHES(*cur, "gid=")) {
p = *cur + sizeof("gid=") - 1; p = *cur + sizeof("gid=") - 1;
user_gid = (gid_t) atoid(p, &errstr); user_gid = (gid_t) atoid(p, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
fatalx("%s: %s", *cur, _(errstr)); fatalx("%s: %s", *cur, _(errstr));
continue; continue;
@@ -303,7 +303,7 @@ sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
} }
if (MATCHES(*cur, "sid=")) { if (MATCHES(*cur, "sid=")) {
p = *cur + sizeof("sid=") - 1; p = *cur + sizeof("sid=") - 1;
sudo_user.sid = (pid_t) atoid(p, &errstr); sudo_user.sid = (pid_t) atoid(p, NULL, NULL, &errstr);
if (errstr != NULL) if (errstr != NULL)
fatalx("%s: %s", *cur, _(errstr)); fatalx("%s: %s", *cur, _(errstr));
continue; continue;

View File

@@ -315,7 +315,7 @@ char *get_timestr(time_t, int);
int atobool(const char *str); int atobool(const char *str);
/* atoid.c */ /* atoid.c */
int atoid(const char *str, const char **errstr); int atoid(const char *str, const char *sep, char **endp, const char **errstr);
/* boottime.c */ /* boottime.c */
int get_boottime(struct timeval *); int get_boottime(struct timeval *);

View File

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

View File

@@ -611,7 +611,7 @@ command_info_to_details(char * const info[], struct command_details *details)
case 'r': case 'r':
if (strncmp("runas_egid=", info[i], sizeof("runas_egid=") - 1) == 0) { if (strncmp("runas_egid=", info[i], sizeof("runas_egid=") - 1) == 0) {
cp = info[i] + sizeof("runas_egid=") - 1; cp = info[i] + sizeof("runas_egid=") - 1;
id = atoid(cp, &errstr); id = atoid(cp, NULL, NULL, &errstr);
if (errstr != NULL) { if (errstr != NULL) {
warningx(_("%s: %s"), info[i], _(errstr)); warningx(_("%s: %s"), info[i], _(errstr));
} else { } else {
@@ -622,7 +622,7 @@ command_info_to_details(char * const info[], struct command_details *details)
} }
if (strncmp("runas_euid=", info[i], sizeof("runas_euid=") - 1) == 0) { if (strncmp("runas_euid=", info[i], sizeof("runas_euid=") - 1) == 0) {
cp = info[i] + sizeof("runas_euid=") - 1; cp = info[i] + sizeof("runas_euid=") - 1;
id = atoid(cp, &errstr); id = atoid(cp, NULL, NULL, &errstr);
if (errstr != NULL) { if (errstr != NULL) {
warningx(_("%s: %s"), info[i], _(errstr)); warningx(_("%s: %s"), info[i], _(errstr));
} else { } else {
@@ -633,7 +633,7 @@ command_info_to_details(char * const info[], struct command_details *details)
} }
if (strncmp("runas_gid=", info[i], sizeof("runas_gid=") - 1) == 0) { if (strncmp("runas_gid=", info[i], sizeof("runas_gid=") - 1) == 0) {
cp = info[i] + sizeof("runas_gid=") - 1; cp = info[i] + sizeof("runas_gid=") - 1;
id = atoid(cp, &errstr); id = atoid(cp, NULL, NULL, &errstr);
if (errstr != NULL) { if (errstr != NULL) {
warningx(_("%s: %s"), info[i], _(errstr)); warningx(_("%s: %s"), info[i], _(errstr));
} else { } else {
@@ -660,7 +660,7 @@ command_info_to_details(char * const info[], struct command_details *details)
emalloc2(details->ngroups, sizeof(GETGROUPS_T)); emalloc2(details->ngroups, sizeof(GETGROUPS_T));
cp = info[i] + sizeof("runas_groups=") - 1; cp = info[i] + sizeof("runas_groups=") - 1;
for (j = 0; j < details->ngroups;) { for (j = 0; j < details->ngroups;) {
id = atoid(cp, &errstr); id = atoid(cp, ",", &ep, &errstr);
if (errstr != NULL) { if (errstr != NULL) {
warningx(_("%s: %s"), cp, _(errstr)); warningx(_("%s: %s"), cp, _(errstr));
break; break;
@@ -674,7 +674,7 @@ command_info_to_details(char * const info[], struct command_details *details)
} }
if (strncmp("runas_uid=", info[i], sizeof("runas_uid=") - 1) == 0) { if (strncmp("runas_uid=", info[i], sizeof("runas_uid=") - 1) == 0) {
cp = info[i] + sizeof("runas_uid=") - 1; cp = info[i] + sizeof("runas_uid=") - 1;
id = atoid(cp, &errstr); id = atoid(cp, NULL, NULL, &errstr);
if (errstr != NULL) { if (errstr != NULL) {
warningx(_("%s: %s"), info[i], _(errstr)); warningx(_("%s: %s"), info[i], _(errstr));
} else { } else {

View File

@@ -188,7 +188,7 @@ char *fmt_string(const char *var, const char *value);
bool atobool(const char *str); bool atobool(const char *str);
/* atoid.c */ /* atoid.c */
id_t atoid(const char *str, const char **errstr); id_t atoid(const char *str, const char *sep, char **endp, const char **errstr);
/* parse_args.c */ /* parse_args.c */
int parse_args(int argc, char **argv, int *nargc, char ***nargv, int parse_args(int argc, char **argv, int *nargc, char ***nargv,