pidl:Python: improve the .doc string for the get/set elements
[Samba.git] / source3 / auth / server_info.c
blobd2b7823aa5438415427bd61616fe4bd141e22539
1 /*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "auth.h"
22 #include "../lib/crypto/arcfour.h"
23 #include "../librpc/gen_ndr/netlogon.h"
24 #include "../libcli/security/security.h"
25 #include "rpc_client/util_netlogon.h"
26 #include "nsswitch/libwbclient/wbclient.h"
27 #include "lib/winbind_util.h"
28 #include "passdb.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_AUTH
33 /***************************************************************************
34 Make a server_info struct. Free with TALLOC_FREE().
35 ***************************************************************************/
37 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
39 struct auth_serversupplied_info *result;
41 result = talloc_zero(mem_ctx, struct auth_serversupplied_info);
42 if (result == NULL) {
43 DEBUG(0, ("talloc failed\n"));
44 return NULL;
47 /* Initialise the uid and gid values to something non-zero
48 which may save us from giving away root access if there
49 is a bug in allocating these fields. */
51 result->utok.uid = -1;
52 result->utok.gid = -1;
54 return result;
57 /****************************************************************************
58 inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
59 already be initialized and is used as the talloc parent for its members.
60 *****************************************************************************/
62 NTSTATUS serverinfo_to_SamInfo2(struct auth_serversupplied_info *server_info,
63 struct netr_SamInfo2 *sam2)
65 struct netr_SamInfo3 *info3;
67 info3 = copy_netr_SamInfo3(sam2, server_info->info3);
68 if (!info3) {
69 return NT_STATUS_NO_MEMORY;
72 if (server_info->session_key.length) {
73 memcpy(info3->base.key.key,
74 server_info->session_key.data,
75 MIN(sizeof(info3->base.key.key),
76 server_info->session_key.length));
78 if (server_info->lm_session_key.length) {
79 memcpy(info3->base.LMSessKey.key,
80 server_info->lm_session_key.data,
81 MIN(sizeof(info3->base.LMSessKey.key),
82 server_info->lm_session_key.length));
85 sam2->base = info3->base;
87 return NT_STATUS_OK;
90 /****************************************************************************
91 inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
92 already be initialized and is used as the talloc parent for its members.
93 *****************************************************************************/
95 NTSTATUS serverinfo_to_SamInfo3(const struct auth_serversupplied_info *server_info,
96 struct netr_SamInfo3 *sam3)
98 struct netr_SamInfo3 *info3;
100 info3 = copy_netr_SamInfo3(sam3, server_info->info3);
101 if (!info3) {
102 return NT_STATUS_NO_MEMORY;
105 if (server_info->session_key.length) {
106 memcpy(info3->base.key.key,
107 server_info->session_key.data,
108 MIN(sizeof(info3->base.key.key),
109 server_info->session_key.length));
111 if (server_info->lm_session_key.length) {
112 memcpy(info3->base.LMSessKey.key,
113 server_info->lm_session_key.data,
114 MIN(sizeof(info3->base.LMSessKey.key),
115 server_info->lm_session_key.length));
118 sam3->base = info3->base;
120 sam3->sidcount = 0;
121 sam3->sids = NULL;
123 return NT_STATUS_OK;
126 /****************************************************************************
127 inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
128 already be initialized and is used as the talloc parent for its members.
129 *****************************************************************************/
131 NTSTATUS serverinfo_to_SamInfo6(struct auth_serversupplied_info *server_info,
132 struct netr_SamInfo6 *sam6)
134 struct pdb_domain_info *dominfo;
135 struct netr_SamInfo3 *info3;
137 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
138 DEBUG(10,("Not adding validation info level 6 "
139 "without ADS passdb backend\n"));
140 return NT_STATUS_INVALID_INFO_CLASS;
143 dominfo = pdb_get_domain_info(sam6);
144 if (dominfo == NULL) {
145 return NT_STATUS_NO_MEMORY;
148 info3 = copy_netr_SamInfo3(sam6, server_info->info3);
149 if (!info3) {
150 return NT_STATUS_NO_MEMORY;
153 if (server_info->session_key.length) {
154 memcpy(info3->base.key.key,
155 server_info->session_key.data,
156 MIN(sizeof(info3->base.key.key),
157 server_info->session_key.length));
159 if (server_info->lm_session_key.length) {
160 memcpy(info3->base.LMSessKey.key,
161 server_info->lm_session_key.data,
162 MIN(sizeof(info3->base.LMSessKey.key),
163 server_info->lm_session_key.length));
166 sam6->base = info3->base;
168 sam6->sidcount = 0;
169 sam6->sids = NULL;
171 sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
172 if (sam6->dns_domainname.string == NULL) {
173 return NT_STATUS_NO_MEMORY;
176 sam6->principal_name.string = talloc_asprintf(
177 sam6, "%s@%s", sam6->base.account_name.string,
178 sam6->dns_domainname.string);
179 if (sam6->principal_name.string == NULL) {
180 return NT_STATUS_NO_MEMORY;
183 return NT_STATUS_OK;
186 static NTSTATUS append_netr_SidAttr(TALLOC_CTX *mem_ctx,
187 struct netr_SidAttr **sids,
188 uint32_t *count,
189 const struct dom_sid2 *asid,
190 uint32_t attributes)
192 uint32_t t = *count;
194 *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
195 if (*sids == NULL) {
196 return NT_STATUS_NO_MEMORY;
198 (*sids)[t].sid = dom_sid_dup(*sids, asid);
199 if ((*sids)[t].sid == NULL) {
200 return NT_STATUS_NO_MEMORY;
202 (*sids)[t].attributes = attributes;
203 *count = t + 1;
205 return NT_STATUS_OK;
208 /* Fills the samr_RidWithAttributeArray with the provided sids.
209 * If it happens that we have additional groups that do not belong
210 * to the domain, add their sids as extra sids */
211 static NTSTATUS group_sids_to_info3(struct netr_SamInfo3 *info3,
212 const struct dom_sid *sids,
213 size_t num_sids)
215 uint32_t attributes = SE_GROUP_MANDATORY |
216 SE_GROUP_ENABLED_BY_DEFAULT |
217 SE_GROUP_ENABLED;
218 struct samr_RidWithAttributeArray *groups;
219 struct dom_sid *domain_sid;
220 unsigned int i;
221 NTSTATUS status;
222 uint32_t rid;
223 bool ok;
225 domain_sid = info3->base.domain_sid;
226 groups = &info3->base.groups;
228 groups->rids = talloc_array(info3,
229 struct samr_RidWithAttribute, num_sids);
230 if (!groups->rids) {
231 return NT_STATUS_NO_MEMORY;
234 for (i = 0; i < num_sids; i++) {
235 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
236 if (ok) {
237 /* store domain group rid */
238 groups->rids[groups->count].rid = rid;
239 groups->rids[groups->count].attributes = attributes;
240 groups->count++;
241 continue;
244 /* if this wasn't a domain sid, add it as extra sid */
245 status = append_netr_SidAttr(info3, &info3->sids,
246 &info3->sidcount,
247 &sids[i], attributes);
248 if (!NT_STATUS_IS_OK(status)) {
249 return status;
253 return NT_STATUS_OK;
257 * Merge resource SIDs, if any, into the passed in info3 structure.
260 static NTSTATUS merge_resource_sids(const struct PAC_LOGON_INFO *logon_info,
261 struct netr_SamInfo3 *info3)
263 uint32_t i = 0;
264 const struct PAC_DOMAIN_GROUP_MEMBERSHIP *rg = NULL;
266 if (logon_info->info3.base.user_flags & NETLOGON_RESOURCE_GROUPS) {
267 rg = &logon_info->resource_groups;
270 if (rg == NULL) {
271 return NT_STATUS_OK;
274 if (rg->domain_sid == NULL) {
275 DEBUG(10, ("Missing Resource Group Domain SID\n"));
276 return NT_STATUS_INVALID_PARAMETER;
279 /* The IDL layer would be a better place to check this, but to
280 * guard the integer addition below, we double-check */
281 if (rg->groups.count > 65535) {
282 DEBUG(10, ("Too much Resource Group RIDs %u\n",
283 (unsigned)rg->groups.count));
284 return NT_STATUS_INVALID_PARAMETER;
288 * If there are any resource groups (SID Compression) add
289 * them to the extra sids portion of the info3 in the PAC.
291 * This makes the info3 look like it would if we got the info
292 * from the DC rather than the PAC.
296 * Construct a SID for each RID in the list and then append it
297 * to the info3.
299 for (i = 0; i < rg->groups.count; i++) {
300 NTSTATUS status;
301 struct dom_sid new_sid;
302 uint32_t attributes = rg->groups.rids[i].attributes;
304 sid_compose(&new_sid,
305 rg->domain_sid,
306 rg->groups.rids[i].rid);
308 DEBUG(10, ("Adding SID %s to extra SIDS\n",
309 sid_string_dbg(&new_sid)));
311 status = append_netr_SidAttr(info3, &info3->sids,
312 &info3->sidcount,
313 &new_sid,
314 attributes);
315 if (!NT_STATUS_IS_OK(status)) {
316 DEBUG(1, ("failed to append SID %s to extra SIDS: %s\n",
317 sid_string_dbg(&new_sid),
318 nt_errstr(status)));
319 return status;
323 return NT_STATUS_OK;
327 * Create a copy of an info3 struct from the struct PAC_LOGON_INFO,
328 * then merge resource SIDs, if any, into it. If successful return
329 * the created info3 struct.
332 NTSTATUS create_info3_from_pac_logon_info(TALLOC_CTX *mem_ctx,
333 const struct PAC_LOGON_INFO *logon_info,
334 struct netr_SamInfo3 **pp_info3)
336 NTSTATUS status;
337 struct netr_SamInfo3 *info3 = copy_netr_SamInfo3(mem_ctx,
338 &logon_info->info3);
339 if (info3 == NULL) {
340 return NT_STATUS_NO_MEMORY;
342 status = merge_resource_sids(logon_info, info3);
343 if (!NT_STATUS_IS_OK(status)) {
344 TALLOC_FREE(info3);
345 return status;
347 *pp_info3 = info3;
348 return NT_STATUS_OK;
352 * Check if this is a "Unix Users" domain user, or a
353 * "Unix Groups" domain group, we need to handle it
354 * in a special way if that's the case.
357 static NTSTATUS SamInfo3_handle_sids(const char *username,
358 const struct dom_sid *user_sid,
359 const struct dom_sid *group_sid,
360 struct netr_SamInfo3 *info3,
361 struct dom_sid *domain_sid,
362 struct extra_auth_info *extra)
364 if (sid_check_is_in_unix_users(user_sid)) {
365 /* in info3 you can only set rids for the user and the
366 * primary group, and the domain sid must be that of
367 * the sam domain.
369 * Store a completely bogus value here.
370 * The real SID is stored in the extra sids.
371 * Other code will know to look there if (-1) is found
373 info3->base.rid = (uint32_t)(-1);
374 sid_copy(&extra->user_sid, user_sid);
376 DEBUG(10, ("Unix User found. Rid marked as "
377 "special and sid (%s) saved as extra sid\n",
378 sid_string_dbg(user_sid)));
379 } else {
380 sid_copy(domain_sid, user_sid);
381 sid_split_rid(domain_sid, &info3->base.rid);
384 if (is_null_sid(domain_sid)) {
385 sid_copy(domain_sid, get_global_sam_sid());
388 /* check if this is a "Unix Groups" domain group,
389 * if so we need special handling */
390 if (sid_check_is_in_unix_groups(group_sid)) {
391 /* in info3 you can only set rids for the user and the
392 * primary group, and the domain sid must be that of
393 * the sam domain.
395 * Store a completely bogus value here.
396 * The real SID is stored in the extra sids.
397 * Other code will know to look there if (-1) is found
399 info3->base.primary_gid = (uint32_t)(-1);
400 sid_copy(&extra->pgid_sid, group_sid);
402 DEBUG(10, ("Unix Group found. Rid marked as "
403 "special and sid (%s) saved as extra sid\n",
404 sid_string_dbg(group_sid)));
405 } else {
406 bool ok = sid_peek_check_rid(domain_sid, group_sid,
407 &info3->base.primary_gid);
408 if (!ok) {
409 DEBUG(1, ("The primary group domain sid(%s) does not "
410 "match the domain sid(%s) for %s(%s)\n",
411 sid_string_dbg(group_sid),
412 sid_string_dbg(domain_sid),
413 username,
414 sid_string_dbg(user_sid)));
415 return NT_STATUS_INVALID_SID;
418 return NT_STATUS_OK;
421 #define RET_NOMEM(ptr) do { \
422 if (!ptr) { \
423 TALLOC_FREE(info3); \
424 return NT_STATUS_NO_MEMORY; \
425 } } while(0)
427 NTSTATUS samu_to_SamInfo3(TALLOC_CTX *mem_ctx,
428 struct samu *samu,
429 const char *login_server,
430 struct netr_SamInfo3 **_info3,
431 struct extra_auth_info *extra)
433 struct netr_SamInfo3 *info3;
434 const struct dom_sid *user_sid;
435 const struct dom_sid *group_sid;
436 struct dom_sid domain_sid;
437 struct dom_sid *group_sids;
438 uint32_t num_group_sids = 0;
439 const char *tmp;
440 gid_t *gids;
441 NTSTATUS status;
443 user_sid = pdb_get_user_sid(samu);
444 group_sid = pdb_get_group_sid(samu);
446 if (!user_sid || !group_sid) {
447 DEBUG(1, ("Sam account is missing sids!\n"));
448 return NT_STATUS_UNSUCCESSFUL;
451 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
452 if (!info3) {
453 return NT_STATUS_NO_MEMORY;
456 ZERO_STRUCT(domain_sid);
458 status = SamInfo3_handle_sids(pdb_get_username(samu),
459 user_sid,
460 group_sid,
461 info3,
462 &domain_sid,
463 extra);
465 if (!NT_STATUS_IS_OK(status)) {
466 TALLOC_FREE(info3);
467 return status;
470 unix_to_nt_time(&info3->base.logon_time, pdb_get_logon_time(samu));
471 unix_to_nt_time(&info3->base.logoff_time, get_time_t_max());
472 unix_to_nt_time(&info3->base.kickoff_time, get_time_t_max());
473 unix_to_nt_time(&info3->base.last_password_change,
474 pdb_get_pass_last_set_time(samu));
475 unix_to_nt_time(&info3->base.allow_password_change,
476 pdb_get_pass_can_change_time(samu));
477 unix_to_nt_time(&info3->base.force_password_change,
478 pdb_get_pass_must_change_time(samu));
480 tmp = pdb_get_username(samu);
481 if (tmp) {
482 info3->base.account_name.string = talloc_strdup(info3, tmp);
483 RET_NOMEM(info3->base.account_name.string);
485 tmp = pdb_get_fullname(samu);
486 if (tmp) {
487 info3->base.full_name.string = talloc_strdup(info3, tmp);
488 RET_NOMEM(info3->base.full_name.string);
490 tmp = pdb_get_logon_script(samu);
491 if (tmp) {
492 info3->base.logon_script.string = talloc_strdup(info3, tmp);
493 RET_NOMEM(info3->base.logon_script.string);
495 tmp = pdb_get_profile_path(samu);
496 if (tmp) {
497 info3->base.profile_path.string = talloc_strdup(info3, tmp);
498 RET_NOMEM(info3->base.profile_path.string);
500 tmp = pdb_get_homedir(samu);
501 if (tmp) {
502 info3->base.home_directory.string = talloc_strdup(info3, tmp);
503 RET_NOMEM(info3->base.home_directory.string);
505 tmp = pdb_get_dir_drive(samu);
506 if (tmp) {
507 info3->base.home_drive.string = talloc_strdup(info3, tmp);
508 RET_NOMEM(info3->base.home_drive.string);
511 info3->base.logon_count = pdb_get_logon_count(samu);
512 info3->base.bad_password_count = pdb_get_bad_password_count(samu);
514 info3->base.logon_domain.string = talloc_strdup(info3,
515 pdb_get_domain(samu));
516 RET_NOMEM(info3->base.logon_domain.string);
518 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
519 RET_NOMEM(info3->base.domain_sid);
521 status = pdb_enum_group_memberships(mem_ctx, samu,
522 &group_sids, &gids,
523 &num_group_sids);
524 if (!NT_STATUS_IS_OK(status)) {
525 DEBUG(1, ("Failed to get groups from sam account.\n"));
526 TALLOC_FREE(info3);
527 return status;
530 if (num_group_sids) {
531 status = group_sids_to_info3(info3, group_sids, num_group_sids);
532 if (!NT_STATUS_IS_OK(status)) {
533 TALLOC_FREE(info3);
534 return status;
538 /* We don't need sids and gids after the conversion */
539 TALLOC_FREE(group_sids);
540 TALLOC_FREE(gids);
541 num_group_sids = 0;
543 /* FIXME: should we add other flags ? */
544 info3->base.user_flags = NETLOGON_EXTRA_SIDS;
546 if (login_server) {
547 info3->base.logon_server.string = talloc_strdup(info3, login_server);
548 RET_NOMEM(info3->base.logon_server.string);
551 info3->base.acct_flags = pdb_get_acct_ctrl(samu);
553 *_info3 = info3;
554 return NT_STATUS_OK;
557 NTSTATUS passwd_to_SamInfo3(TALLOC_CTX *mem_ctx,
558 const char *unix_username,
559 const struct passwd *pwd,
560 struct netr_SamInfo3 **pinfo3,
561 struct extra_auth_info *extra)
563 struct netr_SamInfo3 *info3;
564 NTSTATUS status;
565 TALLOC_CTX *tmp_ctx;
566 const char *domain_name = NULL;
567 const char *user_name = NULL;
568 struct dom_sid domain_sid;
569 struct dom_sid user_sid;
570 struct dom_sid group_sid;
571 enum lsa_SidType type;
572 uint32_t num_sids = 0;
573 struct dom_sid *user_sids = NULL;
574 bool is_null;
575 bool ok;
577 tmp_ctx = talloc_stackframe();
579 ok = lookup_name_smbconf(tmp_ctx,
580 unix_username,
581 LOOKUP_NAME_ALL,
582 &domain_name,
583 &user_name,
584 &user_sid,
585 &type);
586 if (!ok) {
587 status = NT_STATUS_NO_SUCH_USER;
588 goto done;
591 if (type != SID_NAME_USER) {
592 status = NT_STATUS_NO_SUCH_USER;
593 goto done;
596 ok = winbind_lookup_usersids(tmp_ctx,
597 &user_sid,
598 &num_sids,
599 &user_sids);
600 /* Check if winbind is running */
601 if (ok) {
603 * Winbind is running and the first element of the user_sids
604 * is the primary group.
606 if (num_sids > 0) {
607 group_sid = user_sids[0];
609 } else {
611 * Winbind is not running, try to create the group_sid from the
612 * passwd group id.
616 * This can lead to a primary group of S-1-22-2-XX which
617 * will be rejected by other Samba code.
619 gid_to_sid(&group_sid, pwd->pw_gid);
623 * If we are a unix group, or a wellknown/builtin alias,
624 * set the group_sid to the
625 * 'Domain Users' RID of 513 which will always resolve to a
626 * name.
628 if (sid_check_is_in_unix_groups(&group_sid) ||
629 sid_check_is_in_builtin(&group_sid) ||
630 sid_check_is_in_wellknown_domain(&group_sid)) {
631 if (sid_check_is_in_unix_users(&user_sid)) {
632 sid_compose(&group_sid,
633 get_global_sam_sid(),
634 DOMAIN_RID_USERS);
635 } else {
636 sid_copy(&domain_sid, &user_sid);
637 sid_split_rid(&domain_sid, NULL);
638 sid_compose(&group_sid,
639 &domain_sid,
640 DOMAIN_RID_USERS);
644 /* Make sure we have a valid group sid */
645 is_null = is_null_sid(&group_sid);
646 if (is_null) {
647 status = NT_STATUS_NO_SUCH_USER;
648 goto done;
651 /* Construct a netr_SamInfo3 from the information we have */
652 info3 = talloc_zero(tmp_ctx, struct netr_SamInfo3);
653 if (!info3) {
654 status = NT_STATUS_NO_MEMORY;
655 goto done;
658 info3->base.account_name.string = talloc_strdup(info3, unix_username);
659 if (info3->base.account_name.string == NULL) {
660 status = NT_STATUS_NO_MEMORY;
661 goto done;
664 ZERO_STRUCT(domain_sid);
666 status = SamInfo3_handle_sids(unix_username,
667 &user_sid,
668 &group_sid,
669 info3,
670 &domain_sid,
671 extra);
673 if (!NT_STATUS_IS_OK(status)) {
674 goto done;
677 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
678 if (info3->base.domain_sid == NULL) {
679 status = NT_STATUS_NO_MEMORY;
680 goto done;
683 ok = sid_peek_check_rid(&domain_sid, &group_sid,
684 &info3->base.primary_gid);
685 if (!ok) {
686 DEBUG(1, ("The primary group domain sid(%s) does not "
687 "match the domain sid(%s) for %s(%s)\n",
688 sid_string_dbg(&group_sid),
689 sid_string_dbg(&domain_sid),
690 unix_username,
691 sid_string_dbg(&user_sid)));
692 status = NT_STATUS_INVALID_SID;
693 goto done;
696 info3->base.acct_flags = ACB_NORMAL;
698 if (num_sids) {
699 status = group_sids_to_info3(info3, user_sids, num_sids);
700 if (!NT_STATUS_IS_OK(status)) {
701 goto done;
705 *pinfo3 = talloc_steal(mem_ctx, info3);
707 status = NT_STATUS_OK;
708 done:
709 talloc_free(tmp_ctx);
711 return status;
714 #undef RET_NOMEM
716 #define RET_NOMEM(ptr) do { \
717 if (!ptr) { \
718 TALLOC_FREE(info3); \
719 return NULL; \
720 } } while(0)
722 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
723 const struct netr_SamInfo3 *orig)
725 struct netr_SamInfo3 *info3;
726 unsigned int i;
727 NTSTATUS status;
729 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
730 if (!info3) return NULL;
732 status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
733 if (!NT_STATUS_IS_OK(status)) {
734 TALLOC_FREE(info3);
735 return NULL;
738 if (orig->sidcount) {
739 info3->sidcount = orig->sidcount;
740 info3->sids = talloc_array(info3, struct netr_SidAttr,
741 orig->sidcount);
742 RET_NOMEM(info3->sids);
743 for (i = 0; i < orig->sidcount; i++) {
744 info3->sids[i].sid = dom_sid_dup(info3->sids,
745 orig->sids[i].sid);
746 RET_NOMEM(info3->sids[i].sid);
747 info3->sids[i].attributes =
748 orig->sids[i].attributes;
752 return info3;