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/>.
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"
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;
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->sids[PRIMARY_USER_SID_INDEX] = talloc_reference(ptoken, user_sid);
55 ptoken->sids[PRIMARY_GROUP_SID_INDEX] = talloc_reference(ptoken, group_sid);
56 ptoken->privilege_mask = 0;
59 * Finally add the "standard" SIDs.
60 * The only difference between guest and "anonymous"
61 * is the addition of Authenticated_Users.
63 ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
64 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
65 ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
66 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
69 if (is_authenticated) {
70 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
71 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
75 for (i = 0; i < n_groupSIDs; i++) {
77 for (check_sid_idx = 1;
78 check_sid_idx < ptoken->num_sids;
80 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
85 if (check_sid_idx == ptoken->num_sids) {
86 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
92 /* Shortcuts to prevent recursion and avoid lookups */
93 if (ptoken->sids == NULL) {
94 ptoken->privilege_mask = 0;
98 if (security_token_is_system(ptoken)) {
99 ptoken->privilege_mask = ~0;
100 } else if (security_token_is_anonymous(ptoken)) {
101 ptoken->privilege_mask = 0;
102 } else if (security_token_has_builtin_administrators(ptoken)) {
103 ptoken->privilege_mask = ~0;
105 /* All other 'users' get a empty priv set so far */
106 ptoken->privilege_mask = 0;
111 NTSTATUS auth_generate_simple_session_info(TALLOC_CTX *mem_ctx,
112 struct auth_serversupplied_info *server_info,
113 struct auth_session_info **_session_info)
115 struct auth_session_info *session_info;
118 session_info = talloc(mem_ctx, struct auth_session_info);
119 NT_STATUS_HAVE_NO_MEMORY(session_info);
121 session_info->server_info = talloc_reference(session_info, server_info);
123 /* unless set otherwise, the session key is the user session
124 * key from the auth subsystem */
125 session_info->session_key = server_info->user_session_key;
127 nt_status = create_token(session_info,
128 server_info->account_sid,
129 server_info->primary_group_sid,
130 server_info->n_domain_groups,
131 server_info->domain_groups,
132 server_info->authenticated,
133 &session_info->security_token);
134 NT_STATUS_NOT_OK_RETURN(nt_status);
136 session_info->credentials = NULL;
138 *_session_info = session_info;
144 prevent the static system session being freed
146 static int system_session_destructor(struct auth_session_info *info)
151 /* Create a security token for a session SYSTEM (the most
152 * trusted/prvilaged account), including the local machine account as
153 * the off-host credentials
155 _PUBLIC_ struct auth_session_info *system_session(struct loadparm_context *lp_ctx)
157 static struct auth_session_info *static_session;
160 if (static_session) {
161 return static_session;
164 nt_status = auth_system_session_info(talloc_autofree_context(),
167 if (!NT_STATUS_IS_OK(nt_status)) {
168 talloc_free(static_session);
169 static_session = NULL;
172 talloc_set_destructor(static_session, system_session_destructor);
173 return static_session;
176 static NTSTATUS _auth_system_session_info(TALLOC_CTX *parent_ctx,
177 struct loadparm_context *lp_ctx,
178 struct auth_session_info **_session_info)
181 struct auth_serversupplied_info *server_info = NULL;
182 struct auth_session_info *session_info = NULL;
183 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
185 nt_status = auth_system_server_info(mem_ctx, lpcfg_netbios_name(lp_ctx),
187 if (!NT_STATUS_IS_OK(nt_status)) {
188 talloc_free(mem_ctx);
192 /* references the server_info into the session_info */
193 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
194 talloc_free(mem_ctx);
196 NT_STATUS_NOT_OK_RETURN(nt_status);
198 session_info->credentials = cli_credentials_init(session_info);
199 if (!session_info->credentials) {
200 return NT_STATUS_NO_MEMORY;
203 cli_credentials_set_conf(session_info->credentials, lp_ctx);
205 cli_credentials_set_machine_account_pending(session_info->credentials, lp_ctx);
206 *_session_info = session_info;
212 Create a system session, but with anonymous credentials (so we do not need to open secrets.ldb)
214 _PUBLIC_ struct auth_session_info *system_session_anon(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
217 struct auth_session_info *session_info = NULL;
218 nt_status = _auth_system_session_info(mem_ctx, lp_ctx, &session_info);
219 if (!NT_STATUS_IS_OK(nt_status)) {
227 _PUBLIC_ NTSTATUS auth_system_session_info(TALLOC_CTX *parent_ctx,
228 struct loadparm_context *lp_ctx,
229 struct auth_session_info **_session_info)
231 return _auth_system_session_info(parent_ctx,
236 NTSTATUS auth_system_server_info(TALLOC_CTX *mem_ctx, const char *netbios_name,
237 struct auth_serversupplied_info **_server_info)
239 struct auth_serversupplied_info *server_info;
241 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
242 NT_STATUS_HAVE_NO_MEMORY(server_info);
244 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_SYSTEM);
245 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
247 /* is this correct? */
248 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
249 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
251 server_info->n_domain_groups = 0;
252 server_info->domain_groups = NULL;
254 /* annoying, but the Anonymous really does have a session key,
255 and it is all zeros! */
256 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
257 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
259 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
260 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
262 data_blob_clear(&server_info->user_session_key);
263 data_blob_clear(&server_info->lm_session_key);
265 server_info->account_name = talloc_strdup(server_info, "SYSTEM");
266 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
268 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
269 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
271 server_info->full_name = talloc_strdup(server_info, "System");
272 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
274 server_info->logon_script = talloc_strdup(server_info, "");
275 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
277 server_info->profile_path = talloc_strdup(server_info, "");
278 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
280 server_info->home_directory = talloc_strdup(server_info, "");
281 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
283 server_info->home_drive = talloc_strdup(server_info, "");
284 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
286 server_info->logon_server = talloc_strdup(server_info, netbios_name);
287 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
289 server_info->last_logon = 0;
290 server_info->last_logoff = 0;
291 server_info->acct_expiry = 0;
292 server_info->last_password_change = 0;
293 server_info->allow_password_change = 0;
294 server_info->force_password_change = 0;
296 server_info->logon_count = 0;
297 server_info->bad_password_count = 0;
299 server_info->acct_flags = ACB_NORMAL;
301 server_info->authenticated = true;
303 *_server_info = server_info;
309 static NTSTATUS auth_domain_admin_server_info(TALLOC_CTX *mem_ctx,
310 const char *netbios_name,
311 const char *domain_name,
312 struct dom_sid *domain_sid,
313 struct auth_serversupplied_info **_server_info)
315 struct auth_serversupplied_info *server_info;
317 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
318 NT_STATUS_HAVE_NO_MEMORY(server_info);
320 server_info->account_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINISTRATOR);
321 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
323 server_info->primary_group_sid = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
324 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
326 server_info->n_domain_groups = 6;
327 server_info->domain_groups = talloc_array(server_info, struct dom_sid *, server_info->n_domain_groups);
329 server_info->domain_groups[0] = dom_sid_parse_talloc(server_info, SID_BUILTIN_ADMINISTRATORS);
330 server_info->domain_groups[1] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ADMINS);
331 server_info->domain_groups[2] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_USERS);
332 server_info->domain_groups[3] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_ENTERPRISE_ADMINS);
333 server_info->domain_groups[4] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_POLICY_ADMINS);
334 server_info->domain_groups[5] = dom_sid_add_rid(server_info, domain_sid, DOMAIN_RID_SCHEMA_ADMINS);
336 /* What should the session key be?*/
337 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
338 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
340 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
341 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
343 data_blob_clear(&server_info->user_session_key);
344 data_blob_clear(&server_info->lm_session_key);
346 server_info->account_name = talloc_strdup(server_info, "Administrator");
347 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
349 server_info->domain_name = talloc_strdup(server_info, domain_name);
350 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
352 server_info->full_name = talloc_strdup(server_info, "Administrator");
353 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
355 server_info->logon_script = talloc_strdup(server_info, "");
356 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
358 server_info->profile_path = talloc_strdup(server_info, "");
359 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
361 server_info->home_directory = talloc_strdup(server_info, "");
362 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
364 server_info->home_drive = talloc_strdup(server_info, "");
365 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
367 server_info->logon_server = talloc_strdup(server_info, netbios_name);
368 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
370 server_info->last_logon = 0;
371 server_info->last_logoff = 0;
372 server_info->acct_expiry = 0;
373 server_info->last_password_change = 0;
374 server_info->allow_password_change = 0;
375 server_info->force_password_change = 0;
377 server_info->logon_count = 0;
378 server_info->bad_password_count = 0;
380 server_info->acct_flags = ACB_NORMAL;
382 server_info->authenticated = true;
384 *_server_info = server_info;
389 static NTSTATUS auth_domain_admin_session_info(TALLOC_CTX *parent_ctx,
390 struct loadparm_context *lp_ctx,
391 struct dom_sid *domain_sid,
392 struct auth_session_info **_session_info)
395 struct auth_serversupplied_info *server_info = NULL;
396 struct auth_session_info *session_info = NULL;
397 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
399 nt_status = auth_domain_admin_server_info(mem_ctx, lpcfg_netbios_name(lp_ctx),
400 lpcfg_workgroup(lp_ctx), domain_sid,
402 if (!NT_STATUS_IS_OK(nt_status)) {
403 talloc_free(mem_ctx);
407 session_info = talloc(mem_ctx, struct auth_session_info);
408 NT_STATUS_HAVE_NO_MEMORY(session_info);
410 session_info->server_info = talloc_reference(session_info, server_info);
412 /* unless set otherwise, the session key is the user session
413 * key from the auth subsystem */
414 session_info->session_key = server_info->user_session_key;
416 nt_status = create_token(session_info,
417 server_info->account_sid,
418 server_info->primary_group_sid,
419 server_info->n_domain_groups,
420 server_info->domain_groups,
422 &session_info->security_token);
423 NT_STATUS_NOT_OK_RETURN(nt_status);
425 session_info->credentials = cli_credentials_init(session_info);
426 if (!session_info->credentials) {
427 return NT_STATUS_NO_MEMORY;
430 cli_credentials_set_conf(session_info->credentials, lp_ctx);
432 *_session_info = session_info;
437 _PUBLIC_ struct auth_session_info *admin_session(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct dom_sid *domain_sid)
440 struct auth_session_info *session_info = NULL;
441 nt_status = auth_domain_admin_session_info(mem_ctx,
445 if (!NT_STATUS_IS_OK(nt_status)) {
451 _PUBLIC_ NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx,
452 struct loadparm_context *lp_ctx,
453 struct auth_session_info **_session_info)
456 struct auth_serversupplied_info *server_info = NULL;
457 struct auth_session_info *session_info = NULL;
458 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
460 nt_status = auth_anonymous_server_info(mem_ctx,
461 lpcfg_netbios_name(lp_ctx),
463 if (!NT_STATUS_IS_OK(nt_status)) {
464 talloc_free(mem_ctx);
468 /* references the server_info into the session_info */
469 nt_status = auth_generate_simple_session_info(parent_ctx, server_info, &session_info);
470 talloc_free(mem_ctx);
472 NT_STATUS_NOT_OK_RETURN(nt_status);
474 session_info->credentials = cli_credentials_init(session_info);
475 if (!session_info->credentials) {
476 return NT_STATUS_NO_MEMORY;
479 cli_credentials_set_conf(session_info->credentials, lp_ctx);
480 cli_credentials_set_anonymous(session_info->credentials);
482 *_session_info = session_info;
487 _PUBLIC_ NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx,
488 const char *netbios_name,
489 struct auth_serversupplied_info **_server_info)
491 struct auth_serversupplied_info *server_info;
492 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
493 NT_STATUS_HAVE_NO_MEMORY(server_info);
495 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
496 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
498 /* is this correct? */
499 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
500 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
502 server_info->n_domain_groups = 0;
503 server_info->domain_groups = NULL;
505 /* annoying, but the Anonymous really does have a session key... */
506 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
507 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
509 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
510 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
512 /* and it is all zeros! */
513 data_blob_clear(&server_info->user_session_key);
514 data_blob_clear(&server_info->lm_session_key);
516 server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
517 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
519 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
520 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
522 server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
523 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
525 server_info->logon_script = talloc_strdup(server_info, "");
526 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
528 server_info->profile_path = talloc_strdup(server_info, "");
529 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
531 server_info->home_directory = talloc_strdup(server_info, "");
532 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
534 server_info->home_drive = talloc_strdup(server_info, "");
535 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
537 server_info->logon_server = talloc_strdup(server_info, netbios_name);
538 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
540 server_info->last_logon = 0;
541 server_info->last_logoff = 0;
542 server_info->acct_expiry = 0;
543 server_info->last_password_change = 0;
544 server_info->allow_password_change = 0;
545 server_info->force_password_change = 0;
547 server_info->logon_count = 0;
548 server_info->bad_password_count = 0;
550 server_info->acct_flags = ACB_NORMAL;
552 server_info->authenticated = false;
554 *_server_info = server_info;