Created net gpo and new libgpo for samba 4. This adds the init function which finds...
[Samba.git] / source4 / auth / system_session.c
blob8fdf52792e21dde668a2caf450cdf19f8d573485
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_serversupplied_info */
29 #include "auth/session.h"
30 #include "auth/system_session_proto.h"
32 /**
33 * Create the SID list for this user.
35 * @note Specialised version for system sessions that doesn't use the SAM.
37 static NTSTATUS create_token(TALLOC_CTX *mem_ctx,
38 struct dom_sid *user_sid,
39 struct dom_sid *group_sid,
40 unsigned int n_groupSIDs,
41 struct dom_sid **groupSIDs,
42 bool is_authenticated,
43 struct security_token **token)
45 struct security_token *ptoken;
46 unsigned int i;
48 ptoken = security_token_initialise(mem_ctx);
49 NT_STATUS_HAVE_NO_MEMORY(ptoken);
51 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
52 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
54 ptoken->user_sid = talloc_reference(ptoken, user_sid);
55 ptoken->group_sid = talloc_reference(ptoken, group_sid);
56 ptoken->privilege_mask = 0;
58 ptoken->sids[0] = ptoken->user_sid;
59 ptoken->sids[1] = ptoken->group_sid;
62 * Finally add the "standard" SIDs.
63 * The only difference between guest and "anonymous"
64 * is the addition of Authenticated_Users.
66 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
67 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
68 ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
69 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
70 ptoken->num_sids = 4;
72 if (is_authenticated) {
73 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
74 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
75 ptoken->num_sids++;
78 for (i = 0; i < n_groupSIDs; i++) {
79 size_t check_sid_idx;
80 for (check_sid_idx = 1;
81 check_sid_idx < ptoken->num_sids;
82 check_sid_idx++) {
83 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
84 break;
88 if (check_sid_idx == ptoken->num_sids) {
89 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
93 *token = ptoken;
95 /* Shortcuts to prevent recursion and avoid lookups */
96 if (ptoken->user_sid == NULL) {
97 ptoken->privilege_mask = 0;
98 return NT_STATUS_OK;
101 if (security_token_is_system(ptoken)) {
102 ptoken->privilege_mask = ~0;
103 return NT_STATUS_OK;
106 if (security_token_is_anonymous(ptoken)) {
107 ptoken->privilege_mask = 0;
108 return NT_STATUS_OK;
111 /* All other 'users' get a empty priv set so far */
112 ptoken->privilege_mask = 0;
113 return NT_STATUS_OK;
116 NTSTATUS auth_generate_simple_session_info(TALLOC_CTX *mem_ctx,
117 struct auth_serversupplied_info *server_info,
118 struct auth_session_info **_session_info)
120 struct auth_session_info *session_info;
121 NTSTATUS nt_status;
123 session_info = talloc(mem_ctx, struct auth_session_info);
124 NT_STATUS_HAVE_NO_MEMORY(session_info);
126 session_info->server_info = talloc_reference(session_info, server_info);
128 /* unless set otherwise, the session key is the user session
129 * key from the auth subsystem */
130 session_info->session_key = server_info->user_session_key;
132 nt_status = create_token(session_info,
133 server_info->account_sid,
134 server_info->primary_group_sid,
135 server_info->n_domain_groups,
136 server_info->domain_groups,
137 server_info->authenticated,
138 &session_info->security_token);
139 NT_STATUS_NOT_OK_RETURN(nt_status);
141 session_info->credentials = NULL;
143 *_session_info = session_info;
144 return NT_STATUS_OK;
149 prevent the static system session being freed
151 static int system_session_destructor(struct auth_session_info *info)
153 return -1;
156 /* Create a security token for a session SYSTEM (the most
157 * trusted/prvilaged account), including the local machine account as
158 * the off-host credentials
160 _PUBLIC_ struct auth_session_info *system_session(struct loadparm_context *lp_ctx)
162 static struct auth_session_info *static_session;
163 NTSTATUS nt_status;
165 if (static_session) {
166 return static_session;
169 nt_status = auth_system_session_info(talloc_autofree_context(),
170 lp_ctx,
171 &static_session);
172 if (!NT_STATUS_IS_OK(nt_status)) {
173 talloc_free(static_session);
174 static_session = NULL;
175 return NULL;
177 talloc_set_destructor(static_session, system_session_destructor);
178 return static_session;
181 static NTSTATUS _auth_system_session_info(TALLOC_CTX *parent_ctx,
182 struct loadparm_context *lp_ctx,
183 bool anonymous_credentials,
184 struct auth_session_info **_session_info)
186 NTSTATUS nt_status;
187 struct auth_serversupplied_info *server_info = NULL;
188 struct auth_session_info *session_info = NULL;
189 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
191 nt_status = auth_system_server_info(mem_ctx, lp_netbios_name(lp_ctx),
192 &server_info);
193 if (!NT_STATUS_IS_OK(nt_status)) {
194 talloc_free(mem_ctx);
195 return nt_status;
198 /* references the server_info into the session_info */
199 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
200 talloc_free(mem_ctx);
202 NT_STATUS_NOT_OK_RETURN(nt_status);
204 session_info->credentials = cli_credentials_init(session_info);
205 if (!session_info->credentials) {
206 return NT_STATUS_NO_MEMORY;
209 cli_credentials_set_conf(session_info->credentials, lp_ctx);
211 if (anonymous_credentials) {
212 cli_credentials_set_anonymous(session_info->credentials);
213 } else {
214 cli_credentials_set_machine_account_pending(session_info->credentials, lp_ctx);
216 *_session_info = session_info;
218 return NT_STATUS_OK;
222 Create a system session, but with anonymous credentials (so we do not need to open secrets.ldb)
224 _PUBLIC_ struct auth_session_info *system_session_anon(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
226 NTSTATUS nt_status;
227 struct auth_session_info *session_info = NULL;
228 nt_status = _auth_system_session_info(mem_ctx, lp_ctx, false, &session_info);
229 if (!NT_STATUS_IS_OK(nt_status)) {
230 return NULL;
232 return session_info;
237 _PUBLIC_ NTSTATUS auth_system_session_info(TALLOC_CTX *parent_ctx,
238 struct loadparm_context *lp_ctx,
239 struct auth_session_info **_session_info)
241 return _auth_system_session_info(parent_ctx,
242 lp_ctx,
243 lp_parm_bool(lp_ctx, NULL, "system", "anonymous", false),
244 _session_info);
247 NTSTATUS auth_system_server_info(TALLOC_CTX *mem_ctx, const char *netbios_name,
248 struct auth_serversupplied_info **_server_info)
250 struct auth_serversupplied_info *server_info;
252 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
253 NT_STATUS_HAVE_NO_MEMORY(server_info);
255 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_SYSTEM);
256 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
258 /* is this correct? */
259 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
260 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
262 server_info->n_domain_groups = 0;
263 server_info->domain_groups = NULL;
265 /* annoying, but the Anonymous really does have a session key,
266 and it is all zeros! */
267 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
268 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
270 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
271 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
273 data_blob_clear(&server_info->user_session_key);
274 data_blob_clear(&server_info->lm_session_key);
276 server_info->account_name = talloc_strdup(server_info, "SYSTEM");
277 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
279 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
280 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
282 server_info->full_name = talloc_strdup(server_info, "System");
283 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
285 server_info->logon_script = talloc_strdup(server_info, "");
286 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
288 server_info->profile_path = talloc_strdup(server_info, "");
289 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
291 server_info->home_directory = talloc_strdup(server_info, "");
292 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
294 server_info->home_drive = talloc_strdup(server_info, "");
295 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
297 server_info->logon_server = talloc_strdup(server_info, netbios_name);
298 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
300 server_info->last_logon = 0;
301 server_info->last_logoff = 0;
302 server_info->acct_expiry = 0;
303 server_info->last_password_change = 0;
304 server_info->allow_password_change = 0;
305 server_info->force_password_change = 0;
307 server_info->logon_count = 0;
308 server_info->bad_password_count = 0;
310 server_info->acct_flags = ACB_NORMAL;
312 server_info->authenticated = true;
314 *_server_info = server_info;
316 return NT_STATUS_OK;
320 /* Create server info for the Administrator account. This should only be used
321 * during provisioning when we need to impersonate Administrator but
322 * the account has not been created yet */
324 static NTSTATUS create_admin_token(TALLOC_CTX *mem_ctx,
325 struct dom_sid *user_sid,
326 struct dom_sid *group_sid,
327 unsigned int n_groupSIDs,
328 struct dom_sid **groupSIDs,
329 struct security_token **token)
331 struct security_token *ptoken;
332 unsigned int i;
334 ptoken = security_token_initialise(mem_ctx);
335 NT_STATUS_HAVE_NO_MEMORY(ptoken);
337 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 3);
338 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
340 ptoken->user_sid = talloc_reference(ptoken, user_sid);
341 ptoken->group_sid = talloc_reference(ptoken, group_sid);
342 ptoken->privilege_mask = 0;
344 ptoken->sids[0] = ptoken->user_sid;
345 ptoken->sids[1] = ptoken->group_sid;
346 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
347 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
348 ptoken->num_sids = 3;
351 for (i = 0; i < n_groupSIDs; i++) {
352 size_t check_sid_idx;
353 for (check_sid_idx = 1;
354 check_sid_idx < ptoken->num_sids;
355 check_sid_idx++) {
356 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
357 break;
361 if (check_sid_idx == ptoken->num_sids) {
362 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
366 *token = ptoken;
367 ptoken->privilege_mask = ~0;
368 return NT_STATUS_OK;
371 static NTSTATUS auth_domain_admin_server_info(TALLOC_CTX *mem_ctx,
372 const char *netbios_name,
373 const char *domain_name,
374 struct dom_sid *domain_sid,
375 struct auth_serversupplied_info **_server_info)
377 struct auth_serversupplied_info *server_info;
379 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
380 NT_STATUS_HAVE_NO_MEMORY(server_info);
382 server_info->account_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINISTRATOR);
383 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
385 server_info->primary_group_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
386 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
388 server_info->n_domain_groups = 6;
389 server_info->domain_groups = talloc_array(server_info, struct dom_sid *, server_info->n_domain_groups);
391 server_info->domain_groups[0] = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
392 server_info->domain_groups[1] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINS);
393 server_info->domain_groups[2] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
394 server_info->domain_groups[3] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
395 server_info->domain_groups[4] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_POLICY_ADMINS);
396 server_info->domain_groups[5] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
398 /* What should the session key be?*/
399 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
400 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
402 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
403 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
405 data_blob_clear(&server_info->user_session_key);
406 data_blob_clear(&server_info->lm_session_key);
408 server_info->account_name = talloc_strdup(server_info, "Administrator");
409 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
411 server_info->domain_name = talloc_strdup(server_info, domain_name);
412 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
414 server_info->full_name = talloc_strdup(server_info, "Administrator");
415 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
417 server_info->logon_script = talloc_strdup(server_info, "");
418 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
420 server_info->profile_path = talloc_strdup(server_info, "");
421 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
423 server_info->home_directory = talloc_strdup(server_info, "");
424 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
426 server_info->home_drive = talloc_strdup(server_info, "");
427 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
429 server_info->logon_server = talloc_strdup(server_info, netbios_name);
430 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
432 server_info->last_logon = 0;
433 server_info->last_logoff = 0;
434 server_info->acct_expiry = 0;
435 server_info->last_password_change = 0;
436 server_info->allow_password_change = 0;
437 server_info->force_password_change = 0;
439 server_info->logon_count = 0;
440 server_info->bad_password_count = 0;
442 server_info->acct_flags = ACB_NORMAL;
444 server_info->authenticated = true;
446 *_server_info = server_info;
448 return NT_STATUS_OK;
451 static NTSTATUS auth_domain_admin_session_info(TALLOC_CTX *parent_ctx,
452 struct loadparm_context *lp_ctx,
453 struct dom_sid *domain_sid,
454 struct auth_session_info **_session_info)
456 NTSTATUS nt_status;
457 struct auth_serversupplied_info *server_info = NULL;
458 struct auth_session_info *session_info = NULL;
459 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
461 nt_status = auth_domain_admin_server_info(mem_ctx, lp_netbios_name(lp_ctx),
462 lp_workgroup(lp_ctx), domain_sid,
463 &server_info);
464 if (!NT_STATUS_IS_OK(nt_status)) {
465 talloc_free(mem_ctx);
466 return nt_status;
469 session_info = talloc(mem_ctx, struct auth_session_info);
470 NT_STATUS_HAVE_NO_MEMORY(session_info);
472 session_info->server_info = talloc_reference(session_info, server_info);
474 /* unless set otherwise, the session key is the user session
475 * key from the auth subsystem */
476 session_info->session_key = server_info->user_session_key;
478 nt_status = create_admin_token(session_info,
479 server_info->account_sid,
480 server_info->primary_group_sid,
481 server_info->n_domain_groups,
482 server_info->domain_groups,
483 &session_info->security_token);
484 NT_STATUS_NOT_OK_RETURN(nt_status);
486 session_info->credentials = cli_credentials_init(session_info);
487 if (!session_info->credentials) {
488 return NT_STATUS_NO_MEMORY;
491 cli_credentials_set_conf(session_info->credentials, lp_ctx);
493 *_session_info = session_info;
495 return NT_STATUS_OK;
498 _PUBLIC_ struct auth_session_info *admin_session(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct dom_sid *domain_sid)
500 NTSTATUS nt_status;
501 struct auth_session_info *session_info = NULL;
502 nt_status = auth_domain_admin_session_info(mem_ctx,
503 lp_ctx,
504 domain_sid,
505 &session_info);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 return NULL;
509 return session_info;
512 _PUBLIC_ NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx,
513 struct loadparm_context *lp_ctx,
514 struct auth_session_info **_session_info)
516 NTSTATUS nt_status;
517 struct auth_serversupplied_info *server_info = NULL;
518 struct auth_session_info *session_info = NULL;
519 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
521 nt_status = auth_anonymous_server_info(mem_ctx,
522 lp_netbios_name(lp_ctx),
523 &server_info);
524 if (!NT_STATUS_IS_OK(nt_status)) {
525 talloc_free(mem_ctx);
526 return nt_status;
529 /* references the server_info into the session_info */
530 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
531 talloc_free(mem_ctx);
533 NT_STATUS_NOT_OK_RETURN(nt_status);
535 session_info->credentials = cli_credentials_init(session_info);
536 if (!session_info->credentials) {
537 return NT_STATUS_NO_MEMORY;
540 cli_credentials_set_conf(session_info->credentials, lp_ctx);
541 cli_credentials_set_anonymous(session_info->credentials);
543 *_session_info = session_info;
545 return NT_STATUS_OK;
548 _PUBLIC_ NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx,
549 const char *netbios_name,
550 struct auth_serversupplied_info **_server_info)
552 struct auth_serversupplied_info *server_info;
553 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
554 NT_STATUS_HAVE_NO_MEMORY(server_info);
556 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
557 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
559 /* is this correct? */
560 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
561 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
563 server_info->n_domain_groups = 0;
564 server_info->domain_groups = NULL;
566 /* annoying, but the Anonymous really does have a session key... */
567 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
568 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
570 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
571 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
573 /* and it is all zeros! */
574 data_blob_clear(&server_info->user_session_key);
575 data_blob_clear(&server_info->lm_session_key);
577 server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
578 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
580 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
581 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
583 server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
584 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
586 server_info->logon_script = talloc_strdup(server_info, "");
587 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
589 server_info->profile_path = talloc_strdup(server_info, "");
590 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
592 server_info->home_directory = talloc_strdup(server_info, "");
593 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
595 server_info->home_drive = talloc_strdup(server_info, "");
596 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
598 server_info->logon_server = talloc_strdup(server_info, netbios_name);
599 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
601 server_info->last_logon = 0;
602 server_info->last_logoff = 0;
603 server_info->acct_expiry = 0;
604 server_info->last_password_change = 0;
605 server_info->allow_password_change = 0;
606 server_info->force_password_change = 0;
608 server_info->logon_count = 0;
609 server_info->bad_password_count = 0;
611 server_info->acct_flags = ACB_NORMAL;
613 server_info->authenticated = false;
615 *_server_info = server_info;
617 return NT_STATUS_OK;