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.
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...
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
));
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
);
61 pstrcpy(ret
->pw_dir
, from
->pw_dir
);
63 ret
->pw_shell
= pw_shell
;
64 pstrcpy(ret
->pw_shell
, from
->pw_shell
);
69 static struct passwd
*alloc_copy_passwd(const struct passwd
*from
)
71 struct passwd
*ret
= smb_xmalloc(sizeof(*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
);
83 void passwd_free (struct passwd
**buf
)
86 DEBUG(0, ("attempted double-free of allocated passwd\n"));
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
);
99 struct passwd
*getpwnam_alloc(const char *name
)
103 temp
= getpwnam(name
);
107 if (errno
== ENOMEM
) {
114 return alloc_copy_passwd(temp
);
117 struct passwd
*getpwuid_alloc(uid_t uid
)
121 temp
= getpwuid(uid
);
125 if (errno
== ENOMEM
) {
132 return alloc_copy_passwd(temp
);