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 3 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, see <http://www.gnu.org/licenses/>.
24 static struct passwd *alloc_copy_passwd(const struct passwd *from)
26 struct passwd *ret = smb_xmalloc_p(struct passwd);
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);
38 void passwd_free (struct passwd **buf)
41 DEBUG(0, ("attempted double-free of allocated passwd\n"));
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);
54 struct passwd *getpwnam_alloc(const char *name)
58 temp = sys_getpwnam(name);
62 if (errno == ENOMEM) {
69 return alloc_copy_passwd(temp);
72 struct passwd *getpwuid_alloc(uid_t uid)
76 temp = sys_getpwuid(uid);
80 if (errno == ENOMEM) {
87 return alloc_copy_passwd(temp);