vfs: Make function pointer names consistent. They all end in _fn
[Samba/gebeck_regimport.git] / source3 / auth / server_info.c
blob6c2723d699f6882d8e153ed724d51a06978e8bd8
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 "passdb.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
32 /***************************************************************************
33 Make a server_info struct. Free with TALLOC_FREE().
34 ***************************************************************************/
36 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
38 struct auth_serversupplied_info *result;
40 result = talloc_zero(mem_ctx, struct auth_serversupplied_info);
41 if (result == NULL) {
42 DEBUG(0, ("talloc failed\n"));
43 return NULL;
46 /* Initialise the uid and gid values to something non-zero
47 which may save us from giving away root access if there
48 is a bug in allocating these fields. */
50 result->utok.uid = -1;
51 result->utok.gid = -1;
53 return result;
56 /****************************************************************************
57 inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
58 already be initialized and is used as the talloc parent for its members.
59 *****************************************************************************/
61 NTSTATUS serverinfo_to_SamInfo2(struct auth_serversupplied_info *server_info,
62 uint8_t *pipe_session_key,
63 size_t pipe_session_key_len,
64 struct netr_SamInfo2 *sam2)
66 struct netr_SamInfo3 *info3;
68 info3 = copy_netr_SamInfo3(sam2, server_info->info3);
69 if (!info3) {
70 return NT_STATUS_NO_MEMORY;
73 if (server_info->session_key.length) {
74 memcpy(info3->base.key.key,
75 server_info->session_key.data,
76 MIN(sizeof(info3->base.key.key),
77 server_info->session_key.length));
78 if (pipe_session_key) {
79 arcfour_crypt(info3->base.key.key,
80 pipe_session_key, 16);
83 if (server_info->lm_session_key.length) {
84 memcpy(info3->base.LMSessKey.key,
85 server_info->lm_session_key.data,
86 MIN(sizeof(info3->base.LMSessKey.key),
87 server_info->lm_session_key.length));
88 if (pipe_session_key) {
89 arcfour_crypt(info3->base.LMSessKey.key,
90 pipe_session_key, 8);
94 sam2->base = info3->base;
96 return NT_STATUS_OK;
99 /****************************************************************************
100 inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
101 already be initialized and is used as the talloc parent for its members.
102 *****************************************************************************/
104 NTSTATUS serverinfo_to_SamInfo3(const struct auth_serversupplied_info *server_info,
105 uint8_t *pipe_session_key,
106 size_t pipe_session_key_len,
107 struct netr_SamInfo3 *sam3)
109 struct netr_SamInfo3 *info3;
111 info3 = copy_netr_SamInfo3(sam3, server_info->info3);
112 if (!info3) {
113 return NT_STATUS_NO_MEMORY;
116 if (server_info->session_key.length) {
117 memcpy(info3->base.key.key,
118 server_info->session_key.data,
119 MIN(sizeof(info3->base.key.key),
120 server_info->session_key.length));
121 if (pipe_session_key) {
122 arcfour_crypt(info3->base.key.key,
123 pipe_session_key, 16);
126 if (server_info->lm_session_key.length) {
127 memcpy(info3->base.LMSessKey.key,
128 server_info->lm_session_key.data,
129 MIN(sizeof(info3->base.LMSessKey.key),
130 server_info->lm_session_key.length));
131 if (pipe_session_key) {
132 arcfour_crypt(info3->base.LMSessKey.key,
133 pipe_session_key, 8);
137 sam3->base = info3->base;
139 sam3->sidcount = 0;
140 sam3->sids = NULL;
142 return NT_STATUS_OK;
145 /****************************************************************************
146 inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
147 already be initialized and is used as the talloc parent for its members.
148 *****************************************************************************/
150 NTSTATUS serverinfo_to_SamInfo6(struct auth_serversupplied_info *server_info,
151 uint8_t *pipe_session_key,
152 size_t pipe_session_key_len,
153 struct netr_SamInfo6 *sam6)
155 struct pdb_domain_info *dominfo;
156 struct netr_SamInfo3 *info3;
158 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
159 DEBUG(10,("Not adding validation info level 6 "
160 "without ADS passdb backend\n"));
161 return NT_STATUS_INVALID_INFO_CLASS;
164 dominfo = pdb_get_domain_info(sam6);
165 if (dominfo == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 info3 = copy_netr_SamInfo3(sam6, server_info->info3);
170 if (!info3) {
171 return NT_STATUS_NO_MEMORY;
174 if (server_info->session_key.length) {
175 memcpy(info3->base.key.key,
176 server_info->session_key.data,
177 MIN(sizeof(info3->base.key.key),
178 server_info->session_key.length));
179 if (pipe_session_key) {
180 arcfour_crypt(info3->base.key.key,
181 pipe_session_key, 16);
184 if (server_info->lm_session_key.length) {
185 memcpy(info3->base.LMSessKey.key,
186 server_info->lm_session_key.data,
187 MIN(sizeof(info3->base.LMSessKey.key),
188 server_info->lm_session_key.length));
189 if (pipe_session_key) {
190 arcfour_crypt(info3->base.LMSessKey.key,
191 pipe_session_key, 8);
195 sam6->base = info3->base;
197 sam6->sidcount = 0;
198 sam6->sids = NULL;
200 sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
201 if (sam6->dns_domainname.string == NULL) {
202 return NT_STATUS_NO_MEMORY;
205 sam6->principle.string = talloc_asprintf(sam6, "%s@%s",
206 sam6->base.account_name.string,
207 sam6->dns_domainname.string);
208 if (sam6->principle.string == NULL) {
209 return NT_STATUS_NO_MEMORY;
212 return NT_STATUS_OK;
215 static NTSTATUS append_netr_SidAttr(TALLOC_CTX *mem_ctx,
216 struct netr_SidAttr **sids,
217 uint32_t *count,
218 const struct dom_sid2 *asid,
219 uint32_t attributes)
221 uint32_t t = *count;
223 *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
224 if (*sids == NULL) {
225 return NT_STATUS_NO_MEMORY;
227 (*sids)[t].sid = dom_sid_dup(*sids, asid);
228 if ((*sids)[t].sid == NULL) {
229 return NT_STATUS_NO_MEMORY;
231 (*sids)[t].attributes = attributes;
232 *count = t + 1;
234 return NT_STATUS_OK;
237 /* Fills the samr_RidWithAttributeArray with the provided sids.
238 * If it happens that we have additional groups that do not belong
239 * to the domain, add their sids as extra sids */
240 static NTSTATUS group_sids_to_info3(struct netr_SamInfo3 *info3,
241 const struct dom_sid *sids,
242 size_t num_sids)
244 uint32_t attributes = SE_GROUP_MANDATORY |
245 SE_GROUP_ENABLED_BY_DEFAULT |
246 SE_GROUP_ENABLED;
247 struct samr_RidWithAttributeArray *groups;
248 struct dom_sid *domain_sid;
249 unsigned int i;
250 NTSTATUS status;
251 uint32_t rid;
252 bool ok;
254 domain_sid = info3->base.domain_sid;
255 groups = &info3->base.groups;
257 groups->rids = talloc_array(info3,
258 struct samr_RidWithAttribute, num_sids);
259 if (!groups->rids) {
260 return NT_STATUS_NO_MEMORY;
263 for (i = 0; i < num_sids; i++) {
264 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
265 if (ok) {
267 /* if it is the primary gid, skip it, we
268 * obviously already have it */
269 if (info3->base.primary_gid == rid) continue;
271 /* store domain group rid */
272 groups->rids[groups->count].rid = rid;
273 groups->rids[groups->count].attributes = attributes;
274 groups->count++;
275 continue;
278 /* if this wasn't a domain sid, add it as extra sid */
279 status = append_netr_SidAttr(info3, &info3->sids,
280 &info3->sidcount,
281 &sids[i], attributes);
282 if (!NT_STATUS_IS_OK(status)) {
283 return status;
287 return NT_STATUS_OK;
290 #define RET_NOMEM(ptr) do { \
291 if (!ptr) { \
292 TALLOC_FREE(info3); \
293 return NT_STATUS_NO_MEMORY; \
294 } } while(0)
296 NTSTATUS samu_to_SamInfo3(TALLOC_CTX *mem_ctx,
297 struct samu *samu,
298 const char *login_server,
299 struct netr_SamInfo3 **_info3,
300 struct extra_auth_info *extra)
302 struct netr_SamInfo3 *info3;
303 const struct dom_sid *user_sid;
304 const struct dom_sid *group_sid;
305 struct dom_sid domain_sid;
306 struct dom_sid *group_sids;
307 uint32_t num_group_sids = 0;
308 const char *tmp;
309 gid_t *gids;
310 NTSTATUS status;
311 bool ok;
313 user_sid = pdb_get_user_sid(samu);
314 group_sid = pdb_get_group_sid(samu);
316 if (!user_sid || !group_sid) {
317 DEBUG(1, ("Sam account is missing sids!\n"));
318 return NT_STATUS_UNSUCCESSFUL;
321 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
322 if (!info3) {
323 return NT_STATUS_NO_MEMORY;
326 ZERO_STRUCT(domain_sid);
328 /* check if this is a "Unix Users" domain user,
329 * we need to handle it in a special way if that's the case */
330 if (sid_check_is_in_unix_users(user_sid)) {
331 /* in info3 you can only set rids for the user and the
332 * primary group, and the domain sid must be that of
333 * the sam domain.
335 * Store a completely bogus value here.
336 * The real SID is stored in the extra sids.
337 * Other code will know to look there if (-1) is found
339 info3->base.rid = (uint32_t)(-1);
340 sid_copy(&extra->user_sid, user_sid);
342 DEBUG(10, ("Unix User found in struct samu. Rid marked as "
343 "special and sid (%s) saved as extra sid\n",
344 sid_string_dbg(user_sid)));
345 } else {
346 sid_copy(&domain_sid, user_sid);
347 sid_split_rid(&domain_sid, &info3->base.rid);
350 if (is_null_sid(&domain_sid)) {
351 sid_copy(&domain_sid, get_global_sam_sid());
354 /* check if this is a "Unix Groups" domain group,
355 * if so we need special handling */
356 if (sid_check_is_in_unix_groups(group_sid)) {
357 /* in info3 you can only set rids for the user and the
358 * primary group, and the domain sid must be that of
359 * the sam domain.
361 * Store a completely bogus value here.
362 * The real SID is stored in the extra sids.
363 * Other code will know to look there if (-1) is found
365 info3->base.primary_gid = (uint32_t)(-1);
366 sid_copy(&extra->pgid_sid, group_sid);
368 DEBUG(10, ("Unix Group found in struct samu. Rid marked as "
369 "special and sid (%s) saved as extra sid\n",
370 sid_string_dbg(group_sid)));
372 } else {
373 ok = sid_peek_check_rid(&domain_sid, group_sid,
374 &info3->base.primary_gid);
375 if (!ok) {
376 DEBUG(1, ("The primary group domain sid(%s) does not "
377 "match the domain sid(%s) for %s(%s)\n",
378 sid_string_dbg(group_sid),
379 sid_string_dbg(&domain_sid),
380 pdb_get_username(samu),
381 sid_string_dbg(user_sid)));
382 TALLOC_FREE(info3);
383 return NT_STATUS_UNSUCCESSFUL;
387 unix_to_nt_time(&info3->base.logon_time, pdb_get_logon_time(samu));
388 unix_to_nt_time(&info3->base.logoff_time, get_time_t_max());
389 unix_to_nt_time(&info3->base.kickoff_time, get_time_t_max());
390 unix_to_nt_time(&info3->base.last_password_change,
391 pdb_get_pass_last_set_time(samu));
392 unix_to_nt_time(&info3->base.allow_password_change,
393 pdb_get_pass_can_change_time(samu));
394 unix_to_nt_time(&info3->base.force_password_change,
395 pdb_get_pass_must_change_time(samu));
397 tmp = pdb_get_username(samu);
398 if (tmp) {
399 info3->base.account_name.string = talloc_strdup(info3, tmp);
400 RET_NOMEM(info3->base.account_name.string);
402 tmp = pdb_get_fullname(samu);
403 if (tmp) {
404 info3->base.full_name.string = talloc_strdup(info3, tmp);
405 RET_NOMEM(info3->base.full_name.string);
407 tmp = pdb_get_logon_script(samu);
408 if (tmp) {
409 info3->base.logon_script.string = talloc_strdup(info3, tmp);
410 RET_NOMEM(info3->base.logon_script.string);
412 tmp = pdb_get_profile_path(samu);
413 if (tmp) {
414 info3->base.profile_path.string = talloc_strdup(info3, tmp);
415 RET_NOMEM(info3->base.profile_path.string);
417 tmp = pdb_get_homedir(samu);
418 if (tmp) {
419 info3->base.home_directory.string = talloc_strdup(info3, tmp);
420 RET_NOMEM(info3->base.home_directory.string);
422 tmp = pdb_get_dir_drive(samu);
423 if (tmp) {
424 info3->base.home_drive.string = talloc_strdup(info3, tmp);
425 RET_NOMEM(info3->base.home_drive.string);
428 info3->base.logon_count = pdb_get_logon_count(samu);
429 info3->base.bad_password_count = pdb_get_bad_password_count(samu);
431 info3->base.logon_domain.string = talloc_strdup(info3,
432 pdb_get_domain(samu));
433 RET_NOMEM(info3->base.logon_domain.string);
435 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
436 RET_NOMEM(info3->base.domain_sid);
438 status = pdb_enum_group_memberships(mem_ctx, samu,
439 &group_sids, &gids,
440 &num_group_sids);
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(1, ("Failed to get groups from sam account.\n"));
443 TALLOC_FREE(info3);
444 return status;
447 if (num_group_sids) {
448 status = group_sids_to_info3(info3, group_sids, num_group_sids);
449 if (!NT_STATUS_IS_OK(status)) {
450 TALLOC_FREE(info3);
451 return status;
455 /* We don't need sids and gids after the conversion */
456 TALLOC_FREE(group_sids);
457 TALLOC_FREE(gids);
458 num_group_sids = 0;
460 /* FIXME: should we add other flags ? */
461 info3->base.user_flags = NETLOGON_EXTRA_SIDS;
463 if (login_server) {
464 info3->base.logon_server.string = talloc_strdup(info3, login_server);
465 RET_NOMEM(info3->base.logon_server.string);
468 info3->base.acct_flags = pdb_get_acct_ctrl(samu);
470 *_info3 = info3;
471 return NT_STATUS_OK;
474 #undef RET_NOMEM
476 #define RET_NOMEM(ptr) do { \
477 if (!ptr) { \
478 TALLOC_FREE(info3); \
479 return NULL; \
480 } } while(0)
482 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
483 struct netr_SamInfo3 *orig)
485 struct netr_SamInfo3 *info3;
486 unsigned int i;
487 NTSTATUS status;
489 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
490 if (!info3) return NULL;
492 status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
493 if (!NT_STATUS_IS_OK(status)) {
494 TALLOC_FREE(info3);
495 return NULL;
498 if (orig->sidcount) {
499 info3->sidcount = orig->sidcount;
500 info3->sids = talloc_array(info3, struct netr_SidAttr,
501 orig->sidcount);
502 RET_NOMEM(info3->sids);
503 for (i = 0; i < orig->sidcount; i++) {
504 info3->sids[i].sid = dom_sid_dup(info3->sids,
505 orig->sids[i].sid);
506 RET_NOMEM(info3->sids[i].sid);
507 info3->sids[i].attributes =
508 orig->sids[i].attributes;
512 return info3;
515 static NTSTATUS wbcsids_to_samr_RidWithAttributeArray(
516 TALLOC_CTX *mem_ctx,
517 struct samr_RidWithAttributeArray *groups,
518 const struct dom_sid *domain_sid,
519 const struct wbcSidWithAttr *sids,
520 size_t num_sids)
522 unsigned int i;
523 bool ok;
525 groups->rids = talloc_array(mem_ctx,
526 struct samr_RidWithAttribute, num_sids);
527 if (!groups->rids) {
528 return NT_STATUS_NO_MEMORY;
531 /* a wbcDomainSid is the same as a dom_sid */
532 for (i = 0; i < num_sids; i++) {
533 ok = sid_peek_check_rid(domain_sid,
534 (const struct dom_sid *)&sids[i].sid,
535 &groups->rids[i].rid);
536 if (!ok) continue;
538 groups->rids[i].attributes = SE_GROUP_MANDATORY |
539 SE_GROUP_ENABLED_BY_DEFAULT |
540 SE_GROUP_ENABLED;
541 groups->count++;
544 return NT_STATUS_OK;
547 struct netr_SamInfo3 *wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX *mem_ctx,
548 const struct wbcAuthUserInfo *info)
550 struct netr_SamInfo3 *info3;
551 struct dom_sid user_sid;
552 struct dom_sid group_sid;
553 struct dom_sid domain_sid;
554 NTSTATUS status;
555 bool ok;
557 memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
558 memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
560 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
561 if (!info3) return NULL;
563 info3->base.logon_time = info->logon_time;
564 info3->base.logoff_time = info->logoff_time;
565 info3->base.kickoff_time = info->kickoff_time;
566 unix_to_nt_time(&info3->base.last_password_change, info->pass_last_set_time);
567 unix_to_nt_time(&info3->base.allow_password_change,
568 info->pass_can_change_time);
569 unix_to_nt_time(&info3->base.force_password_change,
570 info->pass_must_change_time);
572 if (info->account_name) {
573 info3->base.account_name.string =
574 talloc_strdup(info3, info->account_name);
575 RET_NOMEM(info3->base.account_name.string);
577 if (info->full_name) {
578 info3->base.full_name.string =
579 talloc_strdup(info3, info->full_name);
580 RET_NOMEM(info3->base.full_name.string);
582 if (info->logon_script) {
583 info3->base.logon_script.string =
584 talloc_strdup(info3, info->logon_script);
585 RET_NOMEM(info3->base.logon_script.string);
587 if (info->profile_path) {
588 info3->base.profile_path.string =
589 talloc_strdup(info3, info->profile_path);
590 RET_NOMEM(info3->base.profile_path.string);
592 if (info->home_directory) {
593 info3->base.home_directory.string =
594 talloc_strdup(info3, info->home_directory);
595 RET_NOMEM(info3->base.home_directory.string);
597 if (info->home_drive) {
598 info3->base.home_drive.string =
599 talloc_strdup(info3, info->home_drive);
600 RET_NOMEM(info3->base.home_drive.string);
603 info3->base.logon_count = info->logon_count;
604 info3->base.bad_password_count = info->bad_password_count;
606 sid_copy(&domain_sid, &user_sid);
607 sid_split_rid(&domain_sid, &info3->base.rid);
609 ok = sid_peek_check_rid(&domain_sid, &group_sid,
610 &info3->base.primary_gid);
611 if (!ok) {
612 DEBUG(1, ("The primary group sid domain does not"
613 "match user sid domain for user: %s\n",
614 info->account_name));
615 TALLOC_FREE(info3);
616 return NULL;
619 status = wbcsids_to_samr_RidWithAttributeArray(info3,
620 &info3->base.groups,
621 &domain_sid,
622 &info->sids[1],
623 info->num_sids - 1);
624 if (!NT_STATUS_IS_OK(status)) {
625 TALLOC_FREE(info3);
626 return NULL;
629 info3->base.user_flags = info->user_flags;
630 memcpy(info3->base.key.key, info->user_session_key, 16);
632 if (info->logon_server) {
633 info3->base.logon_server.string =
634 talloc_strdup(info3, info->logon_server);
635 RET_NOMEM(info3->base.logon_server.string);
637 if (info->domain_name) {
638 info3->base.logon_domain.string =
639 talloc_strdup(info3, info->domain_name);
640 RET_NOMEM(info3->base.logon_domain.string);
643 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
644 RET_NOMEM(info3->base.domain_sid);
646 memcpy(info3->base.LMSessKey.key, info->lm_session_key, 8);
647 info3->base.acct_flags = info->acct_flags;
649 return info3;