selftest: generate a ramdon domain sid during provision and export as SAMSID/[TRUST_...
[Samba.git] / source4 / auth / system_session.c
blob4c5290db71a5654b54d051cefffff04c559bca91
1 /*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001-2010
6 Copyright (C) Jeremy Allison 2000-2001
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "libcli/security/security.h"
26 #include "auth/credentials/credentials.h"
27 #include "param/param.h"
28 #include "auth/auth.h" /* for auth_user_info_dc */
29 #include "auth/session.h"
30 #include "auth/system_session_proto.h"
34 prevent the static system session being freed
36 static int system_session_destructor(struct auth_session_info *info)
38 return -1;
41 /* Create a security token for a session SYSTEM (the most
42 * trusted/prvilaged account), including the local machine account as
43 * the off-host credentials
44 */
45 _PUBLIC_ struct auth_session_info *system_session(struct loadparm_context *lp_ctx)
47 static struct auth_session_info *static_session;
48 NTSTATUS nt_status;
50 if (static_session) {
51 return static_session;
55 * Use NULL here, not the autofree context for this
56 * static pointer. The destructor prevents freeing this
57 * memory anyway.
59 nt_status = auth_system_session_info(NULL,
60 lp_ctx,
61 &static_session);
62 if (!NT_STATUS_IS_OK(nt_status)) {
63 talloc_free(static_session);
64 static_session = NULL;
65 return NULL;
67 talloc_set_destructor(static_session, system_session_destructor);
68 return static_session;
71 NTSTATUS auth_system_session_info(TALLOC_CTX *parent_ctx,
72 struct loadparm_context *lp_ctx,
73 struct auth_session_info **_session_info)
75 NTSTATUS nt_status;
76 struct auth_user_info_dc *user_info_dc = NULL;
77 struct auth_session_info *session_info = NULL;
78 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
80 nt_status = auth_system_user_info_dc(mem_ctx, lpcfg_netbios_name(lp_ctx),
81 &user_info_dc);
82 if (!NT_STATUS_IS_OK(nt_status)) {
83 talloc_free(mem_ctx);
84 return nt_status;
87 /* references the user_info_dc into the session_info */
88 nt_status = auth_generate_session_info(parent_ctx, NULL, NULL, user_info_dc, AUTH_SESSION_INFO_SIMPLE_PRIVILEGES, &session_info);
89 talloc_free(mem_ctx);
91 NT_STATUS_NOT_OK_RETURN(nt_status);
93 session_info->credentials = cli_credentials_init(session_info);
94 if (!session_info->credentials) {
95 return NT_STATUS_NO_MEMORY;
98 cli_credentials_set_conf(session_info->credentials, lp_ctx);
100 cli_credentials_set_machine_account_pending(session_info->credentials, lp_ctx);
101 *_session_info = session_info;
103 return NT_STATUS_OK;
106 NTSTATUS auth_system_user_info_dc(TALLOC_CTX *mem_ctx, const char *netbios_name,
107 struct auth_user_info_dc **_user_info_dc)
109 struct auth_user_info_dc *user_info_dc;
110 struct auth_user_info *info;
112 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
113 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
115 /* This returns a pointer to a struct dom_sid, which is the
116 * same as a 1 element list of struct dom_sid */
117 user_info_dc->num_sids = 1;
118 user_info_dc->sids = dom_sid_parse_talloc(user_info_dc, SID_NT_SYSTEM);
119 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
121 /* annoying, but the Anonymous really does have a session key,
122 and it is all zeros! */
123 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, NULL, 16);
124 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
126 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, NULL, 16);
127 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
129 data_blob_clear(&user_info_dc->user_session_key);
130 data_blob_clear(&user_info_dc->lm_session_key);
132 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
133 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
135 info->account_name = talloc_strdup(info, "SYSTEM");
136 NT_STATUS_HAVE_NO_MEMORY(info->account_name);
138 info->domain_name = talloc_strdup(info, "NT AUTHORITY");
139 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
141 info->full_name = talloc_strdup(info, "System");
142 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
144 info->logon_script = talloc_strdup(info, "");
145 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
147 info->profile_path = talloc_strdup(info, "");
148 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
150 info->home_directory = talloc_strdup(info, "");
151 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
153 info->home_drive = talloc_strdup(info, "");
154 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
156 info->logon_server = talloc_strdup(info, netbios_name);
157 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
159 info->last_logon = 0;
160 info->last_logoff = 0;
161 info->acct_expiry = 0;
162 info->last_password_change = 0;
163 info->allow_password_change = 0;
164 info->force_password_change = 0;
166 info->logon_count = 0;
167 info->bad_password_count = 0;
169 info->acct_flags = ACB_NORMAL;
171 info->authenticated = true;
173 *_user_info_dc = user_info_dc;
175 return NT_STATUS_OK;
179 static NTSTATUS auth_domain_admin_user_info_dc(TALLOC_CTX *mem_ctx,
180 const char *netbios_name,
181 const char *domain_name,
182 struct dom_sid *domain_sid,
183 struct auth_user_info_dc **_user_info_dc)
185 struct auth_user_info_dc *user_info_dc;
186 struct auth_user_info *info;
188 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
189 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
191 user_info_dc->num_sids = 7;
192 user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid, user_info_dc->num_sids);
194 user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *domain_sid;
195 sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], DOMAIN_RID_ADMINISTRATOR);
197 user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
198 sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], DOMAIN_RID_USERS);
200 user_info_dc->sids[2] = global_sid_Builtin_Administrators;
202 user_info_dc->sids[3] = *domain_sid;
203 sid_append_rid(&user_info_dc->sids[3], DOMAIN_RID_ADMINS);
204 user_info_dc->sids[4] = *domain_sid;
205 sid_append_rid(&user_info_dc->sids[4], DOMAIN_RID_ENTERPRISE_ADMINS);
206 user_info_dc->sids[5] = *domain_sid;
207 sid_append_rid(&user_info_dc->sids[5], DOMAIN_RID_POLICY_ADMINS);
208 user_info_dc->sids[6] = *domain_sid;
209 sid_append_rid(&user_info_dc->sids[6], DOMAIN_RID_SCHEMA_ADMINS);
211 /* What should the session key be?*/
212 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, NULL, 16);
213 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
215 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, NULL, 16);
216 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
218 data_blob_clear(&user_info_dc->user_session_key);
219 data_blob_clear(&user_info_dc->lm_session_key);
221 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
222 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
224 info->account_name = talloc_strdup(info, "Administrator");
225 NT_STATUS_HAVE_NO_MEMORY(info->account_name);
227 info->domain_name = talloc_strdup(info, domain_name);
228 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
230 info->full_name = talloc_strdup(info, "Administrator");
231 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
233 info->logon_script = talloc_strdup(info, "");
234 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
236 info->profile_path = talloc_strdup(info, "");
237 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
239 info->home_directory = talloc_strdup(info, "");
240 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
242 info->home_drive = talloc_strdup(info, "");
243 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
245 info->logon_server = talloc_strdup(info, netbios_name);
246 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
248 info->last_logon = 0;
249 info->last_logoff = 0;
250 info->acct_expiry = 0;
251 info->last_password_change = 0;
252 info->allow_password_change = 0;
253 info->force_password_change = 0;
255 info->logon_count = 0;
256 info->bad_password_count = 0;
258 info->acct_flags = ACB_NORMAL;
260 info->authenticated = true;
262 *_user_info_dc = user_info_dc;
264 return NT_STATUS_OK;
267 static NTSTATUS auth_domain_admin_session_info(TALLOC_CTX *parent_ctx,
268 struct loadparm_context *lp_ctx,
269 struct dom_sid *domain_sid,
270 struct auth_session_info **session_info)
272 NTSTATUS nt_status;
273 struct auth_user_info_dc *user_info_dc = NULL;
274 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
276 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
278 nt_status = auth_domain_admin_user_info_dc(mem_ctx, lpcfg_netbios_name(lp_ctx),
279 lpcfg_workgroup(lp_ctx), domain_sid,
280 &user_info_dc);
281 if (!NT_STATUS_IS_OK(nt_status)) {
282 talloc_free(mem_ctx);
283 return nt_status;
286 nt_status = auth_generate_session_info(mem_ctx, NULL, NULL, user_info_dc,
287 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES|AUTH_SESSION_INFO_AUTHENTICATED|AUTH_SESSION_INFO_DEFAULT_GROUPS,
288 session_info);
289 /* There is already a reference between the sesion_info and user_info_dc */
290 if (NT_STATUS_IS_OK(nt_status)) {
291 talloc_steal(parent_ctx, *session_info);
293 talloc_free(mem_ctx);
294 return nt_status;
297 _PUBLIC_ struct auth_session_info *admin_session(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct dom_sid *domain_sid)
299 NTSTATUS nt_status;
300 struct auth_session_info *session_info = NULL;
301 nt_status = auth_domain_admin_session_info(mem_ctx,
302 lp_ctx,
303 domain_sid,
304 &session_info);
305 if (!NT_STATUS_IS_OK(nt_status)) {
306 return NULL;
308 return session_info;
311 _PUBLIC_ NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx,
312 struct loadparm_context *lp_ctx,
313 struct auth_session_info **_session_info)
315 NTSTATUS nt_status;
316 struct auth_user_info_dc *user_info_dc = NULL;
317 struct auth_session_info *session_info = NULL;
318 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
320 nt_status = auth_anonymous_user_info_dc(mem_ctx,
321 lpcfg_netbios_name(lp_ctx),
322 &user_info_dc);
323 if (!NT_STATUS_IS_OK(nt_status)) {
324 talloc_free(mem_ctx);
325 return nt_status;
328 /* references the user_info_dc into the session_info */
329 nt_status = auth_generate_session_info(parent_ctx, NULL, NULL, user_info_dc, AUTH_SESSION_INFO_SIMPLE_PRIVILEGES, &session_info);
330 talloc_free(mem_ctx);
332 NT_STATUS_NOT_OK_RETURN(nt_status);
334 session_info->credentials = cli_credentials_init(session_info);
335 if (!session_info->credentials) {
336 return NT_STATUS_NO_MEMORY;
339 cli_credentials_set_conf(session_info->credentials, lp_ctx);
340 cli_credentials_set_anonymous(session_info->credentials);
342 *_session_info = session_info;
344 return NT_STATUS_OK;
347 _PUBLIC_ NTSTATUS auth_anonymous_user_info_dc(TALLOC_CTX *mem_ctx,
348 const char *netbios_name,
349 struct auth_user_info_dc **_user_info_dc)
351 struct auth_user_info_dc *user_info_dc;
352 struct auth_user_info *info;
353 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
354 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
356 /* This returns a pointer to a struct dom_sid, which is the
357 * same as a 1 element list of struct dom_sid */
358 user_info_dc->num_sids = 1;
359 user_info_dc->sids = dom_sid_parse_talloc(user_info_dc, SID_NT_ANONYMOUS);
360 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
362 /* annoying, but the Anonymous really does have a session key... */
363 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, NULL, 16);
364 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
366 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, NULL, 16);
367 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
369 /* and it is all zeros! */
370 data_blob_clear(&user_info_dc->user_session_key);
371 data_blob_clear(&user_info_dc->lm_session_key);
373 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
374 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
376 info->account_name = talloc_strdup(info, "ANONYMOUS LOGON");
377 NT_STATUS_HAVE_NO_MEMORY(info->account_name);
379 info->domain_name = talloc_strdup(info, "NT AUTHORITY");
380 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
382 info->full_name = talloc_strdup(info, "Anonymous Logon");
383 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
385 info->logon_script = talloc_strdup(info, "");
386 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
388 info->profile_path = talloc_strdup(info, "");
389 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
391 info->home_directory = talloc_strdup(info, "");
392 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
394 info->home_drive = talloc_strdup(info, "");
395 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
397 info->logon_server = talloc_strdup(info, netbios_name);
398 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
400 info->last_logon = 0;
401 info->last_logoff = 0;
402 info->acct_expiry = 0;
403 info->last_password_change = 0;
404 info->allow_password_change = 0;
405 info->force_password_change = 0;
407 info->logon_count = 0;
408 info->bad_password_count = 0;
410 info->acct_flags = ACB_NORMAL;
412 info->authenticated = false;
414 *_user_info_dc = user_info_dc;
416 return NT_STATUS_OK;