r14860: create libcli/security/security.h
[kai/samba-autobuild/.git] / source4 / auth / auth_sam_reply.c
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
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27
28 NTSTATUS auth_convert_server_info_sambaseinfo(TALLOC_CTX *mem_ctx, 
29                                               struct auth_serversupplied_info *server_info, 
30                                               struct netr_SamBaseInfo **_sam)
31 {
32         struct netr_SamBaseInfo *sam = talloc_zero(mem_ctx, struct netr_SamBaseInfo);
33         NT_STATUS_HAVE_NO_MEMORY(sam);
34
35         sam->domain_sid = dom_sid_dup(mem_ctx, server_info->account_sid);
36         NT_STATUS_HAVE_NO_MEMORY(sam->domain_sid);
37         sam->domain_sid->num_auths--;
38
39         sam->last_logon = server_info->last_logon;
40         sam->last_logoff = server_info->last_logoff;
41         sam->acct_expiry = server_info->acct_expiry;
42         sam->last_password_change = server_info->last_password_change;
43         sam->allow_password_change = server_info->allow_password_change;
44         sam->force_password_change = server_info->force_password_change;
45
46         sam->account_name.string = server_info->account_name;
47         sam->full_name.string = server_info->full_name;
48         sam->logon_script.string = server_info->logon_script;
49         sam->profile_path.string = server_info->profile_path;
50         sam->home_directory.string = server_info->home_directory;
51         sam->home_drive.string = server_info->home_drive;
52
53         sam->logon_count = server_info->logon_count;
54         sam->bad_password_count = sam->bad_password_count;
55         sam->rid = server_info->account_sid->sub_auths[server_info->account_sid->num_auths-1];
56         sam->primary_gid = server_info->primary_group_sid->sub_auths[server_info->primary_group_sid->num_auths-1];
57
58         sam->groups.count = 0;
59         sam->groups.rids = NULL;
60
61         if (server_info->n_domain_groups > 0) {
62                 int i;
63                 sam->groups.rids = talloc_array(sam, struct samr_RidWithAttribute,
64                                                 server_info->n_domain_groups);
65
66                 if (sam->groups.rids == NULL)
67                         return NT_STATUS_NO_MEMORY;
68
69                 for (i=0; i<server_info->n_domain_groups; i++) {
70                         struct dom_sid *group_sid = server_info->domain_groups[i];
71                         if (!dom_sid_in_domain(sam->domain_sid, group_sid)) {
72                                 /* We handle this elsewhere */
73                                 continue;
74                         }
75                         sam->groups.rids[sam->groups.count].rid =
76                                 group_sid->sub_auths[group_sid->num_auths-1];
77                         
78                         sam->groups.rids[sam->groups.count].attributes = 
79                                 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
80                         sam->groups.count += 1;
81                 }
82         }
83
84         sam->user_flags = 0; /* TODO: w2k3 uses 0x120.  We know 0x20
85                               * as extra sids (PAC doc) but what is
86                               * 0x100? */
87         sam->acct_flags = server_info->acct_flags;
88         sam->logon_server.string = server_info->logon_server;
89         sam->domain.string = server_info->domain_name;
90
91         ZERO_STRUCT(sam->unknown);
92
93         ZERO_STRUCT(sam->key);
94         if (server_info->user_session_key.length == sizeof(sam->key.key)) {
95                 memcpy(sam->key.key, server_info->user_session_key.data, sizeof(sam->key.key));
96         }
97
98         ZERO_STRUCT(sam->LMSessKey);
99         if (server_info->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
100                 memcpy(sam->LMSessKey.key, server_info->lm_session_key.data, 
101                        sizeof(sam->LMSessKey.key));
102         }
103         
104         *_sam = sam;
105
106         return NT_STATUS_OK;
107 }       
108
109 NTSTATUS auth_convert_server_info_saminfo3(TALLOC_CTX *mem_ctx, 
110                                            struct auth_serversupplied_info *server_info, 
111                                            struct netr_SamInfo3 **_sam3)
112 {
113         struct netr_SamBaseInfo *sam;
114         struct netr_SamInfo3 *sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
115         NTSTATUS status;
116         int i;
117         NT_STATUS_HAVE_NO_MEMORY(sam3);
118
119         status = auth_convert_server_info_sambaseinfo(mem_ctx, server_info, &sam);
120         if (!NT_STATUS_IS_OK(status)) {
121                 return status;
122         }
123         sam3->base = *sam;
124         sam3->sidcount  = 0;
125         sam3->sids      = NULL;
126
127         
128         sam3->sids = talloc_array(sam, struct netr_SidAttr,
129                                   server_info->n_domain_groups);
130         NT_STATUS_HAVE_NO_MEMORY(sam3->sids);
131         
132         for (i=0; i<server_info->n_domain_groups; i++) {
133                 if (dom_sid_in_domain(sam->domain_sid, server_info->domain_groups[i])) {
134                         continue;
135                 }
136                 sam3->sids[sam3->sidcount].sid = talloc_reference(sam3->sids,server_info->domain_groups[i]);
137                 sam3->sids[sam3->sidcount].attribute = 
138                         SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
139                 sam3->sidcount += 1;
140         }
141         if (sam3->sidcount) {
142                 sam3->base.user_flags |= NETLOGON_EXTRA_SIDS;
143         } else {
144                 sam3->sids = NULL;
145         }
146         *_sam3 = sam3;
147
148         return NT_STATUS_OK;
149 }       
150