r23792: convert Samba4 to GPLv3
[gd/samba-autobuild/.git] / source4 / lib / util / util_pw.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Safe versions of getpw* calls
5
6    Copyright (C) Andrew Bartlett 2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 static struct passwd *alloc_copy_passwd(const struct passwd *from) 
25 {
26         struct passwd *ret = smb_xmalloc_p(struct passwd);
27         ZERO_STRUCTP(ret);
28         ret->pw_name = smb_xstrdup(from->pw_name);
29         ret->pw_passwd = smb_xstrdup(from->pw_passwd);
30         ret->pw_uid = from->pw_uid;
31         ret->pw_gid = from->pw_gid;
32         ret->pw_gecos = smb_xstrdup(from->pw_gecos);
33         ret->pw_dir = smb_xstrdup(from->pw_dir);
34         ret->pw_shell = smb_xstrdup(from->pw_shell);
35         return ret;
36 }
37
38 void passwd_free (struct passwd **buf)
39 {
40         if (!*buf) {
41                 DEBUG(0, ("attempted double-free of allocated passwd\n"));
42                 return;
43         }
44
45         SAFE_FREE((*buf)->pw_name);
46         SAFE_FREE((*buf)->pw_passwd);
47         SAFE_FREE((*buf)->pw_gecos);
48         SAFE_FREE((*buf)->pw_dir);
49         SAFE_FREE((*buf)->pw_shell);
50
51         SAFE_FREE(*buf);
52 }
53
54 struct passwd *getpwnam_alloc(const char *name) 
55 {
56         struct passwd *temp;
57
58         temp = sys_getpwnam(name);
59         
60         if (!temp) {
61 #if 0
62                 if (errno == ENOMEM) {
63                         /* what now? */
64                 }
65 #endif
66                 return NULL;
67         }
68
69         return alloc_copy_passwd(temp);
70 }
71
72 struct passwd *getpwuid_alloc(uid_t uid) 
73 {
74         struct passwd *temp;
75
76         temp = sys_getpwuid(uid);
77         
78         if (!temp) {
79 #if 0
80                 if (errno == ENOMEM) {
81                         /* what now? */
82                 }
83 #endif
84                 return NULL;
85         }
86
87         return alloc_copy_passwd(temp);
88 }