From 2dd2eb3835a3ad72ac34bb16ac1c541c9eaf12ce Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Fri, 15 Oct 2010 15:57:10 +0200
Subject: [PATCH] Use unsigned long for conversion to id_t

We used strtol() on a number of places to convert into uid_t or gid_t
from a string representation such as LDAP attribute, but on some
platforms, unsigned long might be necessary to store big id_t values.

This patch converts to using strtoul() instead.
---
 src/providers/ldap/ldap_id.c             |    4 +-
 src/providers/ldap/sdap_async_accounts.c |   46 +++++++++--------------------
 src/providers/proxy/proxy_id.c           |    4 +-
 src/util/find_uid.c                      |    8 ++--
 4 files changed, 22 insertions(+), 40 deletions(-)

diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
index be4642a..135e370 100644
--- a/src/providers/ldap/ldap_id.c
+++ b/src/providers/ldap/ldap_id.c
@@ -220,7 +220,7 @@ static void users_get_done(struct tevent_req *subreq)
 
         case BE_FILTER_IDNUM:
             errno = 0;
-            uid = (uid_t)strtol(state->name, &endptr, 0);
+            uid = (uid_t) strtoul(state->name, &endptr, 0);
             if (errno || *endptr || (state->name == endptr)) {
                 tevent_req_error(req, errno);
                 return;
@@ -446,7 +446,7 @@ static void groups_get_done(struct tevent_req *subreq)
 
         case BE_FILTER_IDNUM:
             errno = 0;
-            gid = (gid_t)strtol(state->name, &endptr, 0);
+            gid = (gid_t) strtoul(state->name, &endptr, 0);
             if (errno || *endptr || (state->name == endptr)) {
                 tevent_req_error(req, errno);
                 return;
diff --git a/src/providers/ldap/sdap_async_accounts.c b/src/providers/ldap/sdap_async_accounts.c
index 96de973..8547dae 100644
--- a/src/providers/ldap/sdap_async_accounts.c
+++ b/src/providers/ldap/sdap_async_accounts.c
@@ -43,7 +43,7 @@ static int sdap_save_user(TALLOC_CTX *memctx,
     const char *gecos;
     const char *homedir;
     const char *shell;
-    long int l;
+    unsigned long l;
     uid_t uid;
     gid_t gid;
     struct sysdb_attrs *user_attrs;
@@ -90,21 +90,15 @@ static int sdap_save_user(TALLOC_CTX *memctx,
     if (el->num_values == 0) shell = NULL;
     else shell = (const char *)el->values[0].data;
 
-    ret = sysdb_attrs_get_el(attrs,
-                             opts->user_map[SDAP_AT_USER_UID].sys_name, &el);
-    if (ret) goto fail;
-    if (el->num_values == 0) {
+    ret = sysdb_attrs_get_ulong(attrs,
+                                opts->user_map[SDAP_AT_USER_UID].sys_name,
+                                &l);
+    if (ret != EOK) {
         DEBUG(1, ("no uid provided for [%s] in domain [%s].\n",
                   name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    errno = 0;
-    l = strtol((const char *)el->values[0].data, NULL, 0);
-    if (errno) {
-        ret = EINVAL;
-        goto fail;
-    }
     uid = l;
 
     /* check that the uid is valid for this domain */
@@ -115,21 +109,15 @@ static int sdap_save_user(TALLOC_CTX *memctx,
         goto fail;
     }
 
-    ret = sysdb_attrs_get_el(attrs,
-                             opts->user_map[SDAP_AT_USER_GID].sys_name, &el);
-    if (ret) goto fail;
-    if (el->num_values == 0) {
+    ret = sysdb_attrs_get_ulong(attrs,
+                                opts->user_map[SDAP_AT_USER_GID].sys_name,
+                                &l);
+    if (ret != EOK) {
         DEBUG(1, ("no gid provided for [%s] in domain [%s].\n",
                   name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    errno = 0;
-    l = strtol((const char *)el->values[0].data, NULL, 0);
-    if (errno) {
-        ret = EINVAL;
-        goto fail;
-    }
     gid = l;
 
     /* check that the gid is valid for this domain */
@@ -621,7 +609,7 @@ static int sdap_save_group(TALLOC_CTX *memctx,
     struct ldb_message_element *el;
     struct sysdb_attrs *group_attrs;
     const char *name = NULL;
-    long int l;
+    unsigned long l;
     gid_t gid;
     int ret;
     char *timestamp = NULL;
@@ -635,21 +623,15 @@ static int sdap_save_group(TALLOC_CTX *memctx,
     }
     name = (const char *)el->values[0].data;
 
-    ret = sysdb_attrs_get_el(attrs,
-                          opts->group_map[SDAP_AT_GROUP_GID].sys_name, &el);
-    if (ret) goto fail;
-    if (el->num_values == 0) {
+    ret = sysdb_attrs_get_ulong(attrs,
+                                opts->group_map[SDAP_AT_GROUP_GID].sys_name,
+                                &l);
+    if (ret != EOK) {
         DEBUG(1, ("no gid provided for [%s] in domain [%s].\n",
                   name, dom->name));
         ret = EINVAL;
         goto fail;
     }
-    errno = 0;
-    l = strtol((const char *)el->values[0].data, NULL, 0);
-    if (errno) {
-        ret = EINVAL;
-        goto fail;
-    }
     gid = l;
 
     /* check that the gid is valid for this domain */
diff --git a/src/providers/proxy/proxy_id.c b/src/providers/proxy/proxy_id.c
index ab2be35..3fe58f3 100644
--- a/src/providers/proxy/proxy_id.c
+++ b/src/providers/proxy/proxy_id.c
@@ -1078,7 +1078,7 @@ void proxy_get_account_info(struct be_req *breq)
             } else {
                 char *endptr;
                 errno = 0;
-                uid = (uid_t)strtol(ar->filter_value, &endptr, 0);
+                uid = (uid_t) strtoul(ar->filter_value, &endptr, 0);
                 if (errno || *endptr || (ar->filter_value == endptr)) {
                     return proxy_reply(breq, DP_ERR_FATAL,
                                        EINVAL, "Invalid attr type");
@@ -1108,7 +1108,7 @@ void proxy_get_account_info(struct be_req *breq)
             } else {
                 char *endptr;
                 errno = 0;
-                gid = (gid_t)strtol(ar->filter_value, &endptr, 0);
+                gid = (gid_t) strtoul(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 02d6686..0937c66 100644
--- a/src/util/find_uid.c
+++ b/src/util/find_uid.c
@@ -63,7 +63,7 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
     char *p;
     char *e;
     char *endptr;
-    long num=0;
+    unsigned long num=0;
     errno_t error;
 
     ret = snprintf(path, PATHLEN, "/proc/%d/status", pid);
@@ -132,9 +132,9 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
             *e = '\0';
         }
         errno = 0;
-        num = strtol(p, &endptr, 10);
+        num = strtoul(p, &endptr, 10);
         error = errno;
-        if (error == ERANGE) {
+        if (error != 0) {
             DEBUG(1, ("strtol failed [%s].\n", strerror(error)));
             return error;
         }
@@ -143,7 +143,7 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
             return EINVAL;
         }
 
-        if (num < 0 || num >= INT_MAX) {
+        if (num >= UINT32_MAX) {
             DEBUG(1, ("uid out of range.\n"));
             return ERANGE;
         }
-- 
1.7.2.3

