s3-auth: use auth.h where needed.
[kai/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 /* FIXME: do we really still need this ? */
33 static int server_info_dtor(struct auth_serversupplied_info *server_info)
34 {
35         TALLOC_FREE(server_info->info3);
36         ZERO_STRUCTP(server_info);
37         return 0;
38 }
39
40 /***************************************************************************
41  Make a server_info struct. Free with TALLOC_FREE().
42 ***************************************************************************/
43
44 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
45 {
46         struct auth_serversupplied_info *result;
47
48         result = TALLOC_ZERO_P(mem_ctx, struct auth_serversupplied_info);
49         if (result == NULL) {
50                 DEBUG(0, ("talloc failed\n"));
51                 return NULL;
52         }
53
54         talloc_set_destructor(result, server_info_dtor);
55
56         /* Initialise the uid and gid values to something non-zero
57            which may save us from giving away root access if there
58            is a bug in allocating these fields. */
59
60         result->utok.uid = -1;
61         result->utok.gid = -1;
62
63         return result;
64 }
65
66 /****************************************************************************
67  inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
68  already be initialized and is used as the talloc parent for its members.
69 *****************************************************************************/
70
71 NTSTATUS serverinfo_to_SamInfo2(struct auth_serversupplied_info *server_info,
72                                 uint8_t *pipe_session_key,
73                                 size_t pipe_session_key_len,
74                                 struct netr_SamInfo2 *sam2)
75 {
76         struct netr_SamInfo3 *info3;
77
78         info3 = copy_netr_SamInfo3(sam2, server_info->info3);
79         if (!info3) {
80                 return NT_STATUS_NO_MEMORY;
81         }
82
83         if (server_info->user_session_key.length) {
84                 memcpy(info3->base.key.key,
85                        server_info->user_session_key.data,
86                        MIN(sizeof(info3->base.key.key),
87                            server_info->user_session_key.length));
88                 if (pipe_session_key) {
89                         arcfour_crypt(info3->base.key.key,
90                                       pipe_session_key, 16);
91                 }
92         }
93         if (server_info->lm_session_key.length) {
94                 memcpy(info3->base.LMSessKey.key,
95                        server_info->lm_session_key.data,
96                        MIN(sizeof(info3->base.LMSessKey.key),
97                            server_info->lm_session_key.length));
98                 if (pipe_session_key) {
99                         arcfour_crypt(info3->base.LMSessKey.key,
100                                       pipe_session_key, 8);
101                 }
102         }
103
104         sam2->base = info3->base;
105
106         return NT_STATUS_OK;
107 }
108
109 /****************************************************************************
110  inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
111  already be initialized and is used as the talloc parent for its members.
112 *****************************************************************************/
113
114 NTSTATUS serverinfo_to_SamInfo3(const struct auth_serversupplied_info *server_info,
115                                 uint8_t *pipe_session_key,
116                                 size_t pipe_session_key_len,
117                                 struct netr_SamInfo3 *sam3)
118 {
119         struct netr_SamInfo3 *info3;
120
121         info3 = copy_netr_SamInfo3(sam3, server_info->info3);
122         if (!info3) {
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         if (server_info->user_session_key.length) {
127                 memcpy(info3->base.key.key,
128                        server_info->user_session_key.data,
129                        MIN(sizeof(info3->base.key.key),
130                            server_info->user_session_key.length));
131                 if (pipe_session_key) {
132                         arcfour_crypt(info3->base.key.key,
133                                       pipe_session_key, 16);
134                 }
135         }
136         if (server_info->lm_session_key.length) {
137                 memcpy(info3->base.LMSessKey.key,
138                        server_info->lm_session_key.data,
139                        MIN(sizeof(info3->base.LMSessKey.key),
140                            server_info->lm_session_key.length));
141                 if (pipe_session_key) {
142                         arcfour_crypt(info3->base.LMSessKey.key,
143                                       pipe_session_key, 8);
144                 }
145         }
146
147         sam3->base = info3->base;
148
149         sam3->sidcount          = 0;
150         sam3->sids              = NULL;
151
152         return NT_STATUS_OK;
153 }
154
155 /****************************************************************************
156  inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
157  already be initialized and is used as the talloc parent for its members.
158 *****************************************************************************/
159
160 NTSTATUS serverinfo_to_SamInfo6(struct auth_serversupplied_info *server_info,
161                                 uint8_t *pipe_session_key,
162                                 size_t pipe_session_key_len,
163                                 struct netr_SamInfo6 *sam6)
164 {
165         struct pdb_domain_info *dominfo;
166         struct netr_SamInfo3 *info3;
167
168         if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
169                 DEBUG(10,("Not adding validation info level 6 "
170                            "without ADS passdb backend\n"));
171                 return NT_STATUS_INVALID_INFO_CLASS;
172         }
173
174         dominfo = pdb_get_domain_info(sam6);
175         if (dominfo == NULL) {
176                 return NT_STATUS_NO_MEMORY;
177         }
178
179         info3 = copy_netr_SamInfo3(sam6, server_info->info3);
180         if (!info3) {
181                 return NT_STATUS_NO_MEMORY;
182         }
183
184         if (server_info->user_session_key.length) {
185                 memcpy(info3->base.key.key,
186                        server_info->user_session_key.data,
187                        MIN(sizeof(info3->base.key.key),
188                            server_info->user_session_key.length));
189                 if (pipe_session_key) {
190                         arcfour_crypt(info3->base.key.key,
191                                       pipe_session_key, 16);
192                 }
193         }
194         if (server_info->lm_session_key.length) {
195                 memcpy(info3->base.LMSessKey.key,
196                        server_info->lm_session_key.data,
197                        MIN(sizeof(info3->base.LMSessKey.key),
198                            server_info->lm_session_key.length));
199                 if (pipe_session_key) {
200                         arcfour_crypt(info3->base.LMSessKey.key,
201                                       pipe_session_key, 8);
202                 }
203         }
204
205         sam6->base = info3->base;
206
207         sam6->sidcount          = 0;
208         sam6->sids              = NULL;
209
210         sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
211         if (sam6->dns_domainname.string == NULL) {
212                 return NT_STATUS_NO_MEMORY;
213         }
214
215         sam6->principle.string  = talloc_asprintf(sam6, "%s@%s",
216                                                   sam6->base.account_name.string,
217                                                   sam6->dns_domainname.string);
218         if (sam6->principle.string == NULL) {
219                 return NT_STATUS_NO_MEMORY;
220         }
221
222         return NT_STATUS_OK;
223 }
224
225 static NTSTATUS append_netr_SidAttr(TALLOC_CTX *mem_ctx,
226                                     struct netr_SidAttr **sids,
227                                     uint32_t *count,
228                                     const struct dom_sid2 *asid,
229                                     uint32_t attributes)
230 {
231         uint32_t t = *count;
232
233         *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
234         if (*sids == NULL) {
235                 return NT_STATUS_NO_MEMORY;
236         }
237         (*sids)[t].sid = dom_sid_dup(*sids, asid);
238         if ((*sids)[t].sid == NULL) {
239                 return NT_STATUS_NO_MEMORY;
240         }
241         (*sids)[t].attributes = attributes;
242         *count = t + 1;
243
244         return NT_STATUS_OK;
245 }
246
247 /* Fills the samr_RidWithAttributeArray with the provided sids.
248  * If it happens that we have additional groups that do not belong
249  * to the domain, add their sids as extra sids */
250 static NTSTATUS group_sids_to_info3(struct netr_SamInfo3 *info3,
251                                     const struct dom_sid *sids,
252                                     size_t num_sids)
253 {
254         uint32_t attributes = SE_GROUP_MANDATORY |
255                                 SE_GROUP_ENABLED_BY_DEFAULT |
256                                 SE_GROUP_ENABLED;
257         struct samr_RidWithAttributeArray *groups;
258         struct dom_sid *domain_sid;
259         unsigned int i;
260         NTSTATUS status;
261         uint32_t rid;
262         bool ok;
263
264         domain_sid = info3->base.domain_sid;
265         groups = &info3->base.groups;
266
267         groups->rids = talloc_array(info3,
268                                     struct samr_RidWithAttribute, num_sids);
269         if (!groups->rids) {
270                 return NT_STATUS_NO_MEMORY;
271         }
272
273         for (i = 0; i < num_sids; i++) {
274                 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
275                 if (ok) {
276
277                         /* if it is the primary gid, skip it, we
278                          * obviously already have it */
279                         if (info3->base.primary_gid == rid) continue;
280
281                         /* store domain group rid */
282                         groups->rids[i].rid = rid;
283                         groups->rids[i].attributes = attributes;
284                         groups->count++;
285                         continue;
286                 }
287
288                 /* if this wasn't a domain sid, add it as extra sid */
289                 status = append_netr_SidAttr(info3, &info3->sids,
290                                              &info3->sidcount,
291                                              &sids[i], attributes);
292                 if (!NT_STATUS_IS_OK(status)) {
293                         return status;
294                 }
295         }
296
297         return NT_STATUS_OK;
298 }
299
300 #define RET_NOMEM(ptr) do { \
301         if (!ptr) { \
302                 TALLOC_FREE(info3); \
303                 return NT_STATUS_NO_MEMORY; \
304         } } while(0)
305
306 NTSTATUS samu_to_SamInfo3(TALLOC_CTX *mem_ctx,
307                           struct samu *samu,
308                           const char *login_server,
309                           struct netr_SamInfo3 **_info3,
310                           struct extra_auth_info *extra)
311 {
312         struct netr_SamInfo3 *info3;
313         const struct dom_sid *user_sid;
314         const struct dom_sid *group_sid;
315         struct dom_sid domain_sid;
316         struct dom_sid *group_sids;
317         uint32_t num_group_sids = 0;
318         const char *tmp;
319         gid_t *gids;
320         NTSTATUS status;
321         bool ok;
322
323         user_sid = pdb_get_user_sid(samu);
324         group_sid = pdb_get_group_sid(samu);
325
326         if (!user_sid || !group_sid) {
327                 DEBUG(1, ("Sam account is missing sids!\n"));
328                 return NT_STATUS_UNSUCCESSFUL;
329         }
330
331         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
332         if (!info3) {
333                 return NT_STATUS_NO_MEMORY;
334         }
335
336         ZERO_STRUCT(domain_sid);
337
338         /* check if this is a "Unix Users" domain user,
339          * we need to handle it in a special way if that's the case */
340         if (sid_check_is_in_unix_users(user_sid)) {
341                 /* in info3 you can only set rids for the user and the
342                  * primary group, and the domain sid must be that of
343                  * the sam domain.
344                  *
345                  * Store a completely bogus value here.
346                  * The real SID is stored in the extra sids.
347                  * Other code will know to look there if (-1) is found
348                  */
349                 info3->base.rid = (uint32_t)(-1);
350                 sid_copy(&extra->user_sid, user_sid);
351
352                 DEBUG(10, ("Unix User found in struct samu. Rid marked as "
353                            "special and sid (%s) saved as extra sid\n",
354                            sid_string_dbg(user_sid)));
355         } else {
356                 sid_copy(&domain_sid, user_sid);
357                 sid_split_rid(&domain_sid, &info3->base.rid);
358         }
359
360         if (is_null_sid(&domain_sid)) {
361                 sid_copy(&domain_sid, get_global_sam_sid());
362         }
363
364         /* check if this is a "Unix Groups" domain group,
365          * if so we need special handling */
366         if (sid_check_is_in_unix_groups(group_sid)) {
367                 /* in info3 you can only set rids for the user and the
368                  * primary group, and the domain sid must be that of
369                  * the sam domain.
370                  *
371                  * Store a completely bogus value here.
372                  * The real SID is stored in the extra sids.
373                  * Other code will know to look there if (-1) is found
374                  */
375                 info3->base.primary_gid = (uint32_t)(-1);
376                 sid_copy(&extra->pgid_sid, group_sid);
377
378                 DEBUG(10, ("Unix Group found in struct samu. Rid marked as "
379                            "special and sid (%s) saved as extra sid\n",
380                            sid_string_dbg(group_sid)));
381
382         } else {
383                 ok = sid_peek_check_rid(&domain_sid, group_sid,
384                                         &info3->base.primary_gid);
385                 if (!ok) {
386                         DEBUG(1, ("The primary group domain sid(%s) does not "
387                                   "match the domain sid(%s) for %s(%s)\n",
388                                   sid_string_dbg(group_sid),
389                                   sid_string_dbg(&domain_sid),
390                                   pdb_get_username(samu),
391                                   sid_string_dbg(user_sid)));
392                         TALLOC_FREE(info3);
393                         return NT_STATUS_UNSUCCESSFUL;
394                 }
395         }
396
397         unix_to_nt_time(&info3->base.last_logon, pdb_get_logon_time(samu));
398         unix_to_nt_time(&info3->base.last_logoff, get_time_t_max());
399         unix_to_nt_time(&info3->base.acct_expiry, get_time_t_max());
400         unix_to_nt_time(&info3->base.last_password_change,
401                         pdb_get_pass_last_set_time(samu));
402         unix_to_nt_time(&info3->base.allow_password_change,
403                         pdb_get_pass_can_change_time(samu));
404         unix_to_nt_time(&info3->base.force_password_change,
405                         pdb_get_pass_must_change_time(samu));
406
407         tmp = pdb_get_username(samu);
408         if (tmp) {
409                 info3->base.account_name.string = talloc_strdup(info3, tmp);
410                 RET_NOMEM(info3->base.account_name.string);
411         }
412         tmp = pdb_get_fullname(samu);
413         if (tmp) {
414                 info3->base.full_name.string = talloc_strdup(info3, tmp);
415                 RET_NOMEM(info3->base.full_name.string);
416         }
417         tmp = pdb_get_logon_script(samu);
418         if (tmp) {
419                 info3->base.logon_script.string = talloc_strdup(info3, tmp);
420                 RET_NOMEM(info3->base.logon_script.string);
421         }
422         tmp = pdb_get_profile_path(samu);
423         if (tmp) {
424                 info3->base.profile_path.string = talloc_strdup(info3, tmp);
425                 RET_NOMEM(info3->base.profile_path.string);
426         }
427         tmp = pdb_get_homedir(samu);
428         if (tmp) {
429                 info3->base.home_directory.string = talloc_strdup(info3, tmp);
430                 RET_NOMEM(info3->base.home_directory.string);
431         }
432         tmp = pdb_get_dir_drive(samu);
433         if (tmp) {
434                 info3->base.home_drive.string = talloc_strdup(info3, tmp);
435                 RET_NOMEM(info3->base.home_drive.string);
436         }
437
438         info3->base.logon_count = pdb_get_logon_count(samu);
439         info3->base.bad_password_count = pdb_get_bad_password_count(samu);
440
441         status = pdb_enum_group_memberships(mem_ctx, samu,
442                                             &group_sids, &gids,
443                                             &num_group_sids);
444         if (!NT_STATUS_IS_OK(status)) {
445                 DEBUG(1, ("Failed to get groups from sam account.\n"));
446                 TALLOC_FREE(info3);
447                 return status;
448         }
449
450         if (num_group_sids) {
451                 status = group_sids_to_info3(info3, group_sids, num_group_sids);
452                 if (!NT_STATUS_IS_OK(status)) {
453                         TALLOC_FREE(info3);
454                         return status;
455                 }
456         }
457
458         /* We don't need sids and gids after the conversion */
459         TALLOC_FREE(group_sids);
460         TALLOC_FREE(gids);
461         num_group_sids = 0;
462
463         /* FIXME: should we add other flags ? */
464         info3->base.user_flags = NETLOGON_EXTRA_SIDS;
465
466         if (login_server) {
467                 info3->base.logon_server.string = talloc_strdup(info3, login_server);
468                 RET_NOMEM(info3->base.logon_server.string);
469         }
470
471         info3->base.domain.string = talloc_strdup(info3,
472                                                   pdb_get_domain(samu));
473         RET_NOMEM(info3->base.domain.string);
474
475         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
476         RET_NOMEM(info3->base.domain_sid);
477
478         info3->base.acct_flags = pdb_get_acct_ctrl(samu);
479
480         *_info3 = info3;
481         return NT_STATUS_OK;
482 }
483
484 #undef RET_NOMEM
485
486 #define RET_NOMEM(ptr) do { \
487         if (!ptr) { \
488                 TALLOC_FREE(info3); \
489                 return NULL; \
490         } } while(0)
491
492 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
493                                          struct netr_SamInfo3 *orig)
494 {
495         struct netr_SamInfo3 *info3;
496         unsigned int i;
497         NTSTATUS status;
498
499         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
500         if (!info3) return NULL;
501
502         status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
503         if (!NT_STATUS_IS_OK(status)) {
504                 TALLOC_FREE(info3);
505                 return NULL;
506         }
507
508         if (orig->sidcount) {
509                 info3->sidcount = orig->sidcount;
510                 info3->sids = talloc_array(info3, struct netr_SidAttr,
511                                            orig->sidcount);
512                 RET_NOMEM(info3->sids);
513                 for (i = 0; i < orig->sidcount; i++) {
514                         info3->sids[i].sid = dom_sid_dup(info3->sids,
515                                                             orig->sids[i].sid);
516                         RET_NOMEM(info3->sids[i].sid);
517                         info3->sids[i].attributes =
518                                 orig->sids[i].attributes;
519                 }
520         }
521
522         return info3;
523 }
524
525 static NTSTATUS wbcsids_to_samr_RidWithAttributeArray(
526                                 TALLOC_CTX *mem_ctx,
527                                 struct samr_RidWithAttributeArray *groups,
528                                 const struct dom_sid *domain_sid,
529                                 const struct wbcSidWithAttr *sids,
530                                 size_t num_sids)
531 {
532         unsigned int i;
533         bool ok;
534
535         groups->rids = talloc_array(mem_ctx,
536                                     struct samr_RidWithAttribute, num_sids);
537         if (!groups->rids) {
538                 return NT_STATUS_NO_MEMORY;
539         }
540
541         /* a wbcDomainSid is the same as a dom_sid */
542         for (i = 0; i < num_sids; i++) {
543                 ok = sid_peek_check_rid(domain_sid,
544                                         (const struct dom_sid *)&sids[i].sid,
545                                         &groups->rids[i].rid);
546                 if (!ok) continue;
547
548                 groups->rids[i].attributes = SE_GROUP_MANDATORY |
549                                              SE_GROUP_ENABLED_BY_DEFAULT |
550                                              SE_GROUP_ENABLED;
551                 groups->count++;
552         }
553
554         return NT_STATUS_OK;
555 }
556
557 struct netr_SamInfo3 *wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX *mem_ctx,
558                                         const struct wbcAuthUserInfo *info)
559 {
560         struct netr_SamInfo3 *info3;
561         struct dom_sid user_sid;
562         struct dom_sid group_sid;
563         struct dom_sid domain_sid;
564         NTSTATUS status;
565         bool ok;
566
567         memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
568         memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
569
570         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
571         if (!info3) return NULL;
572
573         info3->base.last_logon = info->logon_time;
574         info3->base.last_logoff = info->logoff_time;
575         info3->base.acct_expiry = info->kickoff_time;
576         unix_to_nt_time(&info3->base.last_password_change, info->pass_last_set_time);
577         unix_to_nt_time(&info3->base.allow_password_change,
578                         info->pass_can_change_time);
579         unix_to_nt_time(&info3->base.force_password_change,
580                         info->pass_must_change_time);
581
582         if (info->account_name) {
583                 info3->base.account_name.string =
584                                 talloc_strdup(info3, info->account_name);
585                 RET_NOMEM(info3->base.account_name.string);
586         }
587         if (info->full_name) {
588                 info3->base.full_name.string =
589                                 talloc_strdup(info3, info->full_name);
590                 RET_NOMEM(info3->base.full_name.string);
591         }
592         if (info->logon_script) {
593                 info3->base.logon_script.string =
594                                 talloc_strdup(info3, info->logon_script);
595                 RET_NOMEM(info3->base.logon_script.string);
596         }
597         if (info->profile_path) {
598                 info3->base.profile_path.string =
599                                 talloc_strdup(info3, info->profile_path);
600                 RET_NOMEM(info3->base.profile_path.string);
601         }
602         if (info->home_directory) {
603                 info3->base.home_directory.string =
604                                 talloc_strdup(info3, info->home_directory);
605                 RET_NOMEM(info3->base.home_directory.string);
606         }
607         if (info->home_drive) {
608                 info3->base.home_drive.string =
609                                 talloc_strdup(info3, info->home_drive);
610                 RET_NOMEM(info3->base.home_drive.string);
611         }
612
613         info3->base.logon_count = info->logon_count;
614         info3->base.bad_password_count = info->bad_password_count;
615
616         sid_copy(&domain_sid, &user_sid);
617         sid_split_rid(&domain_sid, &info3->base.rid);
618
619         ok = sid_peek_check_rid(&domain_sid, &group_sid,
620                                 &info3->base.primary_gid);
621         if (!ok) {
622                 DEBUG(1, ("The primary group sid domain does not"
623                           "match user sid domain for user: %s\n",
624                           info->account_name));
625                 TALLOC_FREE(info3);
626                 return NULL;
627         }
628
629         status = wbcsids_to_samr_RidWithAttributeArray(info3,
630                                                        &info3->base.groups,
631                                                        &domain_sid,
632                                                        &info->sids[1],
633                                                        info->num_sids - 1);
634         if (!NT_STATUS_IS_OK(status)) {
635                 TALLOC_FREE(info3);
636                 return NULL;
637         }
638
639         info3->base.user_flags = info->user_flags;
640         memcpy(info3->base.key.key, info->user_session_key, 16);
641
642         if (info->logon_server) {
643                 info3->base.logon_server.string =
644                                 talloc_strdup(info3, info->logon_server);
645                 RET_NOMEM(info3->base.logon_server.string);
646         }
647         if (info->domain_name) {
648                 info3->base.domain.string =
649                                 talloc_strdup(info3, info->domain_name);
650                 RET_NOMEM(info3->base.domain.string);
651         }
652
653         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
654         RET_NOMEM(info3->base.domain_sid);
655
656         memcpy(info3->base.LMSessKey.key, info->lm_session_key, 8);
657         info3->base.acct_flags = info->acct_flags;
658
659         return info3;
660 }