lib/util_pw.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003 
00004    Safe versions of getpw* calls
00005 
00006    Copyright (C) Andrew Bartlett 2002
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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                         /* what now? */
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                         /* what now? */
00122                 }
00123 #endif
00124                 return NULL;
00125         }
00126 
00127         return tcopy_passwd(mem_ctx, temp);
00128 }

Sambaに対してSat Aug 29 21:23:00 2009に生成されました。  doxygen 1.4.7