s3:lib: implement serverid_equal() as macro of server_id_equal()
[kai/samba.git] / source3 / lib / username.c
index 4e77bee304b2039f74343dab7cacc65a2b154d1d..665fbb42536c1038a15321d621fa23b311bbdd7e 100644 (file)
@@ -20,7 +20,9 @@
 */
 
 #include "includes.h"
+#include "system/passwd.h"
 #include "memcache.h"
+#include "../lib/util/util_pw.h"
 
 /* internal functions */
 static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
@@ -40,7 +42,7 @@ static struct passwd *getpwnam_alloc_cached(TALLOC_CTX *mem_ctx, const char *nam
                return tcopy_passwd(mem_ctx, pw);
        }
 
-       pw = sys_getpwnam(name);
+       pw = getpwnam(name);
        if (pw == NULL) {
                return NULL;
        }
@@ -56,26 +58,6 @@ static struct passwd *getpwnam_alloc_cached(TALLOC_CTX *mem_ctx, const char *nam
        return tcopy_passwd(mem_ctx, pw);
 }
 
-/****************************************************************************
- talloc copy a struct passwd.
-****************************************************************************/
-
-struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from)
-{
-       struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
-       if (!ret) {
-               return NULL;
-       }
-       ret->pw_name = talloc_strdup(ret, from->pw_name);
-       ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
-       ret->pw_uid = from->pw_uid;
-       ret->pw_gid = from->pw_gid;
-       ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
-       ret->pw_dir = talloc_strdup(ret, from->pw_dir);
-       ret->pw_shell = talloc_strdup(ret, from->pw_shell);
-       return ret;
-}
-
 /****************************************************************************
  Flush all cached passwd structs.
 ****************************************************************************/
@@ -85,21 +67,6 @@ void flush_pwnam_cache(void)
         memcache_flush(NULL, GETPWNAM_CACHE);
 }
 
-/****************************************************************************
- talloc'ed version of getpwuid.
-****************************************************************************/
-
-struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
-{
-       struct passwd *temp = sys_getpwuid(uid);
-
-       if (!temp) {
-               return NULL;
-       }
-
-       return tcopy_passwd(mem_ctx, temp);
-}
-
 /****************************************************************************
  Get a users home directory.
 ****************************************************************************/
@@ -125,7 +92,7 @@ char *get_user_home_dir(TALLOC_CTX *mem_ctx, const char *user)
 }
 
 /****************************************************************************
- * A wrapper for sys_getpwnam().  The following variations are tried:
+ * A wrapper for getpwnam().  The following variations are tried:
  *   - as transmitted
  *   - in all lower case if this differs from transmitted
  *   - in all upper case if this differs from transmitted
@@ -145,7 +112,11 @@ static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
 
        /* Try in all lower case first as this is the most 
           common case on UNIX systems */
-       strlower_m(user2);
+       if (!strlower_m(user2)) {
+               DEBUG(5,("strlower_m %s failed\n", user2));
+               goto done;
+       }
+
        DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
        ret = getpwnam_alloc_cached(mem_ctx, user2);
        if(ret)
@@ -161,7 +132,10 @@ static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
        }
 
        /* Try as uppercase, if username wasn't originally uppercase */
-       strupper_m(user2);
+       if (!strupper_m(user2)) {
+               goto done;
+       }
+
        if(strcmp(user, user2) != 0) {
                DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
                         user2));
@@ -171,7 +145,10 @@ static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
        }
 
        /* Try all combinations up to usernamelevel */
-       strlower_m(user2);
+       if (!strlower_m(user2)) {
+               DEBUG(5,("strlower_m %s failed\n", user2));
+               goto done;
+       }
        DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
                 lp_usernamelevel(), user2));
        ret = uname_string_combinations(user2, mem_ctx, getpwnam_alloc_cached,
@@ -229,9 +206,9 @@ static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx,
 
        for (i=offset;i<(len-(N-1));i++) {
                char c = s[i];
-               if (!islower_ascii((int)c))
+               if (!islower_m((int)c))
                        continue;
-               s[i] = toupper_ascii(c);
+               s[i] = toupper_m(c);
                ret = uname_string_combinations2(s, mem_ctx, i+1, fn, N-1);
                if(ret)
                        return(ret);