s4-libnet: cope with an empty client site name from CLDAP
[Samba/gebeck_regimport.git] / source4 / auth / system_session.c
blobc6df082f69756ef55683d25e19828c063cb3d829
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 "libcli/auth/libcli_auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "param/param.h"
29 #include "auth/auth.h" /* for auth_serversupplied_info */
30 #include "auth/session.h"
31 #include "auth/system_session_proto.h"
33 /**
34 * Create the SID list for this user.
36 * @note Specialised version for system sessions that doesn't use the SAM.
38 static NTSTATUS create_token(TALLOC_CTX *mem_ctx,
39 struct dom_sid *user_sid,
40 struct dom_sid *group_sid,
41 unsigned int n_groupSIDs,
42 struct dom_sid **groupSIDs,
43 bool is_authenticated,
44 struct security_token **token)
46 struct security_token *ptoken;
47 unsigned int i;
49 ptoken = security_token_initialise(mem_ctx);
50 NT_STATUS_HAVE_NO_MEMORY(ptoken);
52 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
53 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
55 ptoken->user_sid = talloc_reference(ptoken, user_sid);
56 ptoken->group_sid = talloc_reference(ptoken, group_sid);
57 ptoken->privilege_mask = 0;
59 ptoken->sids[0] = ptoken->user_sid;
60 ptoken->sids[1] = ptoken->group_sid;
63 * Finally add the "standard" SIDs.
64 * The only difference between guest and "anonymous"
65 * is the addition of Authenticated_Users.
67 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
68 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
69 ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
70 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
71 ptoken->num_sids = 4;
73 if (is_authenticated) {
74 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
75 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
76 ptoken->num_sids++;
79 for (i = 0; i < n_groupSIDs; i++) {
80 size_t check_sid_idx;
81 for (check_sid_idx = 1;
82 check_sid_idx < ptoken->num_sids;
83 check_sid_idx++) {
84 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
85 break;
89 if (check_sid_idx == ptoken->num_sids) {
90 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
94 *token = ptoken;
96 /* Shortcuts to prevent recursion and avoid lookups */
97 if (ptoken->user_sid == NULL) {
98 ptoken->privilege_mask = 0;
99 return NT_STATUS_OK;
102 if (security_token_is_system(ptoken)) {
103 ptoken->privilege_mask = ~0;
104 return NT_STATUS_OK;
107 if (security_token_is_anonymous(ptoken)) {
108 ptoken->privilege_mask = 0;
109 return NT_STATUS_OK;
112 /* All other 'users' get a empty priv set so far */
113 ptoken->privilege_mask = 0;
114 return NT_STATUS_OK;
117 NTSTATUS auth_generate_simple_session_info(TALLOC_CTX *mem_ctx,
118 struct auth_serversupplied_info *server_info,
119 struct auth_session_info **_session_info)
121 struct auth_session_info *session_info;
122 NTSTATUS nt_status;
124 session_info = talloc(mem_ctx, struct auth_session_info);
125 NT_STATUS_HAVE_NO_MEMORY(session_info);
127 session_info->server_info = talloc_reference(session_info, server_info);
129 /* unless set otherwise, the session key is the user session
130 * key from the auth subsystem */
131 session_info->session_key = server_info->user_session_key;
133 nt_status = create_token(session_info,
134 server_info->account_sid,
135 server_info->primary_group_sid,
136 server_info->n_domain_groups,
137 server_info->domain_groups,
138 server_info->authenticated,
139 &session_info->security_token);
140 NT_STATUS_NOT_OK_RETURN(nt_status);
142 session_info->credentials = NULL;
144 *_session_info = session_info;
145 return NT_STATUS_OK;
150 prevent the static system session being freed
152 static int system_session_destructor(struct auth_session_info *info)
154 return -1;
157 /* Create a security token for a session SYSTEM (the most
158 * trusted/prvilaged account), including the local machine account as
159 * the off-host credentials
161 _PUBLIC_ struct auth_session_info *system_session(struct loadparm_context *lp_ctx)
163 static struct auth_session_info *static_session;
164 NTSTATUS nt_status;
166 if (static_session) {
167 return static_session;
170 nt_status = auth_system_session_info(talloc_autofree_context(),
171 lp_ctx,
172 &static_session);
173 if (!NT_STATUS_IS_OK(nt_status)) {
174 talloc_free(static_session);
175 static_session = NULL;
176 return NULL;
178 talloc_set_destructor(static_session, system_session_destructor);
179 return static_session;
182 static NTSTATUS _auth_system_session_info(TALLOC_CTX *parent_ctx,
183 struct loadparm_context *lp_ctx,
184 bool anonymous_credentials,
185 struct auth_session_info **_session_info)
187 NTSTATUS nt_status;
188 struct auth_serversupplied_info *server_info = NULL;
189 struct auth_session_info *session_info = NULL;
190 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
192 nt_status = auth_system_server_info(mem_ctx, lp_netbios_name(lp_ctx),
193 &server_info);
194 if (!NT_STATUS_IS_OK(nt_status)) {
195 talloc_free(mem_ctx);
196 return nt_status;
199 /* references the server_info into the session_info */
200 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
201 talloc_free(mem_ctx);
203 NT_STATUS_NOT_OK_RETURN(nt_status);
205 session_info->credentials = cli_credentials_init(session_info);
206 if (!session_info->credentials) {
207 return NT_STATUS_NO_MEMORY;
210 cli_credentials_set_conf(session_info->credentials, lp_ctx);
212 if (anonymous_credentials) {
213 cli_credentials_set_anonymous(session_info->credentials);
214 } else {
215 cli_credentials_set_machine_account_pending(session_info->credentials, lp_ctx);
217 *_session_info = session_info;
219 return NT_STATUS_OK;
223 Create a system session, but with anonymous credentials (so we do not need to open secrets.ldb)
225 _PUBLIC_ struct auth_session_info *system_session_anon(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
227 NTSTATUS nt_status;
228 struct auth_session_info *session_info = NULL;
229 nt_status = _auth_system_session_info(mem_ctx, lp_ctx, false, &session_info);
230 if (!NT_STATUS_IS_OK(nt_status)) {
231 return NULL;
233 return session_info;
238 _PUBLIC_ NTSTATUS auth_system_session_info(TALLOC_CTX *parent_ctx,
239 struct loadparm_context *lp_ctx,
240 struct auth_session_info **_session_info)
242 return _auth_system_session_info(parent_ctx,
243 lp_ctx,
244 lp_parm_bool(lp_ctx, NULL, "system", "anonymous", false),
245 _session_info);
248 NTSTATUS auth_system_server_info(TALLOC_CTX *mem_ctx, const char *netbios_name,
249 struct auth_serversupplied_info **_server_info)
251 struct auth_serversupplied_info *server_info;
253 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
254 NT_STATUS_HAVE_NO_MEMORY(server_info);
256 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_SYSTEM);
257 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
259 /* is this correct? */
260 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
261 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
263 server_info->n_domain_groups = 0;
264 server_info->domain_groups = NULL;
266 /* annoying, but the Anonymous really does have a session key,
267 and it is all zeros! */
268 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
269 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
271 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
272 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
274 data_blob_clear(&server_info->user_session_key);
275 data_blob_clear(&server_info->lm_session_key);
277 server_info->account_name = talloc_strdup(server_info, "SYSTEM");
278 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
280 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
281 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
283 server_info->full_name = talloc_strdup(server_info, "System");
284 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
286 server_info->logon_script = talloc_strdup(server_info, "");
287 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
289 server_info->profile_path = talloc_strdup(server_info, "");
290 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
292 server_info->home_directory = talloc_strdup(server_info, "");
293 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
295 server_info->home_drive = talloc_strdup(server_info, "");
296 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
298 server_info->logon_server = talloc_strdup(server_info, netbios_name);
299 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
301 server_info->last_logon = 0;
302 server_info->last_logoff = 0;
303 server_info->acct_expiry = 0;
304 server_info->last_password_change = 0;
305 server_info->allow_password_change = 0;
306 server_info->force_password_change = 0;
308 server_info->logon_count = 0;
309 server_info->bad_password_count = 0;
311 server_info->acct_flags = ACB_NORMAL;
313 server_info->authenticated = true;
315 *_server_info = server_info;
317 return NT_STATUS_OK;
321 /* Create server info for the Administrator account. This should only be used
322 * during provisioning when we need to impersonate Administrator but
323 * the account has not been created yet */
325 static NTSTATUS create_admin_token(TALLOC_CTX *mem_ctx,
326 struct dom_sid *user_sid,
327 struct dom_sid *group_sid,
328 unsigned int n_groupSIDs,
329 struct dom_sid **groupSIDs,
330 struct security_token **token)
332 struct security_token *ptoken;
333 unsigned int i;
335 ptoken = security_token_initialise(mem_ctx);
336 NT_STATUS_HAVE_NO_MEMORY(ptoken);
338 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 3);
339 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
341 ptoken->user_sid = talloc_reference(ptoken, user_sid);
342 ptoken->group_sid = talloc_reference(ptoken, group_sid);
343 ptoken->privilege_mask = 0;
345 ptoken->sids[0] = ptoken->user_sid;
346 ptoken->sids[1] = ptoken->group_sid;
347 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
348 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
349 ptoken->num_sids = 3;
352 for (i = 0; i < n_groupSIDs; i++) {
353 size_t check_sid_idx;
354 for (check_sid_idx = 1;
355 check_sid_idx < ptoken->num_sids;
356 check_sid_idx++) {
357 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
358 break;
362 if (check_sid_idx == ptoken->num_sids) {
363 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
367 *token = ptoken;
368 ptoken->privilege_mask = ~0;
369 return NT_STATUS_OK;
372 static NTSTATUS auth_domain_admin_server_info(TALLOC_CTX *mem_ctx,
373 const char *netbios_name,
374 const char *domain_name,
375 struct dom_sid *domain_sid,
376 struct auth_serversupplied_info **_server_info)
378 struct auth_serversupplied_info *server_info;
380 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
381 NT_STATUS_HAVE_NO_MEMORY(server_info);
383 server_info->account_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINISTRATOR);
384 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
386 server_info->primary_group_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
387 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
389 server_info->n_domain_groups = 6;
390 server_info->domain_groups = talloc_array(server_info, struct dom_sid *, server_info->n_domain_groups);
392 server_info->domain_groups[0] = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
393 server_info->domain_groups[1] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINS);
394 server_info->domain_groups[2] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
395 server_info->domain_groups[3] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
396 server_info->domain_groups[4] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_POLICY_ADMINS);
397 server_info->domain_groups[5] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
399 /* What should the session key be?*/
400 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
401 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
403 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
404 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
406 data_blob_clear(&server_info->user_session_key);
407 data_blob_clear(&server_info->lm_session_key);
409 server_info->account_name = talloc_strdup(server_info, "Administrator");
410 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
412 server_info->domain_name = talloc_strdup(server_info, domain_name);
413 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
415 server_info->full_name = talloc_strdup(server_info, "Administrator");
416 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
418 server_info->logon_script = talloc_strdup(server_info, "");
419 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
421 server_info->profile_path = talloc_strdup(server_info, "");
422 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
424 server_info->home_directory = talloc_strdup(server_info, "");
425 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
427 server_info->home_drive = talloc_strdup(server_info, "");
428 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
430 server_info->logon_server = talloc_strdup(server_info, netbios_name);
431 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
433 server_info->last_logon = 0;
434 server_info->last_logoff = 0;
435 server_info->acct_expiry = 0;
436 server_info->last_password_change = 0;
437 server_info->allow_password_change = 0;
438 server_info->force_password_change = 0;
440 server_info->logon_count = 0;
441 server_info->bad_password_count = 0;
443 server_info->acct_flags = ACB_NORMAL;
445 server_info->authenticated = true;
447 *_server_info = server_info;
449 return NT_STATUS_OK;
452 static NTSTATUS auth_domain_admin_session_info(TALLOC_CTX *parent_ctx,
453 struct loadparm_context *lp_ctx,
454 struct dom_sid *domain_sid,
455 struct auth_session_info **_session_info)
457 NTSTATUS nt_status;
458 struct auth_serversupplied_info *server_info = NULL;
459 struct auth_session_info *session_info = NULL;
460 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
462 nt_status = auth_domain_admin_server_info(mem_ctx, lp_netbios_name(lp_ctx),
463 lp_workgroup(lp_ctx), domain_sid,
464 &server_info);
465 if (!NT_STATUS_IS_OK(nt_status)) {
466 talloc_free(mem_ctx);
467 return nt_status;
470 session_info = talloc(mem_ctx, struct auth_session_info);
471 NT_STATUS_HAVE_NO_MEMORY(session_info);
473 session_info->server_info = talloc_reference(session_info, server_info);
475 /* unless set otherwise, the session key is the user session
476 * key from the auth subsystem */
477 session_info->session_key = server_info->user_session_key;
479 nt_status = create_admin_token(session_info,
480 server_info->account_sid,
481 server_info->primary_group_sid,
482 server_info->n_domain_groups,
483 server_info->domain_groups,
484 &session_info->security_token);
485 NT_STATUS_NOT_OK_RETURN(nt_status);
487 session_info->credentials = cli_credentials_init(session_info);
488 if (!session_info->credentials) {
489 return NT_STATUS_NO_MEMORY;
492 cli_credentials_set_conf(session_info->credentials, lp_ctx);
494 *_session_info = session_info;
496 return NT_STATUS_OK;
499 _PUBLIC_ struct auth_session_info *admin_session(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct dom_sid *domain_sid)
501 NTSTATUS nt_status;
502 struct auth_session_info *session_info = NULL;
503 nt_status = auth_domain_admin_session_info(mem_ctx,
504 lp_ctx,
505 domain_sid,
506 &session_info);
507 if (!NT_STATUS_IS_OK(nt_status)) {
508 return NULL;
510 return session_info;
513 _PUBLIC_ NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx,
514 struct loadparm_context *lp_ctx,
515 struct auth_session_info **_session_info)
517 NTSTATUS nt_status;
518 struct auth_serversupplied_info *server_info = NULL;
519 struct auth_session_info *session_info = NULL;
520 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
522 nt_status = auth_anonymous_server_info(mem_ctx,
523 lp_netbios_name(lp_ctx),
524 &server_info);
525 if (!NT_STATUS_IS_OK(nt_status)) {
526 talloc_free(mem_ctx);
527 return nt_status;
530 /* references the server_info into the session_info */
531 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
532 talloc_free(mem_ctx);
534 NT_STATUS_NOT_OK_RETURN(nt_status);
536 session_info->credentials = cli_credentials_init(session_info);
537 if (!session_info->credentials) {
538 return NT_STATUS_NO_MEMORY;
541 cli_credentials_set_conf(session_info->credentials, lp_ctx);
542 cli_credentials_set_anonymous(session_info->credentials);
544 *_session_info = session_info;
546 return NT_STATUS_OK;
549 _PUBLIC_ NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx,
550 const char *netbios_name,
551 struct auth_serversupplied_info **_server_info)
553 struct auth_serversupplied_info *server_info;
554 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
555 NT_STATUS_HAVE_NO_MEMORY(server_info);
557 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
558 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
560 /* is this correct? */
561 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
562 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
564 server_info->n_domain_groups = 0;
565 server_info->domain_groups = NULL;
567 /* annoying, but the Anonymous really does have a session key... */
568 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
569 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
571 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
572 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
574 /* and it is all zeros! */
575 data_blob_clear(&server_info->user_session_key);
576 data_blob_clear(&server_info->lm_session_key);
578 server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
579 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
581 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
582 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
584 server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
585 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
587 server_info->logon_script = talloc_strdup(server_info, "");
588 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
590 server_info->profile_path = talloc_strdup(server_info, "");
591 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
593 server_info->home_directory = talloc_strdup(server_info, "");
594 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
596 server_info->home_drive = talloc_strdup(server_info, "");
597 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
599 server_info->logon_server = talloc_strdup(server_info, netbios_name);
600 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
602 server_info->last_logon = 0;
603 server_info->last_logoff = 0;
604 server_info->acct_expiry = 0;
605 server_info->last_password_change = 0;
606 server_info->allow_password_change = 0;
607 server_info->force_password_change = 0;
609 server_info->logon_count = 0;
610 server_info->bad_password_count = 0;
612 server_info->acct_flags = ACB_NORMAL;
614 server_info->authenticated = false;
616 *_server_info = server_info;
618 return NT_STATUS_OK;