backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / lib / util / util_pw.c
blobdae3a69492ae43af28e16ac5c6bb8e4863edda9e
1 /*
2 Unix SMB/CIFS implementation.
4 Safe versions of getpw* calls
6 Copyright (C) Andrew Tridgell 1992-1998
7 Copyright (C) Jeremy Allison 1998-2005
8 Copyright (C) Andrew Bartlett 2002
9 Copyright (C) Timur Bakeyev 2005
10 Copyright (C) Bjoern Jacke 2006-2007
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
28 #include "system/passwd.h"
29 #include "lib/util/util_pw.h"
31 struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx,
32 const struct passwd *from)
34 struct passwd *ret;
35 size_t len = 0;
37 len += strlen(from->pw_name)+1;
38 len += strlen(from->pw_passwd)+1;
39 len += strlen(from->pw_gecos)+1;
40 len += strlen(from->pw_dir)+1;
41 len += strlen(from->pw_shell)+1;
43 ret = talloc_pooled_object(mem_ctx, struct passwd, 5, len);
45 if (ret == NULL) {
46 return NULL;
49 ret->pw_name = talloc_strdup(ret, from->pw_name);
50 ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
51 ret->pw_uid = from->pw_uid;
52 ret->pw_gid = from->pw_gid;
53 ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
54 ret->pw_dir = talloc_strdup(ret, from->pw_dir);
55 ret->pw_shell = talloc_strdup(ret, from->pw_shell);
57 return ret;
60 struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
62 struct passwd *temp;
64 temp = getpwnam(name);
66 if (!temp) {
67 #if 0
68 if (errno == ENOMEM) {
69 /* what now? */
71 #endif
72 return NULL;
75 return tcopy_passwd(mem_ctx, temp);
78 /****************************************************************************
79 talloc'ed version of getpwuid.
80 ****************************************************************************/
82 struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
84 struct passwd *temp;
86 temp = getpwuid(uid);
88 if (!temp) {
89 #if 0
90 if (errno == ENOMEM) {
91 /* what now? */
93 #endif
94 return NULL;
97 return tcopy_passwd(mem_ctx, temp);