xref: /onnv-gate/usr/src/lib/smbsrv/libmlsvc/common/mlsvc_util.c (revision 11963:061945695ce1)
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 /*
2211571SShawn.Emery@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw /*
275331Samw  * Utility functions to support the RPC interface library.
285331Samw  */
295331Samw 
305331Samw #include <stdio.h>
315331Samw #include <stdarg.h>
325331Samw #include <strings.h>
335331Samw #include <unistd.h>
345331Samw #include <netdb.h>
355331Samw #include <stdlib.h>
365331Samw #include <sys/time.h>
375331Samw #include <sys/systm.h>
389021Samw@Sun.COM #include <syslog.h>
395331Samw 
405331Samw #include <smbsrv/libsmb.h>
415331Samw #include <smbsrv/libsmbns.h>
425331Samw #include <smbsrv/libmlsvc.h>
4310717Samw@Sun.COM #include <smbsrv/libsmbrdr.h>
445331Samw #include <smbsrv/smbinfo.h>
458334SJose.Borrego@Sun.COM #include <lsalib.h>
468334SJose.Borrego@Sun.COM #include <samlib.h>
478334SJose.Borrego@Sun.COM #include <smbsrv/netrauth.h>
485331Samw 
495772Sas200622 /* Domain join support (using MS-RPC) */
505772Sas200622 static boolean_t mlsvc_ntjoin_support = B_FALSE;
515772Sas200622 
525331Samw extern int netr_open(char *, char *, mlsvc_handle_t *);
535331Samw extern int netr_close(mlsvc_handle_t *);
545331Samw extern DWORD netlogon_auth(char *, mlsvc_handle_t *, DWORD);
555331Samw 
566139Sjb150015 DWORD
576139Sjb150015 mlsvc_netlogon(char *server, char *domain)
586139Sjb150015 {
596139Sjb150015 	mlsvc_handle_t netr_handle;
606139Sjb150015 	DWORD status;
616139Sjb150015 
626139Sjb150015 	if (netr_open(server, domain, &netr_handle) == 0) {
639021Samw@Sun.COM 		if ((status = netlogon_auth(server, &netr_handle,
649021Samw@Sun.COM 		    NETR_FLG_INIT)) != NT_STATUS_SUCCESS)
659021Samw@Sun.COM 			syslog(LOG_NOTICE, "Failed to establish NETLOGON "
669021Samw@Sun.COM 			    "credential chain");
676139Sjb150015 		(void) netr_close(&netr_handle);
686139Sjb150015 	} else {
696139Sjb150015 		status = NT_STATUS_OPEN_FAILED;
706139Sjb150015 	}
716139Sjb150015 
726139Sjb150015 	return (status);
736139Sjb150015 }
746139Sjb150015 
755331Samw /*
769343SAfshin.Ardakani@Sun.COM  * Joins the specified domain by creating a machine account on
779343SAfshin.Ardakani@Sun.COM  * the selected domain controller.
789343SAfshin.Ardakani@Sun.COM  *
799343SAfshin.Ardakani@Sun.COM  * Disconnect any existing connection with the domain controller.
809343SAfshin.Ardakani@Sun.COM  * This will ensure that no stale connection will be used, it will
819343SAfshin.Ardakani@Sun.COM  * also pickup any configuration changes in either side by trying
829343SAfshin.Ardakani@Sun.COM  * to establish a new connection.
835331Samw  *
845331Samw  * Returns NT status codes.
855331Samw  */
865331Samw DWORD
8710717Samw@Sun.COM mlsvc_join(smb_domainex_t *dxi, char *user, char *plain_text)
885331Samw {
895331Samw 	int erc;
905331Samw 	DWORD status;
918334SJose.Borrego@Sun.COM 	char machine_passwd[NETR_MACHINE_ACCT_PASSWD_MAX];
929021Samw@Sun.COM 	smb_adjoin_status_t err;
9310717Samw@Sun.COM 	smb_domain_t *domain;
945331Samw 
955331Samw 	machine_passwd[0] = '\0';
965331Samw 
9710717Samw@Sun.COM 	domain = &dxi->d_primary;
985331Samw 
9910717Samw@Sun.COM 	mlsvc_disconnect(dxi->d_dc);
1009343SAfshin.Ardakani@Sun.COM 
10110717Samw@Sun.COM 	erc = smbrdr_logon(dxi->d_dc, domain->di_nbname, user);
1025331Samw 
1035331Samw 	if (erc == AUTH_USER_GRANT) {
1045772Sas200622 		if (mlsvc_ntjoin_support == B_FALSE) {
1055331Samw 
1069832Samw@Sun.COM 			if ((err = smb_ads_join(domain->di_fqname, user,
1079021Samw@Sun.COM 			    plain_text, machine_passwd,
1089021Samw@Sun.COM 			    sizeof (machine_passwd))) == SMB_ADJOIN_SUCCESS) {
1095331Samw 				status = NT_STATUS_SUCCESS;
1109021Samw@Sun.COM 			} else {
1119021Samw@Sun.COM 				smb_ads_join_errmsg(err);
1125331Samw 				status = NT_STATUS_UNSUCCESSFUL;
1139021Samw@Sun.COM 			}
1145331Samw 		} else {
1155331Samw 
11610717Samw@Sun.COM 			status = sam_create_trust_account(dxi->d_dc,
11710504SKeyur.Desai@Sun.COM 			    domain->di_nbname);
1185331Samw 			if (status == NT_STATUS_SUCCESS) {
1197961SNatalie.Li@Sun.COM 				(void) smb_getnetbiosname(machine_passwd,
1207961SNatalie.Li@Sun.COM 				    sizeof (machine_passwd));
12110966SJordan.Brown@Sun.COM 				(void) smb_strlwr(machine_passwd);
1225331Samw 			}
1235331Samw 		}
1245331Samw 
1255331Samw 		if (status == NT_STATUS_SUCCESS) {
12610717Samw@Sun.COM 			erc = smb_setdomainprops(NULL, dxi->d_dc,
1275772Sas200622 			    machine_passwd);
1289021Samw@Sun.COM 			if (erc != 0) {
129*11963SAfshin.Ardakani@Sun.COM 				syslog(LOG_NOTICE,
130*11963SAfshin.Ardakani@Sun.COM 				    "Failed to update configuration");
13111571SShawn.Emery@Sun.COM 				bzero(machine_passwd, sizeof (machine_passwd));
1325331Samw 				return (NT_STATUS_UNSUCCESSFUL);
1339021Samw@Sun.COM 			}
1345331Samw 
13510717Samw@Sun.COM 			status = mlsvc_netlogon(dxi->d_dc, domain->di_nbname);
1365331Samw 		}
1375331Samw 	} else {
1385331Samw 		status = NT_STATUS_LOGON_FAILURE;
1395331Samw 	}
1405331Samw 
14111571SShawn.Emery@Sun.COM 	bzero(machine_passwd, sizeof (machine_passwd));
1425331Samw 	return (status);
1435331Samw }
14410717Samw@Sun.COM 
14510717Samw@Sun.COM int
14610717Samw@Sun.COM mlsvc_ping(const char *server)
14710717Samw@Sun.COM {
14810717Samw@Sun.COM 	return (smbrdr_echo(server));
14910717Samw@Sun.COM }
15010717Samw@Sun.COM 
15110717Samw@Sun.COM void
15210717Samw@Sun.COM mlsvc_disconnect(const char *server)
15310717Samw@Sun.COM {
15410717Samw@Sun.COM 	smbrdr_disconnect(server);
15510717Samw@Sun.COM }
156