2 Unix SMB/Netbios implementation.
5 Safe versions of getpw* calls
7 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
26 struct passwd *make_modifyable_passwd(const struct passwd *from)
28 struct passwd *ret = smb_xmalloc(sizeof(*ret));
29 /* This is the assumed shape of the members by certain parts of the code...
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));
45 * Now point the struct's members as the
46 * newly allocated buffers:
49 ret->pw_name = pw_name;
50 fstrcpy(ret->pw_name, from->pw_name);
52 ret->pw_passwd = pw_passwd;
53 fstrcpy(ret->pw_passwd, from->pw_passwd);
55 ret->pw_uid = from->pw_uid;
56 ret->pw_gid = from->pw_gid;
58 ret->pw_gecos = pw_gecos;
59 fstrcpy(ret->pw_gecos, from->pw_gecos);
62 pstrcpy(ret->pw_dir, from->pw_dir);
64 ret->pw_shell = pw_shell;
65 pstrcpy(ret->pw_shell, from->pw_shell);
70 static struct passwd *alloc_copy_passwd(const struct passwd *from)
72 struct passwd *ret = smb_xmalloc(sizeof(*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);
84 void passwd_free (struct passwd **buf)
87 DEBUG(0, ("attempted double-free of allocated passwd\n"));
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);
100 struct passwd *getpwnam_alloc(const char *name)
104 temp = getpwnam(name);
108 if (errno == ENOMEM) {
115 return alloc_copy_passwd(temp);
118 struct passwd *getpwuid_alloc(uid_t uid)
122 temp = getpwuid(uid);
126 if (errno == ENOMEM) {
133 return alloc_copy_passwd(temp);