From 2ce613a21a2cf8ce6b040e025c82d2b996f07843 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Mon, 25 Oct 2010 14:57:35 +0200
Subject: [PATCH] Always use uint32_t for UID/GID numbers

---
 src/db/sysdb.c                           |   15 +++++-----
 src/db/sysdb.h                           |    4 +-
 src/providers/ldap/ldap_id.c             |   11 +++----
 src/providers/ldap/sdap_async_accounts.c |   43 +++++++++---------------------
 src/providers/proxy/proxy.c              |    7 ++---
 src/util/find_uid.c                      |    5 ++-
 src/util/strtonum.c                      |    2 +-
 7 files changed, 34 insertions(+), 53 deletions(-)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index 416efba..6488c22 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -20,6 +20,7 @@
 */
 
 #include "util/util.h"
+#include "util/strtonum.h"
 #include "db/sysdb_private.h"
 #include "confdb/confdb.h"
 #include <time.h>
@@ -170,13 +171,13 @@ int sysdb_attrs_get_string(struct sysdb_attrs *attrs, const char *name,
     return EOK;
 }
 
-int sysdb_attrs_get_ulong(struct sysdb_attrs *attrs, const char *name,
-                          unsigned long *value)
+int sysdb_attrs_get_uint32_t(struct sysdb_attrs *attrs, const char *name,
+                             uint32_t *value)
 {
     struct ldb_message_element *el;
     int ret;
     char *endptr;
-    unsigned long val;
+    uint32_t val;
 
     ret = sysdb_attrs_get_el_int(attrs, name, false, &el);
     if (ret) {
@@ -187,11 +188,9 @@ int sysdb_attrs_get_ulong(struct sysdb_attrs *attrs, const char *name,
         return ERANGE;
     }
 
-    errno = 0;
-    val = strtoul((const char *) el->values[0].data, &endptr, 0);
-    if (errno || *endptr) {
-        return EINVAL;
-    }
+    val = strtouint32((const char *) el->values[0].data, &endptr, 0);
+    if (errno != 0) return errno;
+    if (*endptr) return EINVAL;
 
     *value = val;
     return EOK;
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 1b9edd0..9a3bcd1 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -178,8 +178,8 @@ int sysdb_attrs_get_string(struct sysdb_attrs *attrs, const char *name,
                            const char **string);
 int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
                                  TALLOC_CTX *mem_ctx, const char ***string);
-int sysdb_attrs_get_ulong(struct sysdb_attrs *attrs, const char *name,
-                          unsigned long *value);
+int sysdb_attrs_get_uint32_t(struct sysdb_attrs *attrs, const char *name,
+                             uint32_t *value);
 
 int sysdb_attrs_replace_name(struct sysdb_attrs *attrs, const char *oldname,
                                  const char *newname);
diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
index 5a9f575..5cf593d 100644
--- a/src/providers/ldap/ldap_id.c
+++ b/src/providers/ldap/ldap_id.c
@@ -27,6 +27,7 @@
 #include <sys/time.h>
 
 #include "util/util.h"
+#include "util/strtonum.h"
 #include "db/sysdb.h"
 #include "providers/ldap/ldap_common.h"
 #include "providers/ldap/sdap_async.h"
@@ -200,10 +201,9 @@ static void users_get_done(struct tevent_req *subreq)
             return;
 
         case BE_FILTER_IDNUM:
-            errno = 0;
-            uid = (uid_t) strtoul(state->name, &endptr, 0);
+            uid = (uid_t) strtouint32(state->name, &endptr, 0);
             if (errno || *endptr || (state->name == endptr)) {
-                tevent_req_error(req, errno);
+                tevent_req_error(req, errno ? errno : EINVAL);
                 return;
             }
 
@@ -421,10 +421,9 @@ static void groups_get_done(struct tevent_req *subreq)
             return;
 
         case BE_FILTER_IDNUM:
-            errno = 0;
-            gid = (gid_t) strtoul(state->name, &endptr, 0);
+            gid = (gid_t) strtouint32(state->name, &endptr, 0);
             if (errno || *endptr || (state->name == endptr)) {
-                tevent_req_error(req, errno);
+                tevent_req_error(req, errno ? errno : EINVAL);
                 return;
             }
 
diff --git a/src/providers/ldap/sdap_async_accounts.c b/src/providers/ldap/sdap_async_accounts.c
index 0244d93..9a950d7 100644
--- a/src/providers/ldap/sdap_async_accounts.c
+++ b/src/providers/ldap/sdap_async_accounts.c
@@ -57,7 +57,6 @@ static struct tevent_req *sdap_save_user_send(TALLOC_CTX *memctx,
     const char *gecos;
     const char *homedir;
     const char *shell;
-    unsigned long l;
     uid_t uid;
     gid_t gid;
     struct sysdb_attrs *user_attrs;
@@ -111,16 +110,15 @@ static struct tevent_req *sdap_save_user_send(TALLOC_CTX *memctx,
     if (el->num_values == 0) shell = NULL;
     else shell = (const char *)el->values[0].data;
 
-    ret = sysdb_attrs_get_ulong(attrs,
-                                opts->user_map[SDAP_AT_USER_UID].sys_name,
-                                &l);
+    ret = sysdb_attrs_get_uint32_t(attrs,
+                                   opts->user_map[SDAP_AT_USER_UID].sys_name,
+                                   &uid);
     if (ret != EOK) {
         DEBUG(1, ("no uid provided for [%s] in domain [%s].\n",
                   state->name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    uid = l;
 
     /* check that the uid is valid for this domain */
     if (OUT_OF_ID_RANGE(uid, dom->id_min, dom->id_max)) {
@@ -130,16 +128,15 @@ static struct tevent_req *sdap_save_user_send(TALLOC_CTX *memctx,
         goto fail;
     }
 
-    ret = sysdb_attrs_get_ulong(attrs,
-                                opts->user_map[SDAP_AT_USER_GID].sys_name,
-                                &l);
+    ret = sysdb_attrs_get_uint32_t(attrs,
+                                   opts->user_map[SDAP_AT_USER_GID].sys_name,
+                                   &gid);
     if (ret != EOK) {
         DEBUG(1, ("no gid provided for [%s] in domain [%s].\n",
                   state->name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    gid = l;
 
     /* check that the gid is valid for this domain */
     if (OUT_OF_ID_RANGE(gid, dom->id_min, dom->id_max)) {
@@ -818,7 +815,6 @@ static struct tevent_req *sdap_save_group_send(TALLOC_CTX *memctx,
     struct sdap_save_group_state *state;
     struct ldb_message_element *el;
     struct sysdb_attrs *group_attrs;
-    unsigned long l;
     gid_t gid;
     int ret;
 
@@ -840,16 +836,15 @@ static struct tevent_req *sdap_save_group_send(TALLOC_CTX *memctx,
     }
     state->name = (const char *)el->values[0].data;
 
-    ret = sysdb_attrs_get_ulong(attrs,
-                                opts->group_map[SDAP_AT_GROUP_GID].sys_name,
-                                &l);
+    ret = sysdb_attrs_get_uint32_t(attrs,
+                                   opts->group_map[SDAP_AT_GROUP_GID].sys_name,
+                                   &gid);
     if (ret != EOK) {
         DEBUG(1, ("no gid provided for [%s] in domain [%s].\n",
                   state->name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    gid = l;
 
     /* check that the gid is valid for this domain */
     if (OUT_OF_ID_RANGE(gid, dom->id_min, dom->id_max)) {
@@ -2359,26 +2354,14 @@ static void sdap_add_incomplete_groups_next(struct tevent_req *subreq)
             name = (const char *)el->values[0].data;
 
             if (strcmp(name, state->groupnames[state->cur]) == 0) {
-                ret = sysdb_attrs_get_el(state->ldap_groups[ai],
-                                        SYSDB_GIDNUM,
-                                        &el);
+                ret = sysdb_attrs_get_uint32_t(state->ldap_groups[ai],
+                                               SYSDB_GIDNUM,
+                                               &gid);
                 if (ret) {
+                    DEBUG(1, ("no gid provided for [%s]\n", name));
                     tevent_req_error(req, ret);
                     return;
                 }
-                if (el->num_values == 0) {
-                    DEBUG(1, ("no gid provided for [%s]\n",
-                                name));
-                    tevent_req_error(req, EINVAL);
-                    return;
-                }
-
-                errno = 0;
-                gid = (gid_t) strtol((const char *)el->values[0].data, NULL, 0);
-                if (errno) {
-                    tevent_req_error(req, errno);
-                    return;
-                }
 
                 add_req = sysdb_add_incomplete_group_send(state,
                                                     state->ev,
diff --git a/src/providers/proxy/proxy.c b/src/providers/proxy/proxy.c
index 144ab45..b501b58 100644
--- a/src/providers/proxy/proxy.c
+++ b/src/providers/proxy/proxy.c
@@ -31,6 +31,7 @@
 #include <security/pam_modules.h>
 
 #include "util/util.h"
+#include "util/strtonum.h"
 #include "providers/dp_backend.h"
 #include "db/sysdb.h"
 #include "proxy.h"
@@ -2822,8 +2823,7 @@ static void proxy_get_account_info(struct be_req *breq)
                                    EINVAL, "Invalid attr type");
             } else {
                 char *endptr;
-                errno = 0;
-                uid = (uid_t) strtoul(ar->filter_value, &endptr, 0);
+                uid = (uid_t) strtouint32(ar->filter_value, &endptr, 0);
                 if (errno || *endptr || (ar->filter_value == endptr)) {
                     return proxy_reply(breq, DP_ERR_FATAL,
                                        EINVAL, "Invalid attr type");
@@ -2877,8 +2877,7 @@ static void proxy_get_account_info(struct be_req *breq)
                                    EINVAL, "Invalid attr type");
             } else {
                 char *endptr;
-                errno = 0;
-                gid = (gid_t) strtoul(ar->filter_value, &endptr, 0);
+                gid = (gid_t) strtouint32(ar->filter_value, &endptr, 0);
                 if (errno || *endptr || (ar->filter_value == endptr)) {
                     return proxy_reply(breq, DP_ERR_FATAL,
                                        EINVAL, "Invalid attr type");
diff --git a/src/util/find_uid.c b/src/util/find_uid.c
index bd62b12..e18de7a 100644
--- a/src/util/find_uid.c
+++ b/src/util/find_uid.c
@@ -38,6 +38,7 @@
 
 #include "dhash.h"
 #include "util/util.h"
+#include "util/strtonum.h"
 
 #define INITIAL_TABLE_SIZE 64
 #define PATHLEN (NAME_MAX + 14)
@@ -63,7 +64,7 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
     char *p;
     char *e;
     char *endptr;
-    long num=0;
+    uint32_t num=0;
     errno_t error;
 
     ret = snprintf(path, PATHLEN, "/proc/%d/status", pid);
@@ -132,7 +133,7 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
             *e = '\0';
         }
         errno = 0;
-        num = strtoul(p, &endptr, 10);
+        num = strtouint32(p, &endptr, 10);
         error = errno;
         if (error != 0) {
             DEBUG(1, ("strtoul failed [%s].\n", strerror(error)));
diff --git a/src/util/strtonum.c b/src/util/strtonum.c
index 744e0f7..bf1b21a 100644
--- a/src/util/strtonum.c
+++ b/src/util/strtonum.c
@@ -51,7 +51,7 @@ int32_t strtoint32(const char *nptr, char **endptr, int base)
 /* strtouint32 */
 uint32_t strtouint32(const char *nptr, char **endptr, int base)
 {
-    long long ret = 0;
+    unsigned long long ret = 0;
     errno = 0;
     ret = strtoull(nptr, endptr, base);
 
-- 
1.7.2.3

