xref: /illumos-gate/usr/src/lib/smbsrv/libmlsvc/common/netr_auth.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * NETR challenge/response client functions.
30  *
31  * NT_STATUS_INVALID_PARAMETER
32  * NT_STATUS_NO_TRUST_SAM_ACCOUNT
33  * NT_STATUS_ACCESS_DENIED
34  */
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 
42 #include <smbsrv/libsmb.h>
43 #include <smbsrv/mlsvc_util.h>
44 #include <smbsrv/ndl/netlogon.ndl>
45 #include <smbsrv/ntstatus.h>
46 #include <smbsrv/smbinfo.h>
47 #include <smbsrv/mlsvc.h>
48 #include <smbsrv/netrauth.h>
49 
50 int netr_setup_authenticator(netr_info_t *, struct netr_authenticator *,
51     struct netr_authenticator *);
52 DWORD netr_validate_chain(netr_info_t *, struct netr_authenticator *);
53 
54 static int netr_server_req_challenge(mlsvc_handle_t *, netr_info_t *);
55 static int netr_server_authenticate2(mlsvc_handle_t *, netr_info_t *);
56 static int netr_gen_password(BYTE *, BYTE *, BYTE *);
57 
58 /*
59  * Shared with netr_logon.c
60  */
61 netr_info_t netr_global_info;
62 
63 /*
64  * netlogon_auth
65  *
66  * This is the core of the NETLOGON authentication protocol.
67  * Do the challenge response authentication.
68  *
69  * Prior to calling this function, an anonymous session to the NETLOGON
70  * pipe on a domain controller(server) should have already been opened.
71  */
72 DWORD
73 netlogon_auth(char *server, mlsvc_handle_t *netr_handle, DWORD flags)
74 {
75 	netr_info_t *netr_info;
76 	int rc;
77 	DWORD random_challenge[2];
78 
79 	netr_info = &netr_global_info;
80 	bzero(netr_info, sizeof (netr_info_t));
81 
82 	netr_info->flags |= flags;
83 
84 	rc = smb_getnetbiosname(netr_info->hostname, MLSVC_DOMAIN_NAME_MAX);
85 	if (rc != 0)
86 		return (NT_STATUS_UNSUCCESSFUL);
87 
88 	(void) snprintf(netr_info->server, sizeof (netr_info->server),
89 	    "\\\\%s", server);
90 
91 	random_challenge[0] = random();
92 	random_challenge[1] = random();
93 
94 	(void) memcpy(&netr_info->client_challenge, random_challenge,
95 	    sizeof (struct netr_credential));
96 
97 	if ((rc = netr_server_req_challenge(netr_handle, netr_info)) == 0) {
98 		rc = netr_server_authenticate2(netr_handle, netr_info);
99 		if (rc == 0)
100 			netr_info->flags |= NETR_FLG_VALID;
101 	}
102 
103 	return ((rc) ? NT_STATUS_UNSUCCESSFUL : NT_STATUS_SUCCESS);
104 }
105 
106 /*
107  * netr_open
108  *
109  * Open an anonymous session to the NETLOGON pipe on a domain
110  * controller and bind to the NETR RPC interface. We store the
111  * remote server's native OS type - we may need it due to
112  * differences between versions of Windows.
113  */
114 int
115 netr_open(char *server, char *domain, mlsvc_handle_t *netr_handle)
116 {
117 	int fid;
118 	int remote_os = 0;
119 	int remote_lm = 0;
120 	int server_pdc;
121 	char *username;
122 
123 	if (mlsvc_anonymous_logon(server, domain, &username) != 0)
124 		return (-1);
125 
126 	fid = mlsvc_open_pipe(server, domain, username, "\\NETLOGON");
127 	if (fid < 0)
128 		return (-1);
129 
130 	if (mlsvc_rpc_bind(netr_handle, fid, "NETR") < 0) {
131 		(void) mlsvc_close_pipe(fid);
132 		return (-1);
133 	}
134 
135 	(void) mlsvc_session_native_values(fid, &remote_os, &remote_lm,
136 	    &server_pdc);
137 	netr_handle->context->server_os = remote_os;
138 	netr_handle->context->server_pdc = server_pdc;
139 	return (0);
140 }
141 
142 /*
143  * netr_close
144  *
145  * Close a NETLOGON pipe and free the RPC context.
146  */
147 int
148 netr_close(mlsvc_handle_t *netr_handle)
149 {
150 	(void) mlsvc_close_pipe(netr_handle->context->fid);
151 	free(netr_handle->context);
152 	return (0);
153 }
154 
155 /*
156  * netr_server_req_challenge
157  */
158 static int
159 netr_server_req_challenge(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
160 {
161 	struct netr_ServerReqChallenge arg;
162 	mlrpc_heapref_t heap;
163 	int opnum;
164 	int rc;
165 
166 	bzero(&arg, sizeof (struct netr_ServerReqChallenge));
167 	opnum = NETR_OPNUM_ServerReqChallenge;
168 
169 	arg.servername = (unsigned char *)netr_info->server;
170 	arg.hostname = (unsigned char *)netr_info->hostname;
171 
172 	(void) memcpy(&arg.client_challenge, &netr_info->client_challenge,
173 	    sizeof (struct netr_credential));
174 
175 	(void) mlsvc_rpc_init(&heap);
176 	rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap);
177 	if (rc == 0) {
178 		if (arg.status != 0) {
179 			mlsvc_rpc_report_status(opnum, arg.status);
180 			rc = -1;
181 		} else {
182 			(void) memcpy(&netr_info->server_challenge,
183 			    &arg.server_challenge,
184 			    sizeof (struct netr_credential));
185 		}
186 	}
187 
188 	mlsvc_rpc_free(netr_handle->context, &heap);
189 	return (rc);
190 }
191 
192 /*
193  * netr_server_authenticate2
194  */
195 static int
196 netr_server_authenticate2(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
197 {
198 	struct netr_ServerAuthenticate2 arg;
199 	mlrpc_heapref_t heap;
200 	int opnum;
201 	int rc;
202 	char account_name[MLSVC_DOMAIN_NAME_MAX * 2];
203 
204 	bzero(&arg, sizeof (struct netr_ServerAuthenticate2));
205 	opnum = NETR_OPNUM_ServerAuthenticate2;
206 
207 	(void) snprintf(account_name, sizeof (account_name), "%s$",
208 	    netr_info->hostname);
209 
210 	arg.servername = (unsigned char *)netr_info->server;
211 	arg.account_name = (unsigned char *)account_name;
212 	arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
213 	arg.hostname = (unsigned char *)netr_info->hostname;
214 	arg.negotiate_flags = NETR_NEGOTIATE_FLAGS;
215 
216 	smb_tracef("server=[%s] account_name=[%s] hostname=[%s]\n",
217 	    netr_info->server, account_name, netr_info->hostname);
218 
219 	if (netr_gen_session_key(netr_info) != SMBAUTH_SUCCESS)
220 		return (-1);
221 
222 	if (netr_gen_credentials(netr_info->session_key,
223 	    &netr_info->client_challenge,
224 	    0,
225 	    &netr_info->client_credential) != SMBAUTH_SUCCESS) {
226 		return (-1);
227 	}
228 
229 	if (netr_gen_credentials(netr_info->session_key,
230 	    &netr_info->server_challenge,
231 	    0,
232 	    &netr_info->server_credential) != SMBAUTH_SUCCESS) {
233 		return (-1);
234 	}
235 
236 	(void) memcpy(&arg.client_credential, &netr_info->client_credential,
237 	    sizeof (struct netr_credential));
238 
239 	(void) mlsvc_rpc_init(&heap);
240 
241 	rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap);
242 	if (rc == 0) {
243 		if (arg.status != 0) {
244 			mlsvc_rpc_report_status(opnum, arg.status);
245 			rc = -1;
246 		} else {
247 			rc = memcmp(&netr_info->server_credential,
248 			    &arg.server_credential,
249 			    sizeof (struct netr_credential));
250 		}
251 	}
252 
253 	mlsvc_rpc_free(netr_handle->context, &heap);
254 	return (rc);
255 }
256 
257 /*
258  * netr_gen_session_key
259  *
260  * Generate a session key from the client and server challenges. The
261  * algorithm is a two stage hash. For the first hash, the input is
262  * the combination of the client and server challenges, the key is
263  * the first 8 bytes of the password. The initial password is formed
264  * using the NT password hash on the local hostname in lower case.
265  * The result is stored in a temporary buffer.
266  *
267  *		input:	challenge
268  *		key:	passwd lower 8 bytes
269  *		output:	intermediate result
270  *
271  * For the second hash, the input is the result of the first hash and
272  * the key is the last 8 bytes of the password.
273  *
274  *		input:	result of first hash
275  *		key:	passwd upper 8 bytes
276  *		output:	session_key
277  *
278  * The final output should be the session key.
279  *
280  *		FYI: smb_auth_DES(output, key, input)
281  *
282  * If any difficulties occur using the cryptographic framework, the
283  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
284  * returned.
285  */
286 int
287 netr_gen_session_key(netr_info_t *netr_info)
288 {
289 	unsigned char md4hash[32];
290 	unsigned char buffer[8];
291 	DWORD data[2];
292 	DWORD *client_challenge;
293 	DWORD *server_challenge;
294 	int rc;
295 	char *machine_passwd;
296 	DWORD new_data[2];
297 
298 	client_challenge = (DWORD *)(uintptr_t)&netr_info->client_challenge;
299 	server_challenge = (DWORD *)(uintptr_t)&netr_info->server_challenge;
300 	bzero(md4hash, 32);
301 
302 	/*
303 	 * We should check (netr_info->flags & NETR_FLG_INIT) and use
304 	 * the appropriate password but it isn't working yet.  So we
305 	 * always use the default one for now.
306 	 */
307 	smb_config_rdlock();
308 	machine_passwd = smb_config_getstr(SMB_CI_MACHINE_PASSWD);
309 
310 	if (!machine_passwd || *machine_passwd == 0) {
311 		smb_config_unlock();
312 		return (-1);
313 	}
314 
315 	bzero(netr_info->password, sizeof (netr_info->password));
316 	(void) strlcpy((char *)netr_info->password, (char *)machine_passwd,
317 	    sizeof (netr_info->password));
318 
319 	rc = smb_auth_ntlm_hash((char *)machine_passwd, md4hash);
320 	smb_config_unlock();
321 
322 	if (rc != SMBAUTH_SUCCESS)
323 		return (SMBAUTH_FAILURE);
324 
325 	data[0] = LE_IN32(&client_challenge[0]) + LE_IN32(&server_challenge[0]);
326 	data[1] = LE_IN32(&client_challenge[1]) + LE_IN32(&server_challenge[1]);
327 	LE_OUT32(&new_data[0], data[0]);
328 	LE_OUT32(&new_data[1], data[1]);
329 
330 	rc = smb_auth_DES(buffer, 8, md4hash, 8, (unsigned char *)new_data, 8);
331 	if (rc != SMBAUTH_SUCCESS)
332 		return (rc);
333 
334 	rc = smb_auth_DES(netr_info->session_key, 8, &md4hash[9], 8, buffer, 8);
335 	return (rc);
336 }
337 
338 /*
339  * netr_gen_credentials
340  *
341  * Generate a set of credentials from a challenge and a session key.
342  * The algorithm is a two stage hash. For the first hash, the
343  * timestamp is added to the challenge and the result is stored in a
344  * temporary buffer:
345  *
346  *		input:	challenge (including timestamp)
347  *		key:	session_key
348  *		output:	intermediate result
349  *
350  * For the second hash, the input is the result of the first hash and
351  * a strange partial key is used:
352  *
353  *		input:	result of first hash
354  *		key:	funny partial key
355  *		output:	credentiails
356  *
357  * The final output should be an encrypted set of credentials.
358  *
359  *		FYI: smb_auth_DES(output, key, input)
360  *
361  * If any difficulties occur using the cryptographic framework, the
362  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
363  * returned.
364  */
365 int
366 netr_gen_credentials(BYTE *session_key, netr_cred_t *challenge,
367     DWORD timestamp, netr_cred_t *out_cred)
368 {
369 	unsigned char buffer[8];
370 	unsigned char partial_key[8];
371 	DWORD data[2];
372 	DWORD *p;
373 	int rc;
374 
375 	p = (DWORD *)(uintptr_t)challenge;
376 	data[0] = p[0] + LE_IN32(&timestamp);
377 	data[1] = p[1];
378 
379 	if (smb_auth_DES(buffer, 8, session_key, 8,
380 	    (unsigned char *)data, 8) != SMBAUTH_SUCCESS)
381 		return (SMBAUTH_FAILURE);
382 
383 	bzero(partial_key, 8);
384 	partial_key[0] = session_key[7];
385 
386 	rc = smb_auth_DES((unsigned char *)out_cred, 8, partial_key, 8,
387 	    buffer, 8);
388 	return (rc);
389 }
390 
391 /*
392  * netr_server_password_set
393  *
394  * Attempt to change the trust account password for this system.
395  *
396  * Note that this call may legitimately fail if the registry on the
397  * domain controller has been setup to deny attempts to change the
398  * trust account password. In this case we should just continue to
399  * use the original password.
400  *
401  * Possible status values:
402  *	NT_STATUS_ACCESS_DENIED
403  */
404 int
405 netr_server_password_set(mlsvc_handle_t *netr_handle, netr_info_t *netr_info)
406 {
407 	struct netr_PasswordSet  arg;
408 	mlrpc_heapref_t heap;
409 	int opnum;
410 	int rc;
411 	BYTE new_password[NETR_OWF_PASSWORD_SZ];
412 	char account_name[MLSVC_DOMAIN_NAME_MAX * 2];
413 
414 	bzero(&arg, sizeof (struct netr_PasswordSet));
415 	opnum = NETR_OPNUM_ServerPasswordSet;
416 
417 	(void) snprintf(account_name, sizeof (account_name), "%s$",
418 	    netr_info->hostname);
419 
420 	arg.servername = (unsigned char *)netr_info->server;
421 	arg.account_name = (unsigned char *)account_name;
422 	arg.account_type = NETR_WKSTA_TRUST_ACCOUNT_TYPE;
423 	arg.hostname = (unsigned char *)netr_info->hostname;
424 
425 	/*
426 	 * Set up the client side authenticator.
427 	 */
428 	if (netr_setup_authenticator(netr_info, &arg.auth, 0) !=
429 	    SMBAUTH_SUCCESS) {
430 		return (-1);
431 	}
432 
433 	/*
434 	 * Generate a new password from the old password.
435 	 */
436 	if (netr_gen_password(netr_info->session_key,
437 	    netr_info->password, new_password) == SMBAUTH_FAILURE) {
438 		return (-1);
439 	}
440 
441 	(void) memcpy(&arg.uas_new_password, &new_password,
442 	    NETR_OWF_PASSWORD_SZ);
443 
444 	(void) mlsvc_rpc_init(&heap);
445 	rc = mlsvc_rpc_call(netr_handle->context, opnum, &arg, &heap);
446 	if ((rc != 0) || (arg.status != 0)) {
447 		mlsvc_rpc_report_status(opnum, arg.status);
448 		mlsvc_rpc_free(netr_handle->context, &heap);
449 		return (-1);
450 	}
451 
452 	/*
453 	 * Check the returned credentials.  The server returns the new
454 	 * client credential rather than the new server credentiali,
455 	 * as documented elsewhere.
456 	 *
457 	 * Generate the new seed for the credential chain.  Increment
458 	 * the timestamp and add it to the client challenge.  Then we
459 	 * need to copy the challenge to the credential field in
460 	 * preparation for the next cycle.
461 	 */
462 	if (netr_validate_chain(netr_info, &arg.auth) == 0) {
463 		/*
464 		 * Save the new password.
465 		 */
466 		(void) memcpy(netr_info->password, new_password,
467 		    NETR_OWF_PASSWORD_SZ);
468 	}
469 
470 	mlsvc_rpc_free(netr_handle->context, &heap);
471 	return (0);
472 }
473 
474 /*
475  * netr_gen_password
476  *
477  * Generate a new pasword from the old password  and the session key.
478  * The algorithm is a two stage hash. The session key is used in the
479  * first hash but only part of the session key is used in the second
480  * hash.
481  *
482  * If any difficulties occur using the cryptographic framework, the
483  * function returns SMBAUTH_FAILURE.  Otherwise SMBAUTH_SUCCESS is
484  * returned.
485  */
486 static int
487 netr_gen_password(BYTE *session_key, BYTE *old_password, BYTE *new_password)
488 {
489 	unsigned char partial_key[8];
490 	int rv;
491 
492 	rv = smb_auth_DES(new_password, 8, session_key, 8, old_password, 8);
493 	if (rv != SMBAUTH_SUCCESS)
494 		return (rv);
495 
496 	bzero(partial_key, 8);
497 	partial_key[0] = session_key[7];
498 
499 	rv = smb_auth_DES(&new_password[8], 8, partial_key, 8,
500 	    &old_password[8], 8);
501 	return (rv);
502 }
503