LCOV - code coverage report
Current view: top level - auth - auth_sam_reply.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 249 320 77.8 %
Date: 2024-06-13 04:01:37 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Convert a server info struct into the form for PAC and NETLOGON replies
       5             : 
       6             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2011
       7             :    Copyright (C) Stefan Metzmacher <metze@samba.org>  2005
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "librpc/gen_ndr/auth.h"
      25             : #include "libcli/security/security.h"
      26             : #include "auth/auth_sam_reply.h"
      27             : 
      28       62035 : static NTSTATUS auth_convert_user_info_dc_sambaseinfo(TALLOC_CTX *mem_ctx,
      29             :                                 const struct auth_user_info_dc *user_info_dc,
      30             :                                 struct netr_SamBaseInfo *sam)
      31             : {
      32             :         NTSTATUS status;
      33             :         const struct auth_user_info *info;
      34             : 
      35       62035 :         ZERO_STRUCTP(sam);
      36             : 
      37       62035 :         if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX) {
      38       62035 :                 status = dom_sid_split_rid(sam, &user_info_dc->sids[PRIMARY_USER_SID_INDEX],
      39             :                                            &sam->domain_sid, &sam->rid);
      40       62035 :                 if (!NT_STATUS_IS_OK(status)) {
      41           0 :                         return status;
      42             :                 }
      43             :         } else {
      44           0 :                 return NT_STATUS_INVALID_PARAMETER;
      45             :         }
      46             : 
      47       62035 :         if (user_info_dc->num_sids > PRIMARY_GROUP_SID_INDEX) {
      48       62035 :                 status = dom_sid_split_rid(NULL, &user_info_dc->sids[PRIMARY_GROUP_SID_INDEX],
      49             :                                            NULL, &sam->primary_gid);
      50       62035 :                 if (!NT_STATUS_IS_OK(status)) {
      51           0 :                         return status;
      52             :                 }
      53             :         } else {
      54             :                 /* if we have to encode something like SYSTEM (with no
      55             :                  * second SID in the token) then this is the only
      56             :                  * choice */
      57           0 :                 sam->primary_gid = sam->rid;
      58             :         }
      59             : 
      60       62035 :         info = user_info_dc->info;
      61             : 
      62       62035 :         sam->logon_time = info->last_logon;
      63       62035 :         sam->logoff_time =  info->last_logoff;
      64       62035 :         sam->kickoff_time = info->acct_expiry;
      65       62035 :         sam->last_password_change = info->last_password_change;
      66       62035 :         sam->allow_password_change = info->allow_password_change;
      67       62035 :         sam->force_password_change = info->force_password_change;
      68             : 
      69             : #define _COPY_STRING_TALLOC(src_name, dst_name) do { \
      70             :         if (info->src_name != NULL) {\
      71             :                 sam->dst_name.string = talloc_strdup(mem_ctx, info->src_name); \
      72             :                 if (sam->dst_name.string == NULL) { \
      73             :                         return NT_STATUS_NO_MEMORY; \
      74             :                 } \
      75             :         } \
      76             : } while(0)
      77       62035 :         _COPY_STRING_TALLOC(account_name, account_name);
      78       62035 :         _COPY_STRING_TALLOC(full_name, full_name);
      79       62035 :         _COPY_STRING_TALLOC(logon_script, logon_script);
      80       62035 :         _COPY_STRING_TALLOC(profile_path, profile_path);
      81       62035 :         _COPY_STRING_TALLOC(home_directory, home_directory);
      82       62035 :         _COPY_STRING_TALLOC(home_drive, home_drive);
      83       62035 :         _COPY_STRING_TALLOC(logon_server, logon_server);
      84       62035 :         _COPY_STRING_TALLOC(domain_name, logon_domain);
      85             : #undef _COPY_STRING_TALLOC
      86             : 
      87       62035 :         sam->logon_count = info->logon_count;
      88       62035 :         sam->bad_password_count = info->bad_password_count;
      89       62035 :         sam->groups.count = 0;
      90       62035 :         sam->groups.rids = NULL;
      91             : 
      92       62035 :         if (user_info_dc->num_sids > PRIMARY_GROUP_SID_INDEX) {
      93             :                 size_t i;
      94       62035 :                 sam->groups.rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
      95             :                                                 user_info_dc->num_sids);
      96             : 
      97       62035 :                 if (sam->groups.rids == NULL)
      98           0 :                         return NT_STATUS_NO_MEMORY;
      99             : 
     100      416940 :                 for (i=PRIMARY_GROUP_SID_INDEX; i<user_info_dc->num_sids; i++) {
     101      354905 :                         struct dom_sid *group_sid = &user_info_dc->sids[i];
     102      354905 :                         if (!dom_sid_in_domain(sam->domain_sid, group_sid)) {
     103             :                                 /* We handle this elsewhere */
     104       60906 :                                 continue;
     105             :                         }
     106      586171 :                         sam->groups.rids[sam->groups.count].rid =
     107      586171 :                                 group_sid->sub_auths[group_sid->num_auths-1];
     108             : 
     109      293999 :                         sam->groups.rids[sam->groups.count].attributes =
     110             :                                 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
     111      293999 :                         sam->groups.count += 1;
     112             :                 }
     113             :         }
     114             : 
     115       62035 :         sam->user_flags = 0; /* w2k3 uses NETLOGON_EXTRA_SIDS | NETLOGON_NTLMV2_ENABLED */
     116       62035 :         if (!user_info_dc->info->authenticated) {
     117           0 :                 sam->user_flags |= NETLOGON_GUEST;
     118             :         }
     119       62035 :         sam->acct_flags = user_info_dc->info->acct_flags;
     120       62035 :         sam->sub_auth_status = 0;
     121       62035 :         sam->last_successful_logon = 0;
     122       62035 :         sam->last_failed_logon = 0;
     123       62035 :         sam->failed_logon_count = 0;
     124       62035 :         sam->reserved = 0;
     125             : 
     126       62035 :         ZERO_STRUCT(sam->key);
     127       62035 :         if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) {
     128        6043 :                 memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key));
     129             :         }
     130             : 
     131       62035 :         ZERO_STRUCT(sam->LMSessKey);
     132       62035 :         if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
     133        4824 :                 memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data,
     134             :                        sizeof(sam->LMSessKey.key));
     135             :         }
     136             : 
     137       62035 :         return NT_STATUS_OK;
     138             : }
     139             : 
     140             : /* Note that the validity of the _sam6 structure is only as long as
     141             :  * the user_info_dc it was generated from */
     142       62035 : NTSTATUS auth_convert_user_info_dc_saminfo6(TALLOC_CTX *mem_ctx,
     143             :                                             const struct auth_user_info_dc *user_info_dc,
     144             :                                             struct netr_SamInfo6 **_sam6)
     145             : {
     146             :         NTSTATUS status;
     147       62035 :         struct netr_SamInfo6 *sam6 = NULL;
     148             :         size_t i;
     149             : 
     150       62035 :         sam6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
     151       62035 :         if (sam6 == NULL) {
     152           0 :                 return NT_STATUS_NO_MEMORY;
     153             :         }
     154             : 
     155       62035 :         status = auth_convert_user_info_dc_sambaseinfo(sam6,
     156             :                                                        user_info_dc,
     157             :                                                        &sam6->base);
     158       62035 :         if (!NT_STATUS_IS_OK(status)) {
     159           0 :                 TALLOC_FREE(sam6);
     160           0 :                 return status;
     161             :         }
     162             : 
     163       62035 :         sam6->sids = talloc_array(sam6, struct netr_SidAttr,
     164             :                                   user_info_dc->num_sids);
     165       62035 :         if (sam6->sids == NULL) {
     166           0 :                 TALLOC_FREE(sam6);
     167           0 :                 return NT_STATUS_NO_MEMORY;
     168             :         }
     169             : 
     170             :         /* We don't put the user and group SIDs in there */
     171      354905 :         for (i=2; i<user_info_dc->num_sids; i++) {
     172      292870 :                 if (dom_sid_in_domain(sam6->base.domain_sid, &user_info_dc->sids[i])) {
     173      231964 :                         continue;
     174             :                 }
     175       60906 :                 sam6->sids[sam6->sidcount].sid = dom_sid_dup(sam6->sids, &user_info_dc->sids[i]);
     176       60906 :                 if (sam6->sids[sam6->sidcount].sid == NULL) {
     177           0 :                         TALLOC_FREE(sam6);
     178           0 :                         return NT_STATUS_NO_MEMORY;
     179             :                 }
     180       60906 :                 sam6->sids[sam6->sidcount].attributes =
     181             :                         SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
     182       60906 :                 sam6->sidcount += 1;
     183             :         }
     184       62035 :         if (sam6->sidcount) {
     185       54994 :                 sam6->base.user_flags |= NETLOGON_EXTRA_SIDS;
     186             :         } else {
     187        7041 :                 sam6->sids = NULL;
     188             :         }
     189             : 
     190       62035 :         if (user_info_dc->info->dns_domain_name != NULL) {
     191       62341 :                 sam6->dns_domainname.string = talloc_strdup(sam6,
     192       62035 :                                         user_info_dc->info->dns_domain_name);
     193       62035 :                 if (sam6->dns_domainname.string == NULL) {
     194           0 :                         TALLOC_FREE(sam6);
     195           0 :                         return NT_STATUS_NO_MEMORY;
     196             :                 }
     197             :         }
     198             : 
     199       62035 :         if (user_info_dc->info->user_principal_name != NULL) {
     200       62341 :                 sam6->principal_name.string = talloc_strdup(sam6,
     201       62035 :                                         user_info_dc->info->user_principal_name);
     202       62035 :                 if (sam6->principal_name.string == NULL) {
     203           0 :                         TALLOC_FREE(sam6);
     204           0 :                         return NT_STATUS_NO_MEMORY;
     205             :                 }
     206             :         }
     207             : 
     208       62035 :         *_sam6 = sam6;
     209       62035 :         return NT_STATUS_OK;
     210             : }
     211             : 
     212             : /* Note that the validity of the _sam2 structure is only as long as
     213             :  * the user_info_dc it was generated from */
     214        2433 : NTSTATUS auth_convert_user_info_dc_saminfo2(TALLOC_CTX *mem_ctx,
     215             :                                            const struct auth_user_info_dc *user_info_dc,
     216             :                                            struct netr_SamInfo2 **_sam2)
     217             : {
     218             :         NTSTATUS status;
     219        2433 :         struct netr_SamInfo6 *sam6 = NULL;
     220        2433 :         struct netr_SamInfo2 *sam2 = NULL;
     221             : 
     222        2433 :         sam2 = talloc_zero(mem_ctx, struct netr_SamInfo2);
     223        2433 :         if (sam2 == NULL) {
     224           0 :                 return NT_STATUS_NO_MEMORY;
     225             :         }
     226             : 
     227        2433 :         status = auth_convert_user_info_dc_saminfo6(sam2, user_info_dc, &sam6);
     228        2433 :         if (!NT_STATUS_IS_OK(status)) {
     229           0 :                 TALLOC_FREE(sam2);
     230           0 :                 return status;
     231             :         }
     232        2433 :         sam2->base   = sam6->base;
     233             : 
     234        2433 :         *_sam2 = sam2;
     235        2433 :         return NT_STATUS_OK;
     236             : }
     237             : 
     238             : /* Note that the validity of the _sam3 structure is only as long as
     239             :  * the user_info_dc it was generated from */
     240       57431 : NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx,
     241             :                                            const struct auth_user_info_dc *user_info_dc,
     242             :                                            struct netr_SamInfo3 **_sam3)
     243             : {
     244             :         NTSTATUS status;
     245       57431 :         struct netr_SamInfo6 *sam6 = NULL;
     246       57431 :         struct netr_SamInfo3 *sam3 = NULL;
     247             : 
     248       57431 :         sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
     249       57431 :         if (sam3 == NULL) {
     250           0 :                 return NT_STATUS_NO_MEMORY;
     251             :         }
     252             : 
     253       57431 :         status = auth_convert_user_info_dc_saminfo6(sam3, user_info_dc, &sam6);
     254       57431 :         if (!NT_STATUS_IS_OK(status)) {
     255           0 :                 TALLOC_FREE(sam3);
     256           0 :                 return status;
     257             :         }
     258       57431 :         sam3->base   = sam6->base;
     259       57431 :         sam3->sidcount       = sam6->sidcount;
     260       57431 :         sam3->sids   = sam6->sids;
     261             : 
     262       57431 :         *_sam3 = sam3;
     263       57431 :         return NT_STATUS_OK;
     264             : }
     265             : 
     266             : /**
     267             :  * Make a user_info struct from the info3 or similar returned by a domain logon.
     268             :  *
     269             :  * The netr_SamInfo3 is also a key structure in the source3 auth subsystem
     270             :  */
     271             : 
     272       60866 : NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
     273             :                                     const char *account_name,
     274             :                                     const struct netr_SamBaseInfo *base,
     275             :                                     bool authenticated,
     276             :                                     struct auth_user_info **_user_info)
     277             : {
     278             :         struct auth_user_info *info;
     279             : 
     280       60866 :         info = talloc_zero(mem_ctx, struct auth_user_info);
     281       60866 :         NT_STATUS_HAVE_NO_MEMORY(info);
     282             : 
     283       60866 :         if (base->account_name.string) {
     284       60866 :                 info->account_name = talloc_strdup(info, base->account_name.string);
     285             :         } else {
     286           0 :                 info->account_name = talloc_strdup(info, account_name);
     287             :         }
     288       60866 :         NT_STATUS_HAVE_NO_MEMORY(info->account_name);
     289             : 
     290       60866 :         if (base->logon_domain.string) {
     291       60866 :                 info->domain_name = talloc_strdup(info, base->logon_domain.string);
     292       60866 :                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
     293             :         }
     294             : 
     295       60866 :         if (base->full_name.string) {
     296       60603 :                 info->full_name = talloc_strdup(info, base->full_name.string);
     297       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
     298             :         }
     299       60866 :         if (base->logon_script.string) {
     300       60603 :                 info->logon_script = talloc_strdup(info, base->logon_script.string);
     301       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
     302             :         }
     303       60866 :         if (base->profile_path.string) {
     304       60603 :                 info->profile_path = talloc_strdup(info, base->profile_path.string);
     305       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
     306             :         }
     307       60866 :         if (base->home_directory.string) {
     308       60603 :                 info->home_directory = talloc_strdup(info, base->home_directory.string);
     309       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
     310             :         }
     311       60866 :         if (base->home_drive.string) {
     312       60603 :                 info->home_drive = talloc_strdup(info, base->home_drive.string);
     313       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
     314             :         }
     315       60866 :         if (base->logon_server.string) {
     316       60603 :                 info->logon_server = talloc_strdup(info, base->logon_server.string);
     317       60603 :                 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
     318             :         }
     319       60866 :         info->last_logon = base->logon_time;
     320       60866 :         info->last_logoff = base->logoff_time;
     321       60866 :         info->acct_expiry = base->kickoff_time;
     322       60866 :         info->last_password_change = base->last_password_change;
     323       60866 :         info->allow_password_change = base->allow_password_change;
     324       60866 :         info->force_password_change = base->force_password_change;
     325       60866 :         info->logon_count = base->logon_count;
     326       60866 :         info->bad_password_count = base->bad_password_count;
     327       60866 :         info->acct_flags = base->acct_flags;
     328             : 
     329             :         /* Only set authenticated if both NETLOGON_GUEST is not set, and authenticated is set */
     330       60866 :         info->authenticated = (authenticated && (!(base->user_flags & NETLOGON_GUEST)));
     331             : 
     332       60866 :         *_user_info = info;
     333       60866 :         return NT_STATUS_OK;
     334             : }
     335             : 
     336         404 : struct auth_user_info *auth_user_info_copy(TALLOC_CTX *mem_ctx,
     337             :                                            const struct auth_user_info *src)
     338             : {
     339         404 :         struct auth_user_info *dst = NULL;
     340             : 
     341         404 :         dst = talloc_zero(mem_ctx, struct auth_user_info);
     342         404 :         if (dst == NULL) {
     343           0 :                 return NULL;
     344             :         }
     345             : 
     346         404 :         *dst = *src;
     347             : #define _COPY_STRING(_mem, _str) do { \
     348             :         if ((_str) != NULL) { \
     349             :                 (_str) = talloc_strdup((_mem), (_str)); \
     350             :                 if ((_str) == NULL) { \
     351             :                         TALLOC_FREE(dst); \
     352             :                         return NULL; \
     353             :                 } \
     354             :         } \
     355             : } while(0)
     356         404 :         _COPY_STRING(dst, dst->account_name);
     357         404 :         _COPY_STRING(dst, dst->user_principal_name);
     358         404 :         _COPY_STRING(dst, dst->domain_name);
     359         404 :         _COPY_STRING(dst, dst->dns_domain_name);
     360         404 :         _COPY_STRING(dst, dst->full_name);
     361         404 :         _COPY_STRING(dst, dst->logon_script);
     362         404 :         _COPY_STRING(dst, dst->profile_path);
     363         404 :         _COPY_STRING(dst, dst->home_directory);
     364         404 :         _COPY_STRING(dst, dst->home_drive);
     365         404 :         _COPY_STRING(dst, dst->logon_server);
     366             : #undef _COPY_STRING
     367             : 
     368         404 :         return dst;
     369             : }
     370             : 
     371             : /**
     372             :  * Make a user_info_dc struct from the info3 returned by a domain logon
     373             :  */
     374       59389 : NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx,
     375             :                                               const char *account_name,
     376             :                                               uint16_t validation_level,
     377             :                                               const union netr_Validation *validation,
     378             :                                                bool authenticated,
     379             :                                               struct auth_user_info_dc **_user_info_dc)
     380             : {
     381             :         NTSTATUS status;
     382       59389 :         struct auth_user_info_dc *user_info_dc = NULL;
     383       59389 :         const struct netr_SamBaseInfo *base = NULL;
     384       59389 :         uint32_t sidcount = 0;
     385       59389 :         const struct netr_SidAttr *sids = NULL;
     386       59389 :         const char *dns_domainname = NULL;
     387       59389 :         const char *principal = NULL;
     388             :         uint32_t i;
     389             : 
     390       59389 :         switch (validation_level) {
     391           0 :         case 2:
     392           0 :                 if (!validation || !validation->sam2) {
     393           0 :                         return NT_STATUS_INVALID_PARAMETER;
     394             :                 }
     395           0 :                 base = &validation->sam2->base;
     396           0 :                 break;
     397       59102 :         case 3:
     398       59102 :                 if (!validation || !validation->sam3) {
     399           0 :                         return NT_STATUS_INVALID_PARAMETER;
     400             :                 }
     401       59102 :                 base = &validation->sam3->base;
     402       59102 :                 sidcount = validation->sam3->sidcount;
     403       59102 :                 sids = validation->sam3->sids;
     404       59102 :                 break;
     405         287 :         case 6:
     406         287 :                 if (!validation || !validation->sam6) {
     407           0 :                         return NT_STATUS_INVALID_PARAMETER;
     408             :                 }
     409         287 :                 base = &validation->sam6->base;
     410         287 :                 sidcount = validation->sam6->sidcount;
     411         287 :                 sids = validation->sam6->sids;
     412         287 :                 dns_domainname = validation->sam6->dns_domainname.string;
     413         287 :                 principal = validation->sam6->principal_name.string;
     414         287 :                 break;
     415           0 :         default:
     416           0 :                 return NT_STATUS_INVALID_LEVEL;
     417             :         }
     418             : 
     419       59389 :         user_info_dc = talloc_zero(mem_ctx, struct auth_user_info_dc);
     420       59389 :         NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
     421             : 
     422             :         /*
     423             :            Here is where we should check the list of
     424             :            trusted domains, and verify that the SID
     425             :            matches.
     426             :         */
     427       59389 :         if (!base->domain_sid) {
     428           0 :                 DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID"));
     429           0 :                 return NT_STATUS_INVALID_PARAMETER;
     430             :         }
     431             : 
     432             :         /* The IDL layer would be a better place to check this, but to
     433             :          * guard the integer addition below, we double-check */
     434       59389 :         if (base->groups.count > 65535) {
     435           0 :                 return NT_STATUS_INVALID_PARAMETER;
     436             :         }
     437             : 
     438       59389 :         user_info_dc->num_sids = 2;
     439             : 
     440       59389 :         user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid,  user_info_dc->num_sids + base->groups.count);
     441       59389 :         NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
     442             : 
     443       59389 :         user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid;
     444       59389 :         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) {
     445           0 :                 return NT_STATUS_INVALID_PARAMETER;
     446             :         }
     447             : 
     448       59389 :         user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid;
     449       59389 :         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) {
     450           0 :                 return NT_STATUS_INVALID_PARAMETER;
     451             :         }
     452             : 
     453      360069 :         for (i = 0; i < base->groups.count; i++) {
     454             :                 /* Skip primary group, already added above */
     455      300680 :                 if (base->groups.rids[i].rid == base->primary_gid) {
     456       59389 :                         continue;
     457             :                 }
     458      241291 :                 user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid;
     459      241291 :                 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) {
     460           0 :                         return NT_STATUS_INVALID_PARAMETER;
     461             :                 }
     462      241291 :                 user_info_dc->num_sids++;
     463             :         }
     464             : 
     465             :         /* Copy 'other' sids.  We need to do sid filtering here to
     466             :            prevent possible elevation of privileges.  See:
     467             : 
     468             :            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
     469             :          */
     470             : 
     471             :         /*
     472             :          * The IDL layer would be a better place to check this, but to
     473             :          * guard the integer addition below, we double-check
     474             :          */
     475       59389 :         if (sidcount > UINT16_MAX) {
     476           0 :                 return NT_STATUS_INVALID_PARAMETER;
     477             :         }
     478             : 
     479       59389 :         if (sidcount > 0) {
     480       59086 :                 struct dom_sid *dgrps = user_info_dc->sids;
     481             :                 size_t dgrps_count;
     482             : 
     483       59086 :                 dgrps_count = user_info_dc->num_sids + sidcount;
     484       59086 :                 dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid,
     485             :                                        dgrps_count);
     486       59086 :                 if (dgrps == NULL) {
     487           0 :                         return NT_STATUS_NO_MEMORY;
     488             :                 }
     489             : 
     490      123858 :                 for (i = 0; i < sidcount; i++) {
     491       64772 :                         if (sids[i].sid) {
     492       64772 :                                 dgrps[user_info_dc->num_sids] = *sids[i].sid;
     493       64772 :                                 user_info_dc->num_sids++;
     494             :                         }
     495             :                 }
     496             : 
     497       59086 :                 user_info_dc->sids = dgrps;
     498             : 
     499             :                 /* Where are the 'global' sids?... */
     500             :         }
     501             : 
     502       59389 :         status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info);
     503       59389 :         if (!NT_STATUS_IS_OK(status)) {
     504           0 :                 return status;
     505             :         }
     506             : 
     507       59389 :         if (dns_domainname != NULL) {
     508         287 :                 user_info_dc->info->dns_domain_name = talloc_strdup(user_info_dc->info,
     509             :                                                                     dns_domainname);
     510         287 :                 if (user_info_dc->info->dns_domain_name == NULL) {
     511           0 :                         return NT_STATUS_NO_MEMORY;
     512             :                 }
     513             :         }
     514             : 
     515       59389 :         if (principal != NULL) {
     516         287 :                 user_info_dc->info->user_principal_name = talloc_strdup(user_info_dc->info,
     517             :                                                                         principal);
     518         287 :                 if (user_info_dc->info->user_principal_name == NULL) {
     519           0 :                         return NT_STATUS_NO_MEMORY;
     520             :                 }
     521             :         }
     522             : 
     523             :         /* ensure we are never given NULL session keys */
     524             : 
     525       59389 :         if (all_zero(base->key.key, sizeof(base->key.key))) {
     526       59170 :                 user_info_dc->user_session_key = data_blob(NULL, 0);
     527             :         } else {
     528         219 :                 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key));
     529         219 :                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
     530             :         }
     531             : 
     532       59389 :         if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
     533       59180 :                 user_info_dc->lm_session_key = data_blob(NULL, 0);
     534             :         } else {
     535         209 :                 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key));
     536         209 :                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
     537             :         }
     538             : 
     539       59389 :         *_user_info_dc = user_info_dc;
     540       59389 :         return NT_STATUS_OK;
     541             : }
     542             : 
     543             : /**
     544             :  * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon
     545             :  */
     546       59086 : NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx,
     547             :                               const struct PAC_LOGON_INFO *pac_logon_info,
     548             :                               const struct PAC_UPN_DNS_INFO *pac_upn_dns_info,
     549             :                               struct auth_user_info_dc **_user_info_dc)
     550             : {
     551             :         uint32_t i;
     552             :         NTSTATUS nt_status;
     553             :         union netr_Validation validation;
     554             :         struct auth_user_info_dc *user_info_dc;
     555       59086 :         const struct PAC_DOMAIN_GROUP_MEMBERSHIP *rg = NULL;
     556             :         size_t sidcount;
     557             : 
     558       59086 :         rg = &pac_logon_info->resource_groups;
     559             : 
     560       59086 :         validation.sam3 = discard_const_p(struct netr_SamInfo3, &pac_logon_info->info3);
     561             : 
     562       59086 :         nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation,
     563             :                                                           true, /* This user was authenticated */
     564             :                                                           &user_info_dc);
     565       59086 :         if (!NT_STATUS_IS_OK(nt_status)) {
     566           0 :                 return nt_status;
     567             :         }
     568             : 
     569       59086 :         if (pac_logon_info->info3.base.user_flags & NETLOGON_RESOURCE_GROUPS) {
     570           0 :                 rg = &pac_logon_info->resource_groups;
     571             :         }
     572             : 
     573       59086 :         if (rg == NULL) {
     574           0 :                 *_user_info_dc = user_info_dc;
     575           0 :                 return NT_STATUS_OK;
     576             :         }
     577             : 
     578       59086 :         if (rg->groups.count > 0) {
     579             :                 /* The IDL layer would be a better place to check this, but to
     580             :                  * guard the integer addition below, we double-check */
     581           0 :                 if (rg->groups.count > 65535) {
     582           0 :                         talloc_free(user_info_dc);
     583           0 :                         return NT_STATUS_INVALID_PARAMETER;
     584             :                 }
     585             : 
     586             :                 /*
     587             :                   Here is where we should check the list of
     588             :                   trusted domains, and verify that the SID
     589             :                   matches.
     590             :                 */
     591           0 :                 if (rg->domain_sid == NULL) {
     592           0 :                         talloc_free(user_info_dc);
     593           0 :                         DEBUG(0, ("Cannot operate on a PAC without a resource domain SID"));
     594           0 :                         return NT_STATUS_INVALID_PARAMETER;
     595             :                 }
     596             : 
     597           0 :                 sidcount = user_info_dc->num_sids + rg->groups.count;
     598           0 :                 user_info_dc->sids
     599           0 :                         = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount);
     600           0 :                 if (user_info_dc->sids == NULL) {
     601           0 :                         TALLOC_FREE(user_info_dc);
     602           0 :                         return NT_STATUS_NO_MEMORY;
     603             :                 }
     604             : 
     605           0 :                 for (i = 0; i < rg->groups.count; i++) {
     606             :                         bool ok;
     607             : 
     608           0 :                         user_info_dc->sids[user_info_dc->num_sids] = *rg->domain_sid;
     609           0 :                         ok = sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
     610           0 :                                             rg->groups.rids[i].rid);
     611           0 :                         if (!ok) {
     612           0 :                                 return NT_STATUS_INVALID_PARAMETER;
     613             :                         }
     614           0 :                         user_info_dc->num_sids++;
     615             :                 }
     616             :         }
     617             : 
     618       59086 :         if (pac_upn_dns_info != NULL) {
     619       59086 :                 if (pac_upn_dns_info->upn_name != NULL) {
     620      118172 :                         user_info_dc->info->user_principal_name =
     621      114123 :                                 talloc_strdup(user_info_dc->info,
     622        4049 :                                               pac_upn_dns_info->upn_name);
     623       59086 :                         if (user_info_dc->info->user_principal_name == NULL) {
     624           0 :                                 return NT_STATUS_NO_MEMORY;
     625             :                         }
     626             :                 }
     627             : 
     628      118172 :                 user_info_dc->info->dns_domain_name =
     629      114123 :                         talloc_strdup(user_info_dc->info,
     630        4049 :                                       pac_upn_dns_info->dns_domain_name);
     631       59086 :                 if (user_info_dc->info->dns_domain_name == NULL) {
     632           0 :                         return NT_STATUS_NO_MEMORY;
     633             :                 }
     634             : 
     635       59086 :                 if (pac_upn_dns_info->flags & PAC_UPN_DNS_FLAG_CONSTRUCTED) {
     636       55443 :                         user_info_dc->info->user_principal_constructed = true;
     637             :                 }
     638             :         }
     639             : 
     640       59086 :         *_user_info_dc = user_info_dc;
     641       59086 :         return NT_STATUS_OK;
     642             : }

Generated by: LCOV version 1.13