00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024
00025 struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from)
00026 {
00027 struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
00028 if (!ret) {
00029 return NULL;
00030 }
00031 ret->pw_name = talloc_strdup(ret, from->pw_name);
00032 ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
00033 ret->pw_uid = from->pw_uid;
00034 ret->pw_gid = from->pw_gid;
00035 ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
00036 ret->pw_dir = talloc_strdup(ret, from->pw_dir);
00037 ret->pw_shell = talloc_strdup(ret, from->pw_shell);
00038 return ret;
00039 }
00040
00041 #define PWNAMCACHE_SIZE 4
00042 static struct passwd **pwnam_cache = NULL;
00043
00044 static void init_pwnam_cache(void)
00045 {
00046 if (pwnam_cache != NULL)
00047 return;
00048
00049 pwnam_cache = TALLOC_ZERO_ARRAY(NULL, struct passwd *,
00050 PWNAMCACHE_SIZE);
00051 if (pwnam_cache == NULL) {
00052 smb_panic("Could not init pwnam_cache\n");
00053 }
00054
00055 return;
00056 }
00057
00058 void flush_pwnam_cache(void)
00059 {
00060 TALLOC_FREE(pwnam_cache);
00061 pwnam_cache = NULL;
00062 init_pwnam_cache();
00063 }
00064
00065 struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
00066 {
00067 int i;
00068
00069 struct passwd *temp;
00070
00071 init_pwnam_cache();
00072
00073 for (i=0; i<PWNAMCACHE_SIZE; i++) {
00074 if ((pwnam_cache[i] != NULL) &&
00075 (strcmp(name, pwnam_cache[i]->pw_name) == 0)) {
00076 DEBUG(10, ("Got %s from pwnam_cache\n", name));
00077 return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]);
00078 }
00079 }
00080
00081 temp = sys_getpwnam(name);
00082
00083 if (!temp) {
00084 #if 0
00085 if (errno == ENOMEM) {
00086
00087 }
00088 #endif
00089 return NULL;
00090 }
00091
00092 for (i=0; i<PWNAMCACHE_SIZE; i++) {
00093 if (pwnam_cache[i] == NULL)
00094 break;
00095 }
00096
00097 if (i == PWNAMCACHE_SIZE)
00098 i = rand() % PWNAMCACHE_SIZE;
00099
00100 if (pwnam_cache[i] != NULL) {
00101 TALLOC_FREE(pwnam_cache[i]);
00102 }
00103
00104 pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp);
00105 if (pwnam_cache[i]!= NULL && mem_ctx != NULL) {
00106 return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]);
00107 }
00108
00109 return tcopy_passwd(NULL, pwnam_cache[i]);
00110 }
00111
00112 struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
00113 {
00114 struct passwd *temp;
00115
00116 temp = sys_getpwuid(uid);
00117
00118 if (!temp) {
00119 #if 0
00120 if (errno == ENOMEM) {
00121
00122 }
00123 #endif
00124 return NULL;
00125 }
00126
00127 return tcopy_passwd(mem_ctx, temp);
00128 }