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
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.
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.
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/>.
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 #include "auth/session_proto.h"
33 _PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx,
34 struct loadparm_context *lp_ctx)
37 struct auth_session_info *session_info = NULL;
38 nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
39 if (!NT_STATUS_IS_OK(nt_status)) {
45 _PUBLIC_ NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx,
46 struct loadparm_context *lp_ctx,
47 struct auth_session_info **_session_info)
50 struct auth_serversupplied_info *server_info = NULL;
51 struct auth_session_info *session_info = NULL;
52 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
54 nt_status = auth_anonymous_server_info(mem_ctx,
55 lp_netbios_name(lp_ctx),
57 if (!NT_STATUS_IS_OK(nt_status)) {
62 /* references the server_info into the session_info */
63 nt_status = auth_generate_session_info(parent_ctx, lp_ctx, server_info, &session_info);
66 NT_STATUS_NOT_OK_RETURN(nt_status);
68 session_info->credentials = cli_credentials_init(session_info);
69 if (!session_info->credentials) {
70 return NT_STATUS_NO_MEMORY;
73 cli_credentials_set_conf(session_info->credentials, lp_ctx);
74 cli_credentials_set_anonymous(session_info->credentials);
76 *_session_info = session_info;
81 _PUBLIC_ NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx,
82 const char *netbios_name,
83 struct auth_serversupplied_info **_server_info)
85 struct auth_serversupplied_info *server_info;
86 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
87 NT_STATUS_HAVE_NO_MEMORY(server_info);
89 server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
90 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
92 /* is this correct? */
93 server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
94 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
96 server_info->n_domain_groups = 0;
97 server_info->domain_groups = NULL;
99 /* annoying, but the Anonymous really does have a session key,
100 and it is all zeros! */
101 server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
102 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
104 server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
105 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
107 data_blob_clear(&server_info->user_session_key);
108 data_blob_clear(&server_info->lm_session_key);
110 server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
111 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
113 server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
114 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
116 server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
117 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
119 server_info->logon_script = talloc_strdup(server_info, "");
120 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
122 server_info->profile_path = talloc_strdup(server_info, "");
123 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
125 server_info->home_directory = talloc_strdup(server_info, "");
126 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
128 server_info->home_drive = talloc_strdup(server_info, "");
129 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
131 server_info->logon_server = talloc_strdup(server_info, netbios_name);
132 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
134 server_info->last_logon = 0;
135 server_info->last_logoff = 0;
136 server_info->acct_expiry = 0;
137 server_info->last_password_change = 0;
138 server_info->allow_password_change = 0;
139 server_info->force_password_change = 0;
141 server_info->logon_count = 0;
142 server_info->bad_password_count = 0;
144 server_info->acct_flags = ACB_NORMAL;
146 server_info->authenticated = false;
148 *_server_info = server_info;
153 _PUBLIC_ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
154 struct loadparm_context *lp_ctx,
155 struct auth_serversupplied_info *server_info,
156 struct auth_session_info **_session_info)
158 struct auth_session_info *session_info;
161 session_info = talloc(mem_ctx, struct auth_session_info);
162 NT_STATUS_HAVE_NO_MEMORY(session_info);
164 session_info->server_info = talloc_reference(session_info, server_info);
166 /* unless set otherwise, the session key is the user session
167 * key from the auth subsystem */
168 session_info->session_key = server_info->user_session_key;
170 nt_status = security_token_create(session_info,
172 server_info->account_sid,
173 server_info->primary_group_sid,
174 server_info->n_domain_groups,
175 server_info->domain_groups,
176 server_info->authenticated,
177 &session_info->security_token);
178 NT_STATUS_NOT_OK_RETURN(nt_status);
180 session_info->credentials = NULL;
182 *_session_info = session_info;
187 * prints a struct auth_session_info security token to debug output.
189 void auth_session_info_debug(int dbg_lev,
190 const struct auth_session_info *session_info)
193 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
197 security_token_debug(dbg_lev, session_info->security_token);
201 * Make a server_info struct from the info3 returned by a domain logon
203 _PUBLIC_ NTSTATUS make_server_info_netlogon_validation(TALLOC_CTX *mem_ctx,
204 const char *account_name,
205 uint16_t validation_level,
206 union netr_Validation *validation,
207 struct auth_serversupplied_info **_server_info)
209 struct auth_serversupplied_info *server_info;
210 struct netr_SamBaseInfo *base = NULL;
213 switch (validation_level) {
215 if (!validation || !validation->sam2) {
216 return NT_STATUS_INVALID_PARAMETER;
218 base = &validation->sam2->base;
221 if (!validation || !validation->sam3) {
222 return NT_STATUS_INVALID_PARAMETER;
224 base = &validation->sam3->base;
227 if (!validation || !validation->sam6) {
228 return NT_STATUS_INVALID_PARAMETER;
230 base = &validation->sam6->base;
233 return NT_STATUS_INVALID_LEVEL;
236 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
237 NT_STATUS_HAVE_NO_MEMORY(server_info);
240 Here is where we should check the list of
241 trusted domains, and verify that the SID
244 server_info->account_sid = dom_sid_add_rid(server_info, base->domain_sid, base->rid);
245 NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
248 server_info->primary_group_sid = dom_sid_add_rid(server_info, base->domain_sid, base->primary_gid);
249 NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
251 server_info->n_domain_groups = base->groups.count;
252 if (base->groups.count) {
253 server_info->domain_groups = talloc_array(server_info, struct dom_sid*, base->groups.count);
254 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups);
256 server_info->domain_groups = NULL;
259 for (i = 0; i < base->groups.count; i++) {
260 server_info->domain_groups[i] = dom_sid_add_rid(server_info, base->domain_sid, base->groups.rids[i].rid);
261 NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups[i]);
264 /* Copy 'other' sids. We need to do sid filtering here to
265 prevent possible elevation of privileges. See:
267 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
270 if (validation_level == 3) {
271 struct dom_sid **dgrps = server_info->domain_groups;
272 size_t sidcount = server_info->n_domain_groups + validation->sam3->sidcount;
273 size_t n_dgrps = server_info->n_domain_groups;
275 if (validation->sam3->sidcount > 0) {
276 dgrps = talloc_realloc(server_info, dgrps, struct dom_sid*, sidcount);
277 NT_STATUS_HAVE_NO_MEMORY(dgrps);
279 for (i = 0; i < validation->sam3->sidcount; i++) {
280 dgrps[n_dgrps + i] = talloc_reference(dgrps, validation->sam3->sids[i].sid);
284 server_info->n_domain_groups = sidcount;
285 server_info->domain_groups = dgrps;
287 /* Where are the 'global' sids?... */
290 if (base->account_name.string) {
291 server_info->account_name = talloc_reference(server_info, base->account_name.string);
293 server_info->account_name = talloc_strdup(server_info, account_name);
294 NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
297 server_info->domain_name = talloc_reference(server_info, base->domain.string);
298 server_info->full_name = talloc_reference(server_info, base->full_name.string);
299 server_info->logon_script = talloc_reference(server_info, base->logon_script.string);
300 server_info->profile_path = talloc_reference(server_info, base->profile_path.string);
301 server_info->home_directory = talloc_reference(server_info, base->home_directory.string);
302 server_info->home_drive = talloc_reference(server_info, base->home_drive.string);
303 server_info->logon_server = talloc_reference(server_info, base->logon_server.string);
304 server_info->last_logon = base->last_logon;
305 server_info->last_logoff = base->last_logoff;
306 server_info->acct_expiry = base->acct_expiry;
307 server_info->last_password_change = base->last_password_change;
308 server_info->allow_password_change = base->allow_password_change;
309 server_info->force_password_change = base->force_password_change;
310 server_info->logon_count = base->logon_count;
311 server_info->bad_password_count = base->bad_password_count;
312 server_info->acct_flags = base->acct_flags;
314 server_info->authenticated = true;
316 /* ensure we are never given NULL session keys */
318 if (all_zero(base->key.key, sizeof(base->key.key))) {
319 server_info->user_session_key = data_blob(NULL, 0);
321 server_info->user_session_key = data_blob_talloc(server_info, base->key.key, sizeof(base->key.key));
322 NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
325 if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
326 server_info->lm_session_key = data_blob(NULL, 0);
328 server_info->lm_session_key = data_blob_talloc(server_info, base->LMSessKey.key, sizeof(base->LMSessKey.key));
329 NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
332 *_server_info = server_info;