idl: Improve MS-PAC IDL
[samba.git] / source3 / auth / server_info.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authentication utility functions
4    Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18 */
19
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"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
31
32 /***************************************************************************
33  Make a server_info struct. Free with TALLOC_FREE().
34 ***************************************************************************/
35
36 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
37 {
38         struct auth_serversupplied_info *result;
39
40         result = talloc_zero(mem_ctx, struct auth_serversupplied_info);
41         if (result == NULL) {
42                 DEBUG(0, ("talloc failed\n"));
43                 return NULL;
44         }
45
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. */
49
50         result->utok.uid = -1;
51         result->utok.gid = -1;
52
53         return result;
54 }
55
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 *****************************************************************************/
60
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)
65 {
66         struct netr_SamInfo3 *info3;
67
68         info3 = copy_netr_SamInfo3(sam2, server_info->info3);
69         if (!info3) {
70                 return NT_STATUS_NO_MEMORY;
71         }
72
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);
81                 }
82         }
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);
91                 }
92         }
93
94         sam2->base = info3->base;
95
96         return NT_STATUS_OK;
97 }
98
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 *****************************************************************************/
103
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)
108 {
109         struct netr_SamInfo3 *info3;
110
111         info3 = copy_netr_SamInfo3(sam3, server_info->info3);
112         if (!info3) {
113                 return NT_STATUS_NO_MEMORY;
114         }
115
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);
124                 }
125         }
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);
134                 }
135         }
136
137         sam3->base = info3->base;
138
139         sam3->sidcount          = 0;
140         sam3->sids              = NULL;
141
142         return NT_STATUS_OK;
143 }
144
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 *****************************************************************************/
149
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)
154 {
155         struct pdb_domain_info *dominfo;
156         struct netr_SamInfo3 *info3;
157
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;
162         }
163
164         dominfo = pdb_get_domain_info(sam6);
165         if (dominfo == NULL) {
166                 return NT_STATUS_NO_MEMORY;
167         }
168
169         info3 = copy_netr_SamInfo3(sam6, server_info->info3);
170         if (!info3) {
171                 return NT_STATUS_NO_MEMORY;
172         }
173
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);
182                 }
183         }
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);
192                 }
193         }
194
195         sam6->base = info3->base;
196
197         sam6->sidcount          = 0;
198         sam6->sids              = NULL;
199
200         sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
201         if (sam6->dns_domainname.string == NULL) {
202                 return NT_STATUS_NO_MEMORY;
203         }
204
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;
210         }
211
212         return NT_STATUS_OK;
213 }
214
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)
220 {
221         uint32_t t = *count;
222
223         *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
224         if (*sids == NULL) {
225                 return NT_STATUS_NO_MEMORY;
226         }
227         (*sids)[t].sid = dom_sid_dup(*sids, asid);
228         if ((*sids)[t].sid == NULL) {
229                 return NT_STATUS_NO_MEMORY;
230         }
231         (*sids)[t].attributes = attributes;
232         *count = t + 1;
233
234         return NT_STATUS_OK;
235 }
236
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)
243 {
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;
253
254         domain_sid = info3->base.domain_sid;
255         groups = &info3->base.groups;
256
257         groups->rids = talloc_array(info3,
258                                     struct samr_RidWithAttribute, num_sids);
259         if (!groups->rids) {
260                 return NT_STATUS_NO_MEMORY;
261         }
262
263         for (i = 0; i < num_sids; i++) {
264                 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
265                 if (ok) {
266
267                         /* if it is the primary gid, skip it, we
268                          * obviously already have it */
269                         if (info3->base.primary_gid == rid) continue;
270
271                         /* store domain group rid */
272                         groups->rids[groups->count].rid = rid;
273                         groups->rids[groups->count].attributes = attributes;
274                         groups->count++;
275                         continue;
276                 }
277
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;
284                 }
285         }
286
287         return NT_STATUS_OK;
288 }
289
290 #define RET_NOMEM(ptr) do { \
291         if (!ptr) { \
292                 TALLOC_FREE(info3); \
293                 return NT_STATUS_NO_MEMORY; \
294         } } while(0)
295
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)
301 {
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;
312
313         user_sid = pdb_get_user_sid(samu);
314         group_sid = pdb_get_group_sid(samu);
315
316         if (!user_sid || !group_sid) {
317                 DEBUG(1, ("Sam account is missing sids!\n"));
318                 return NT_STATUS_UNSUCCESSFUL;
319         }
320
321         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
322         if (!info3) {
323                 return NT_STATUS_NO_MEMORY;
324         }
325
326         ZERO_STRUCT(domain_sid);
327
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.
334                  *
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
338                  */
339                 info3->base.rid = (uint32_t)(-1);
340                 sid_copy(&extra->user_sid, user_sid);
341
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);
348         }
349
350         if (is_null_sid(&domain_sid)) {
351                 sid_copy(&domain_sid, get_global_sam_sid());
352         }
353
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.
360                  *
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
364                  */
365                 info3->base.primary_gid = (uint32_t)(-1);
366                 sid_copy(&extra->pgid_sid, group_sid);
367
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)));
371
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;
384                 }
385         }
386
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));
396
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);
401         }
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);
406         }
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);
411         }
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);
416         }
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);
421         }
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);
426         }
427
428         info3->base.logon_count = pdb_get_logon_count(samu);
429         info3->base.bad_password_count = pdb_get_bad_password_count(samu);
430
431         info3->base.logon_domain.string = talloc_strdup(info3,
432                                                   pdb_get_domain(samu));
433         RET_NOMEM(info3->base.logon_domain.string);
434
435         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
436         RET_NOMEM(info3->base.domain_sid);
437
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;
445         }
446
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;
452                 }
453         }
454
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;
459
460         /* FIXME: should we add other flags ? */
461         info3->base.user_flags = NETLOGON_EXTRA_SIDS;
462
463         if (login_server) {
464                 info3->base.logon_server.string = talloc_strdup(info3, login_server);
465                 RET_NOMEM(info3->base.logon_server.string);
466         }
467
468         info3->base.acct_flags = pdb_get_acct_ctrl(samu);
469
470         *_info3 = info3;
471         return NT_STATUS_OK;
472 }
473
474 #undef RET_NOMEM
475
476 #define RET_NOMEM(ptr) do { \
477         if (!ptr) { \
478                 TALLOC_FREE(info3); \
479                 return NULL; \
480         } } while(0)
481
482 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
483                                          struct netr_SamInfo3 *orig)
484 {
485         struct netr_SamInfo3 *info3;
486         unsigned int i;
487         NTSTATUS status;
488
489         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
490         if (!info3) return NULL;
491
492         status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
493         if (!NT_STATUS_IS_OK(status)) {
494                 TALLOC_FREE(info3);
495                 return NULL;
496         }
497
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;
509                 }
510         }
511
512         return info3;
513 }
514
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)
521 {
522         unsigned int i;
523         bool ok;
524
525         groups->rids = talloc_array(mem_ctx,
526                                     struct samr_RidWithAttribute, num_sids);
527         if (!groups->rids) {
528                 return NT_STATUS_NO_MEMORY;
529         }
530
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;
537
538                 groups->rids[i].attributes = SE_GROUP_MANDATORY |
539                                              SE_GROUP_ENABLED_BY_DEFAULT |
540                                              SE_GROUP_ENABLED;
541                 groups->count++;
542         }
543
544         return NT_STATUS_OK;
545 }
546
547 struct netr_SamInfo3 *wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX *mem_ctx,
548                                         const struct wbcAuthUserInfo *info)
549 {
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;
556
557         memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
558         memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
559
560         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
561         if (!info3) return NULL;
562
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);
571
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);
576         }
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);
581         }
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);
586         }
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);
591         }
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);
596         }
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);
601         }
602
603         info3->base.logon_count = info->logon_count;
604         info3->base.bad_password_count = info->bad_password_count;
605
606         sid_copy(&domain_sid, &user_sid);
607         sid_split_rid(&domain_sid, &info3->base.rid);
608
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;
617         }
618
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;
627         }
628
629         info3->base.user_flags = info->user_flags;
630         memcpy(info3->base.key.key, info->user_session_key, 16);
631
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);
636         }
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);
641         }
642
643         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
644         RET_NOMEM(info3->base.domain_sid);
645
646         memcpy(info3->base.LMSessKey.key, info->lm_session_key, 8);
647         info3->base.acct_flags = info->acct_flags;
648
649         return info3;
650 }