Add a few more safety catches to the mkrelease.sh script.
[jelmer/samba4-debian.git] / source / auth / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Authentication utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Andrew Bartlett 2001
6    Copyright (C) Jeremy Allison 2000-2001
7    Copyright (C) Rafal Szczesniak 2002
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "auth/credentials/credentials.h"
30 #include "param/param.h"
31
32 struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx, 
33                                             struct loadparm_context *lp_ctx) 
34 {
35         NTSTATUS nt_status;
36         struct auth_session_info *session_info = NULL;
37         nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
38         if (!NT_STATUS_IS_OK(nt_status)) {
39                 return NULL;
40         }
41         return session_info;
42 }
43
44 NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx, 
45                                      struct loadparm_context *lp_ctx,
46                                      struct auth_session_info **_session_info) 
47 {
48         NTSTATUS nt_status;
49         struct auth_serversupplied_info *server_info = NULL;
50         struct auth_session_info *session_info = NULL;
51         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
52         
53         nt_status = auth_anonymous_server_info(mem_ctx,
54                                                lp_netbios_name(lp_ctx),
55                                                &server_info);
56         if (!NT_STATUS_IS_OK(nt_status)) {
57                 talloc_free(mem_ctx);
58                 return nt_status;
59         }
60
61         /* references the server_info into the session_info */
62         nt_status = auth_generate_session_info(parent_ctx, lp_ctx, server_info, &session_info);
63         talloc_free(mem_ctx);
64
65         NT_STATUS_NOT_OK_RETURN(nt_status);
66
67         session_info->credentials = cli_credentials_init(session_info);
68         if (!session_info->credentials) {
69                 return NT_STATUS_NO_MEMORY;
70         }
71
72         cli_credentials_set_conf(session_info->credentials, lp_ctx);
73         cli_credentials_set_anonymous(session_info->credentials);
74         
75         *_session_info = session_info;
76
77         return NT_STATUS_OK;
78 }
79
80 NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx, 
81                                     const char *netbios_name,
82                                     struct auth_serversupplied_info **_server_info) 
83 {
84         struct auth_serversupplied_info *server_info;
85         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
86         NT_STATUS_HAVE_NO_MEMORY(server_info);
87
88         server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
89         NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
90
91         /* is this correct? */
92         server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
93         NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
94
95         server_info->n_domain_groups = 0;
96         server_info->domain_groups = NULL;
97
98         /* annoying, but the Anonymous really does have a session key, 
99            and it is all zeros! */
100         server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
101         NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
102
103         server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
104         NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
105
106         data_blob_clear(&server_info->user_session_key);
107         data_blob_clear(&server_info->lm_session_key);
108
109         server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
110         NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
111
112         server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
113         NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
114
115         server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
116         NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
117
118         server_info->logon_script = talloc_strdup(server_info, "");
119         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
120
121         server_info->profile_path = talloc_strdup(server_info, "");
122         NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
123
124         server_info->home_directory = talloc_strdup(server_info, "");
125         NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
126
127         server_info->home_drive = talloc_strdup(server_info, "");
128         NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
129
130         server_info->logon_server = talloc_strdup(server_info, netbios_name);
131         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
132
133         server_info->last_logon = 0;
134         server_info->last_logoff = 0;
135         server_info->acct_expiry = 0;
136         server_info->last_password_change = 0;
137         server_info->allow_password_change = 0;
138         server_info->force_password_change = 0;
139
140         server_info->logon_count = 0;
141         server_info->bad_password_count = 0;
142
143         server_info->acct_flags = ACB_NORMAL;
144
145         server_info->authenticated = false;
146
147         *_server_info = server_info;
148
149         return NT_STATUS_OK;
150 }
151
152 NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx, 
153                                     struct loadparm_context *lp_ctx,
154                                     struct auth_serversupplied_info *server_info, 
155                                     struct auth_session_info **_session_info) 
156 {
157         struct auth_session_info *session_info;
158         NTSTATUS nt_status;
159
160         session_info = talloc(mem_ctx, struct auth_session_info);
161         NT_STATUS_HAVE_NO_MEMORY(session_info);
162
163         session_info->server_info = talloc_reference(session_info, server_info);
164
165         /* unless set otherwise, the session key is the user session
166          * key from the auth subsystem */ 
167         session_info->session_key = server_info->user_session_key;
168
169         nt_status = security_token_create(session_info,
170                                           lp_ctx,
171                                           server_info->account_sid,
172                                           server_info->primary_group_sid,
173                                           server_info->n_domain_groups,
174                                           server_info->domain_groups,
175                                           server_info->authenticated,
176                                           &session_info->security_token);
177         NT_STATUS_NOT_OK_RETURN(nt_status);
178
179         session_info->credentials = NULL;
180
181         *_session_info = session_info;
182         return NT_STATUS_OK;
183 }
184
185 /**
186  * prints a struct auth_session_info security token to debug output.
187  */
188 void auth_session_info_debug(int dbg_lev, 
189                              const struct auth_session_info *session_info)
190 {
191         if (!session_info) {
192                 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
193                 return; 
194         }
195
196         security_token_debug(dbg_lev, session_info->security_token);
197 }
198
199 /**
200  * Make a server_info struct from the info3 returned by a domain logon 
201  */
202 NTSTATUS make_server_info_netlogon_validation(TALLOC_CTX *mem_ctx,
203                                               const char *account_name,
204                                               uint16_t validation_level,
205                                               union netr_Validation *validation,
206                                               struct auth_serversupplied_info **_server_info)
207 {
208         struct auth_serversupplied_info *server_info;
209         struct netr_SamBaseInfo *base = NULL;
210         int i;
211
212         switch (validation_level) {
213         case 2:
214                 if (!validation || !validation->sam2) {
215                         return NT_STATUS_INVALID_PARAMETER;
216                 }
217                 base = &validation->sam2->base;
218                 break;
219         case 3:
220                 if (!validation || !validation->sam3) {
221                         return NT_STATUS_INVALID_PARAMETER;
222                 }
223                 base = &validation->sam3->base;
224                 break;
225         case 6:
226                 if (!validation || !validation->sam6) {
227                         return NT_STATUS_INVALID_PARAMETER;
228                 }
229                 base = &validation->sam6->base;
230                 break;
231         default:
232                 return NT_STATUS_INVALID_LEVEL;
233         }
234
235         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
236         NT_STATUS_HAVE_NO_MEMORY(server_info);
237
238         /*
239            Here is where we should check the list of
240            trusted domains, and verify that the SID 
241            matches.
242         */
243         server_info->account_sid = dom_sid_add_rid(server_info, base->domain_sid, base->rid);
244         NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
245
246
247         server_info->primary_group_sid = dom_sid_add_rid(server_info, base->domain_sid, base->primary_gid);
248         NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
249
250         server_info->n_domain_groups = base->groups.count;
251         if (base->groups.count) {
252                 server_info->domain_groups = talloc_array(server_info, struct dom_sid*, base->groups.count);
253                 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups);
254         } else {
255                 server_info->domain_groups = NULL;
256         }
257
258         for (i = 0; i < base->groups.count; i++) {
259                 server_info->domain_groups[i] = dom_sid_add_rid(server_info, base->domain_sid, base->groups.rids[i].rid);
260                 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups[i]);
261         }
262
263         /* Copy 'other' sids.  We need to do sid filtering here to
264            prevent possible elevation of privileges.  See:
265
266            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
267          */
268
269         if (validation_level == 3) {
270                 struct dom_sid **dgrps = server_info->domain_groups;
271                 size_t sidcount = server_info->n_domain_groups + validation->sam3->sidcount;
272                 size_t n_dgrps = server_info->n_domain_groups;
273
274                 if (validation->sam3->sidcount > 0) {
275                         dgrps = talloc_realloc(server_info, dgrps, struct dom_sid*, sidcount);
276                         NT_STATUS_HAVE_NO_MEMORY(dgrps);
277
278                         for (i = 0; i < validation->sam3->sidcount; i++) {
279                                 dgrps[n_dgrps + i] = talloc_reference(dgrps, validation->sam3->sids[i].sid);
280                         }
281                 }
282
283                 server_info->n_domain_groups = sidcount;
284                 server_info->domain_groups = dgrps;
285
286                 /* Where are the 'global' sids?... */
287         }
288
289         if (base->account_name.string) {
290                 server_info->account_name = talloc_reference(server_info, base->account_name.string);
291         } else {
292                 server_info->account_name = talloc_strdup(server_info, account_name);
293                 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
294         }
295
296         server_info->domain_name = talloc_reference(server_info, base->domain.string);
297         server_info->full_name = talloc_reference(server_info, base->full_name.string);
298         server_info->logon_script = talloc_reference(server_info, base->logon_script.string);
299         server_info->profile_path = talloc_reference(server_info, base->profile_path.string);
300         server_info->home_directory = talloc_reference(server_info, base->home_directory.string);
301         server_info->home_drive = talloc_reference(server_info, base->home_drive.string);
302         server_info->logon_server = talloc_reference(server_info, base->logon_server.string);
303         server_info->last_logon = base->last_logon;
304         server_info->last_logoff = base->last_logoff;
305         server_info->acct_expiry = base->acct_expiry;
306         server_info->last_password_change = base->last_password_change;
307         server_info->allow_password_change = base->allow_password_change;
308         server_info->force_password_change = base->force_password_change;
309         server_info->logon_count = base->logon_count;
310         server_info->bad_password_count = base->bad_password_count;
311         server_info->acct_flags = base->acct_flags;
312
313         server_info->authenticated = true;
314
315         /* ensure we are never given NULL session keys */
316
317         if (all_zero(base->key.key, sizeof(base->key.key))) {
318                 server_info->user_session_key = data_blob(NULL, 0);
319         } else {
320                 server_info->user_session_key = data_blob_talloc(server_info, base->key.key, sizeof(base->key.key));
321                 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
322         }
323
324         if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
325                 server_info->lm_session_key = data_blob(NULL, 0);
326         } else {
327                 server_info->lm_session_key = data_blob_talloc(server_info, base->LMSessKey.key, sizeof(base->LMSessKey.key));
328                 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
329         }
330
331         *_server_info = server_info;
332         return NT_STATUS_OK;
333 }
334
335