s4:libcli/raw: remove unused SMB_SIGNING_AUTO handling
[Samba/vl.git] / source3 / passdb / pdb_util.c
blob3b7377c31274ff628ed2fe6664f9f5f793b18338
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"
30 /**
31 * Add sid as a member of builtin_sid.
33 * @param[in] builtin_sid An existing builtin group.
34 * @param[in] dom_sid sid to add as a member of builtin_sid.
35 * @return Normal NTSTATUS return
37 static NTSTATUS add_sid_to_builtin(const struct dom_sid *builtin_sid,
38 const struct dom_sid *dom_sid)
40 NTSTATUS status = NT_STATUS_OK;
42 if (!dom_sid || !builtin_sid) {
43 return NT_STATUS_INVALID_PARAMETER;
46 status = pdb_add_aliasmem(builtin_sid, dom_sid);
48 if (NT_STATUS_EQUAL(status, NT_STATUS_MEMBER_IN_ALIAS)) {
49 DEBUG(5, ("add_sid_to_builtin %s is already a member of %s\n",
50 sid_string_dbg(dom_sid),
51 sid_string_dbg(builtin_sid)));
52 return NT_STATUS_OK;
55 if (!NT_STATUS_IS_OK(status)) {
56 DEBUG(4, ("add_sid_to_builtin %s could not be added to %s: "
57 "%s\n", sid_string_dbg(dom_sid),
58 sid_string_dbg(builtin_sid), nt_errstr(status)));
60 return status;
63 /**
64 * Create the requested BUILTIN if it doesn't already exist. This requires
65 * winbindd to be running.
67 * @param[in] rid BUILTIN rid to create
68 * @return Normal NTSTATUS return.
70 static NTSTATUS create_builtin(uint32 rid)
72 NTSTATUS status = NT_STATUS_OK;
73 struct dom_sid sid;
74 gid_t gid;
76 if (!sid_compose(&sid, &global_sid_Builtin, rid)) {
77 return NT_STATUS_NO_SUCH_ALIAS;
80 if (!sid_to_gid(&sid, &gid)) {
81 if (!lp_winbind_nested_groups() || !winbind_ping()) {
82 return NT_STATUS_PROTOCOL_UNREACHABLE;
84 status = pdb_create_builtin_alias(rid);
86 return status;
89 /*******************************************************************
90 *******************************************************************/
92 NTSTATUS create_builtin_users(const struct dom_sid *dom_sid)
94 NTSTATUS status;
95 struct dom_sid dom_users;
97 status = create_builtin(BUILTIN_RID_USERS);
98 if ( !NT_STATUS_IS_OK(status) ) {
99 DEBUG(5,("create_builtin_users: Failed to create Users\n"));
100 return status;
103 /* add domain users */
104 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
105 && sid_compose(&dom_users, dom_sid, DOMAIN_RID_USERS))
107 status = add_sid_to_builtin(&global_sid_Builtin_Users,
108 &dom_users);
111 return status;
114 /*******************************************************************
115 *******************************************************************/
117 NTSTATUS create_builtin_administrators(const struct dom_sid *dom_sid)
119 NTSTATUS status;
120 struct dom_sid dom_admins, root_sid;
121 fstring root_name;
122 enum lsa_SidType type;
123 TALLOC_CTX *ctx;
124 bool ret;
126 status = create_builtin(BUILTIN_RID_ADMINISTRATORS);
127 if ( !NT_STATUS_IS_OK(status) ) {
128 DEBUG(5,("create_builtin_administrators: Failed to create Administrators\n"));
129 return status;
132 /* add domain admins */
133 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
134 && sid_compose(&dom_admins, dom_sid, DOMAIN_RID_ADMINS))
136 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
137 &dom_admins);
138 if (!NT_STATUS_IS_OK(status)) {
139 return status;
143 /* add root */
144 if ( (ctx = talloc_init("create_builtin_administrators")) == NULL ) {
145 return NT_STATUS_NO_MEMORY;
147 fstr_sprintf( root_name, "%s\\root", get_global_sam_name() );
148 ret = lookup_name(ctx, root_name, LOOKUP_NAME_DOMAIN, NULL, NULL,
149 &root_sid, &type);
150 TALLOC_FREE( ctx );
152 if ( ret ) {
153 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
154 &root_sid);
157 return status;