smbd: Don't talloc_zero where we assign the struct a line below
[samba4-gss.git] / source3 / passdb / pdb_util.c
blobb732acacd85b10dd20fbcadff6ff13a8ae13eaf3
1 /*
2 * Unix SMB/CIFS implementation.
3 * Authentication utility functions
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Andrew Bartlett 2001
6 * Copyright (C) Jeremy Allison 2000-2001
7 * Copyright (C) Rafal Szczesniak 2002
8 * Copyright (C) Volker Lendecke 2006
9 * Copyright (C) Michael Adam 2007
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "../libcli/security/security.h"
27 #include "passdb.h"
28 #include "lib/winbind_util.h"
29 #include "../librpc/gen_ndr/idmap.h"
31 /**
32 * Add sid as a member of builtin_sid.
34 * @param[in] builtin_sid An existing builtin group.
35 * @param[in] dom_sid sid to add as a member of builtin_sid.
36 * @return Normal NTSTATUS return
38 static NTSTATUS add_sid_to_builtin(const struct dom_sid *builtin_sid,
39 const struct dom_sid *dom_sid)
41 NTSTATUS status;
43 if (!dom_sid || !builtin_sid) {
44 return NT_STATUS_INVALID_PARAMETER;
47 status = pdb_add_aliasmem(builtin_sid, dom_sid);
49 if (NT_STATUS_EQUAL(status, NT_STATUS_MEMBER_IN_ALIAS)) {
50 struct dom_sid_buf buf1, buf2;
51 DEBUG(5, ("add_sid_to_builtin %s is already a member of %s\n",
52 dom_sid_str_buf(dom_sid, &buf1),
53 dom_sid_str_buf(builtin_sid, &buf2)));
54 return NT_STATUS_OK;
57 if (!NT_STATUS_IS_OK(status)) {
58 struct dom_sid_buf buf1, buf2;
59 DEBUG(4, ("add_sid_to_builtin %s could not be added to %s: "
60 "%s\n",
61 dom_sid_str_buf(dom_sid, &buf1),
62 dom_sid_str_buf(builtin_sid, &buf2),
63 nt_errstr(status)));
65 return status;
68 /**
69 * Create the requested BUILTIN if it doesn't already exist. This requires
70 * winbindd to be running.
72 * @param[in] rid BUILTIN rid to create
73 * @return Normal NTSTATUS return.
75 NTSTATUS pdb_create_builtin(uint32_t rid)
77 NTSTATUS status = NT_STATUS_OK;
78 struct dom_sid sid;
79 gid_t gid;
80 bool mapresult;
82 if (!sid_compose(&sid, &global_sid_Builtin, rid)) {
83 return NT_STATUS_NO_SUCH_ALIAS;
86 if (!pdb_is_responsible_for_builtin()) {
88 * if this backend is not responsible for BUILTIN
90 * Use the gid from the mapping request for entry.
91 * If the mapping fails, bail out
93 mapresult = sid_to_gid(&sid, &gid);
94 if (!mapresult) {
95 status = NT_STATUS_NO_SUCH_GROUP;
96 } else {
97 status = pdb_create_builtin_alias(rid, gid);
99 } else {
101 * this backend is responsible for BUILTIN
103 * a failed mapping result means that the entry
104 * does not exist yet, so create it
106 * we use pdb_sid_to_id intentionally here to
107 * directly query the passdb backend (sid_to_gid
108 * would finally do the same)
110 struct unixid id;
111 mapresult = pdb_sid_to_id(&sid, &id);
112 if (!mapresult) {
113 if (!lp_winbind_nested_groups() || !winbind_ping()) {
114 return NT_STATUS_PROTOCOL_UNREACHABLE;
116 status = pdb_create_builtin_alias(rid, 0);
119 return status;
122 /*******************************************************************
123 *******************************************************************/
125 NTSTATUS create_builtin_users(const struct dom_sid *dom_sid)
127 NTSTATUS status;
128 struct dom_sid dom_users;
130 status = pdb_create_builtin(BUILTIN_RID_USERS);
131 if ( !NT_STATUS_IS_OK(status) ) {
132 DEBUG(5,("create_builtin_users: Failed to create Users\n"));
133 return status;
136 /* add domain users */
137 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER)) &&
138 (dom_sid != NULL) &&
139 sid_compose(&dom_users, dom_sid, DOMAIN_RID_USERS))
141 status = add_sid_to_builtin(&global_sid_Builtin_Users,
142 &dom_users);
145 return status;
148 /*******************************************************************
149 *******************************************************************/
151 NTSTATUS create_builtin_administrators(const struct dom_sid *dom_sid)
153 NTSTATUS status;
154 struct dom_sid dom_admins, root_sid;
155 fstring root_name;
156 enum lsa_SidType type;
157 TALLOC_CTX *ctx;
158 bool ret;
160 status = pdb_create_builtin(BUILTIN_RID_ADMINISTRATORS);
161 if ( !NT_STATUS_IS_OK(status) ) {
162 DEBUG(5,("create_builtin_administrators: Failed to create Administrators\n"));
163 return status;
166 /* add domain admins */
167 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER)) &&
168 (dom_sid != NULL) &&
169 sid_compose(&dom_admins, dom_sid, DOMAIN_RID_ADMINS))
171 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
172 &dom_admins);
173 if (!NT_STATUS_IS_OK(status)) {
174 return status;
178 /* add root */
179 if ( (ctx = talloc_init("create_builtin_administrators")) == NULL ) {
180 return NT_STATUS_NO_MEMORY;
182 fstr_sprintf( root_name, "%s\\root", get_global_sam_name() );
183 ret = lookup_name(ctx, root_name, LOOKUP_NAME_DOMAIN, NULL, NULL,
184 &root_sid, &type);
185 TALLOC_FREE( ctx );
187 if ( ret ) {
188 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
189 &root_sid);
192 return status;
195 /*******************************************************************
196 *******************************************************************/
198 NTSTATUS create_builtin_guests(const struct dom_sid *dom_sid)
200 NTSTATUS status;
201 struct dom_sid tmp_sid = { 0, };
203 status = pdb_create_builtin(BUILTIN_RID_GUESTS);
204 if (!NT_STATUS_IS_OK(status)) {
205 DEBUG(5,("create_builtin_guests: Failed to create Guests\n"));
206 return status;
209 /* add local guest */
210 if (sid_compose(&tmp_sid, get_global_sam_sid(), DOMAIN_RID_GUEST)) {
211 status = add_sid_to_builtin(&global_sid_Builtin_Guests,
212 &tmp_sid);
213 if (!NT_STATUS_IS_OK(status)) {
214 return status;
218 /* add local guests */
219 if (sid_compose(&tmp_sid, get_global_sam_sid(), DOMAIN_RID_GUESTS)) {
220 status = add_sid_to_builtin(&global_sid_Builtin_Guests,
221 &tmp_sid);
222 if (!NT_STATUS_IS_OK(status)) {
223 return status;
227 if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
228 return NT_STATUS_OK;
231 if (dom_sid == NULL) {
232 return NT_STATUS_INTERNAL_ERROR;
235 /* add domain guests */
236 if (sid_compose(&tmp_sid, dom_sid, DOMAIN_RID_GUESTS)) {
237 status = add_sid_to_builtin(&global_sid_Builtin_Guests,
238 &tmp_sid);
239 if (!NT_STATUS_IS_OK(status)) {
240 return status;
244 return NT_STATUS_OK;