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/>.
26 #include "../libcli/security/security.h"
28 #include "lib/winbind_util.h"
29 #include "../librpc/gen_ndr/idmap.h"
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
)
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
)));
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: "
61 dom_sid_str_buf(dom_sid
, &buf1
),
62 dom_sid_str_buf(builtin_sid
, &buf2
),
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
;
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
);
95 status
= NT_STATUS_NO_SUCH_GROUP
;
97 status
= pdb_create_builtin_alias(rid
, gid
);
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)
111 mapresult
= pdb_sid_to_id(&sid
, &id
);
113 if (!lp_winbind_nested_groups() || !winbind_ping()) {
114 return NT_STATUS_PROTOCOL_UNREACHABLE
;
116 status
= pdb_create_builtin_alias(rid
, 0);
122 /*******************************************************************
123 *******************************************************************/
125 NTSTATUS
create_builtin_users(const struct dom_sid
*dom_sid
)
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"));
136 /* add domain users */
137 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
)) &&
139 sid_compose(&dom_users
, dom_sid
, DOMAIN_RID_USERS
))
141 status
= add_sid_to_builtin(&global_sid_Builtin_Users
,
148 /*******************************************************************
149 *******************************************************************/
151 NTSTATUS
create_builtin_administrators(const struct dom_sid
*dom_sid
)
154 struct dom_sid dom_admins
, root_sid
;
156 enum lsa_SidType type
;
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"));
166 /* add domain admins */
167 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
)) &&
169 sid_compose(&dom_admins
, dom_sid
, DOMAIN_RID_ADMINS
))
171 status
= add_sid_to_builtin(&global_sid_Builtin_Administrators
,
173 if (!NT_STATUS_IS_OK(status
)) {
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
,
188 status
= add_sid_to_builtin(&global_sid_Builtin_Administrators
,
195 /*******************************************************************
196 *******************************************************************/
198 NTSTATUS
create_builtin_guests(const struct dom_sid
*dom_sid
)
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"));
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
,
213 if (!NT_STATUS_IS_OK(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
,
222 if (!NT_STATUS_IS_OK(status
)) {
227 if (lp_server_role() != ROLE_DOMAIN_MEMBER
) {
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
,
239 if (!NT_STATUS_IS_OK(status
)) {