3367a6cdc05dfea3a8788104c8fa5a39d38963eb
[vlendec/samba-autobuild/.git] / source3 / lib / util_pw.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0.
4
5    Safe versions of getpw* calls
6
7    Copyright (C) Andrew Bartlett 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 struct passwd *make_modifyable_passwd(const struct passwd *from)
27 {
28         struct passwd *ret = smb_xmalloc(sizeof(*ret));
29 /*  This is the assumed shape of the members by certain parts of the code...
30         fstring         pw_name;
31         fstring         pw_passwd;
32         fstring         pw_gecos;
33         pstring         pw_dir;
34         pstring         pw_shell;
35 */
36         char *pw_name = smb_xmalloc(sizeof(fstring));
37         char *pw_passwd = smb_xmalloc(sizeof(fstring));
38         char *pw_gecos = smb_xmalloc(sizeof(fstring));
39         char *pw_dir = smb_xmalloc(sizeof(pstring));
40         char *pw_shell = smb_xmalloc(sizeof(pstring));
41
42         ZERO_STRUCTP(ret);
43
44         /* 
45          * Now point the struct's members as the 
46          * newly allocated buffers:
47          */
48
49         ret->pw_name = pw_name;
50         fstrcpy(ret->pw_name, from->pw_name);
51
52         ret->pw_passwd = pw_passwd;
53         fstrcpy(ret->pw_passwd, from->pw_passwd);
54
55         ret->pw_uid = from->pw_uid;
56         ret->pw_gid = from->pw_gid;
57
58         ret->pw_gecos = pw_gecos;
59         fstrcpy(ret->pw_gecos, from->pw_gecos);
60
61         ret->pw_dir = pw_dir;
62         pstrcpy(ret->pw_dir, from->pw_dir);
63
64         ret->pw_shell = pw_shell;
65         pstrcpy(ret->pw_shell, from->pw_shell);
66
67         return ret;
68 }
69
70 static struct passwd *alloc_copy_passwd(const struct passwd *from) 
71 {
72         struct passwd *ret = smb_xmalloc(sizeof(*ret));
73         ZERO_STRUCTP(ret);
74         ret->pw_name = smb_xstrdup(from->pw_name);
75         ret->pw_passwd = smb_xstrdup(from->pw_passwd);
76         ret->pw_uid = from->pw_uid;
77         ret->pw_gid = from->pw_gid;
78         ret->pw_gecos = smb_xstrdup(from->pw_gecos);
79         ret->pw_dir = smb_xstrdup(from->pw_dir);
80         ret->pw_shell = smb_xstrdup(from->pw_shell);
81         return ret;
82 }
83
84 void passwd_free (struct passwd **buf)
85 {
86         if (!*buf) {
87                 DEBUG(0, ("attempted double-free of allocated passwd\n"));
88                 return;
89         }
90
91         SAFE_FREE((*buf)->pw_name);
92         SAFE_FREE((*buf)->pw_passwd);
93         SAFE_FREE((*buf)->pw_gecos);
94         SAFE_FREE((*buf)->pw_dir);
95         SAFE_FREE((*buf)->pw_shell);
96
97         SAFE_FREE(*buf);
98 }
99
100 struct passwd *getpwnam_alloc(const char *name) 
101 {
102         struct passwd *temp;
103
104         temp = getpwnam(name);
105         
106         if (!temp) {
107 #if 0
108                 if (errno == ENOMEM) {
109                         /* what now? */
110                 }
111 #endif
112                 return NULL;
113         }
114
115         return alloc_copy_passwd(temp);
116 }
117
118 struct passwd *getpwuid_alloc(uid_t uid) 
119 {
120         struct passwd *temp;
121
122         temp = getpwuid(uid);
123         
124         if (!temp) {
125 #if 0
126                 if (errno == ENOMEM) {
127                         /* what now? */
128                 }
129 #endif
130                 return NULL;
131         }
132
133         return alloc_copy_passwd(temp);
134 }