xref: /onnv-gate/usr/src/lib/smbsrv/libmlsvc/common/mlsvc_util.c (revision 12914:e95332bf1454)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
22*12914SJoyce.McIntosh@Sun.COM  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
235331Samw  */
245331Samw 
255331Samw /*
265331Samw  * Utility functions to support the RPC interface library.
275331Samw  */
285331Samw 
295331Samw #include <stdio.h>
305331Samw #include <stdarg.h>
315331Samw #include <strings.h>
325331Samw #include <unistd.h>
335331Samw #include <netdb.h>
345331Samw #include <stdlib.h>
355331Samw #include <sys/time.h>
365331Samw #include <sys/systm.h>
379021Samw@Sun.COM #include <syslog.h>
385331Samw 
395331Samw #include <smbsrv/libsmb.h>
405331Samw #include <smbsrv/libsmbns.h>
415331Samw #include <smbsrv/libmlsvc.h>
4210717Samw@Sun.COM #include <smbsrv/libsmbrdr.h>
435331Samw #include <smbsrv/smbinfo.h>
448334SJose.Borrego@Sun.COM #include <lsalib.h>
458334SJose.Borrego@Sun.COM #include <samlib.h>
468334SJose.Borrego@Sun.COM #include <smbsrv/netrauth.h>
475331Samw 
485772Sas200622 /* Domain join support (using MS-RPC) */
495772Sas200622 static boolean_t mlsvc_ntjoin_support = B_FALSE;
505772Sas200622 
515331Samw extern int netr_open(char *, char *, mlsvc_handle_t *);
525331Samw extern int netr_close(mlsvc_handle_t *);
535331Samw extern DWORD netlogon_auth(char *, mlsvc_handle_t *, DWORD);
545331Samw 
556139Sjb150015 DWORD
mlsvc_netlogon(char * server,char * domain)566139Sjb150015 mlsvc_netlogon(char *server, char *domain)
576139Sjb150015 {
586139Sjb150015 	mlsvc_handle_t netr_handle;
596139Sjb150015 	DWORD status;
606139Sjb150015 
616139Sjb150015 	if (netr_open(server, domain, &netr_handle) == 0) {
629021Samw@Sun.COM 		if ((status = netlogon_auth(server, &netr_handle,
639021Samw@Sun.COM 		    NETR_FLG_INIT)) != NT_STATUS_SUCCESS)
649021Samw@Sun.COM 			syslog(LOG_NOTICE, "Failed to establish NETLOGON "
659021Samw@Sun.COM 			    "credential chain");
666139Sjb150015 		(void) netr_close(&netr_handle);
676139Sjb150015 	} else {
686139Sjb150015 		status = NT_STATUS_OPEN_FAILED;
696139Sjb150015 	}
706139Sjb150015 
716139Sjb150015 	return (status);
726139Sjb150015 }
736139Sjb150015 
745331Samw /*
759343SAfshin.Ardakani@Sun.COM  * Joins the specified domain by creating a machine account on
769343SAfshin.Ardakani@Sun.COM  * the selected domain controller.
779343SAfshin.Ardakani@Sun.COM  *
789343SAfshin.Ardakani@Sun.COM  * Disconnect any existing connection with the domain controller.
799343SAfshin.Ardakani@Sun.COM  * This will ensure that no stale connection will be used, it will
809343SAfshin.Ardakani@Sun.COM  * also pickup any configuration changes in either side by trying
819343SAfshin.Ardakani@Sun.COM  * to establish a new connection.
825331Samw  *
835331Samw  * Returns NT status codes.
845331Samw  */
855331Samw DWORD
mlsvc_join(smb_domainex_t * dxi,char * user,char * plain_text)8610717Samw@Sun.COM mlsvc_join(smb_domainex_t *dxi, char *user, char *plain_text)
875331Samw {
885331Samw 	int erc;
895331Samw 	DWORD status;
908334SJose.Borrego@Sun.COM 	char machine_passwd[NETR_MACHINE_ACCT_PASSWD_MAX];
919021Samw@Sun.COM 	smb_adjoin_status_t err;
9210717Samw@Sun.COM 	smb_domain_t *domain;
935331Samw 
945331Samw 	machine_passwd[0] = '\0';
955331Samw 
9610717Samw@Sun.COM 	domain = &dxi->d_primary;
975331Samw 
9810717Samw@Sun.COM 	mlsvc_disconnect(dxi->d_dc);
999343SAfshin.Ardakani@Sun.COM 
10010717Samw@Sun.COM 	erc = smbrdr_logon(dxi->d_dc, domain->di_nbname, user);
1015331Samw 
1025331Samw 	if (erc == AUTH_USER_GRANT) {
1035772Sas200622 		if (mlsvc_ntjoin_support == B_FALSE) {
1045331Samw 
1059832Samw@Sun.COM 			if ((err = smb_ads_join(domain->di_fqname, user,
1069021Samw@Sun.COM 			    plain_text, machine_passwd,
1079021Samw@Sun.COM 			    sizeof (machine_passwd))) == SMB_ADJOIN_SUCCESS) {
1085331Samw 				status = NT_STATUS_SUCCESS;
1099021Samw@Sun.COM 			} else {
1109021Samw@Sun.COM 				smb_ads_join_errmsg(err);
1115331Samw 				status = NT_STATUS_UNSUCCESSFUL;
1129021Samw@Sun.COM 			}
1135331Samw 		} else {
1145331Samw 
11510717Samw@Sun.COM 			status = sam_create_trust_account(dxi->d_dc,
11610504SKeyur.Desai@Sun.COM 			    domain->di_nbname);
1175331Samw 			if (status == NT_STATUS_SUCCESS) {
1187961SNatalie.Li@Sun.COM 				(void) smb_getnetbiosname(machine_passwd,
1197961SNatalie.Li@Sun.COM 				    sizeof (machine_passwd));
12010966SJordan.Brown@Sun.COM 				(void) smb_strlwr(machine_passwd);
1215331Samw 			}
1225331Samw 		}
1235331Samw 
1245331Samw 		if (status == NT_STATUS_SUCCESS) {
12510717Samw@Sun.COM 			erc = smb_setdomainprops(NULL, dxi->d_dc,
1265772Sas200622 			    machine_passwd);
1279021Samw@Sun.COM 			if (erc != 0) {
12811963SAfshin.Ardakani@Sun.COM 				syslog(LOG_NOTICE,
12911963SAfshin.Ardakani@Sun.COM 				    "Failed to update configuration");
13011571SShawn.Emery@Sun.COM 				bzero(machine_passwd, sizeof (machine_passwd));
1315331Samw 				return (NT_STATUS_UNSUCCESSFUL);
1329021Samw@Sun.COM 			}
1335331Samw 
13410717Samw@Sun.COM 			status = mlsvc_netlogon(dxi->d_dc, domain->di_nbname);
1355331Samw 		}
1365331Samw 	} else {
1375331Samw 		status = NT_STATUS_LOGON_FAILURE;
1385331Samw 	}
1395331Samw 
14011571SShawn.Emery@Sun.COM 	bzero(machine_passwd, sizeof (machine_passwd));
1415331Samw 	return (status);
1425331Samw }
14310717Samw@Sun.COM 
14410717Samw@Sun.COM void
mlsvc_disconnect(const char * server)14510717Samw@Sun.COM mlsvc_disconnect(const char *server)
14610717Samw@Sun.COM {
14710717Samw@Sun.COM 	smbrdr_disconnect(server);
14810717Samw@Sun.COM }
149