preparing for release of 3.0-alpha17
[Samba/ekacnet.git] / source / lib / util_pw.c
blob67ed43f216d4853b0e3f452d5f532171669b5a06
1 /*
2 Unix SMB/CIFS implementation.
4 Safe versions of getpw* calls
6 Copyright (C) Andrew Bartlett 2002
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 struct passwd *make_modifyable_passwd(const struct passwd *from)
27 struct passwd *ret = smb_xmalloc(sizeof(*ret));
28 /* This is the assumed shape of the members by certain parts of the code...
29 fstring pw_name;
30 fstring pw_passwd;
31 fstring pw_gecos;
32 pstring pw_dir;
33 pstring pw_shell;
35 char *pw_name = smb_xmalloc(sizeof(fstring));
36 char *pw_passwd = smb_xmalloc(sizeof(fstring));
37 char *pw_gecos = smb_xmalloc(sizeof(fstring));
38 char *pw_dir = smb_xmalloc(sizeof(pstring));
39 char *pw_shell = smb_xmalloc(sizeof(pstring));
41 ZERO_STRUCTP(ret);
43 /*
44 * Now point the struct's members as the
45 * newly allocated buffers:
48 ret->pw_name = pw_name;
49 fstrcpy(ret->pw_name, from->pw_name);
51 ret->pw_passwd = pw_passwd;
52 fstrcpy(ret->pw_passwd, from->pw_passwd);
54 ret->pw_uid = from->pw_uid;
55 ret->pw_gid = from->pw_gid;
57 ret->pw_gecos = pw_gecos;
58 fstrcpy(ret->pw_gecos, from->pw_gecos);
60 ret->pw_dir = pw_dir;
61 pstrcpy(ret->pw_dir, from->pw_dir);
63 ret->pw_shell = pw_shell;
64 pstrcpy(ret->pw_shell, from->pw_shell);
66 return ret;
69 static struct passwd *alloc_copy_passwd(const struct passwd *from)
71 struct passwd *ret = smb_xmalloc(sizeof(*ret));
72 ZERO_STRUCTP(ret);
73 ret->pw_name = smb_xstrdup(from->pw_name);
74 ret->pw_passwd = smb_xstrdup(from->pw_passwd);
75 ret->pw_uid = from->pw_uid;
76 ret->pw_gid = from->pw_gid;
77 ret->pw_gecos = smb_xstrdup(from->pw_gecos);
78 ret->pw_dir = smb_xstrdup(from->pw_dir);
79 ret->pw_shell = smb_xstrdup(from->pw_shell);
80 return ret;
83 void passwd_free (struct passwd **buf)
85 if (!*buf) {
86 DEBUG(0, ("attempted double-free of allocated passwd\n"));
87 return;
90 SAFE_FREE((*buf)->pw_name);
91 SAFE_FREE((*buf)->pw_passwd);
92 SAFE_FREE((*buf)->pw_gecos);
93 SAFE_FREE((*buf)->pw_dir);
94 SAFE_FREE((*buf)->pw_shell);
96 SAFE_FREE(*buf);
99 struct passwd *getpwnam_alloc(const char *name)
101 struct passwd *temp;
103 temp = getpwnam(name);
105 if (!temp) {
106 #if 0
107 if (errno == ENOMEM) {
108 /* what now? */
110 #endif
111 return NULL;
114 return alloc_copy_passwd(temp);
117 struct passwd *getpwuid_alloc(uid_t uid)
119 struct passwd *temp;
121 temp = getpwuid(uid);
123 if (!temp) {
124 #if 0
125 if (errno == ENOMEM) {
126 /* what now? */
128 #endif
129 return NULL;
132 return alloc_copy_passwd(temp);