1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4 
5    Copyright (C) 2004 Stefan Metzmacher <metze@samba.org>
6    Copyright (C) 2005 Andrew Bartlett <abartlet@samba.org>
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include "includes.h"
24 #include "utils/net/net.h"
25 #include "libnet/libnet.h"
26 #include "libcli/security/security.h"
27 
net_join(struct net_context * ctx,int argc,const char ** argv)28 int net_join(struct net_context *ctx, int argc, const char **argv)
29 {
30 	NTSTATUS status;
31 	struct libnet_context *libnetctx;
32 	struct libnet_Join *r;
33 	char *tmp;
34 	const char *domain_name;
35 	enum netr_SchannelType secure_channel_type = SEC_CHAN_WKSTA;
36 
37 	switch (argc) {
38 		case 0: /* no args -> fail */
39 			return net_join_usage(ctx, argc, argv);
40 		case 1: /* only DOMAIN */
41 			tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
42 			break;
43 		case 2: /* DOMAIN and role */
44 			tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
45 			if (strcasecmp(argv[1], "BDC") == 0) {
46 				secure_channel_type = SEC_CHAN_BDC;
47 			} else if (strcasecmp(argv[1], "MEMBER") == 0) {
48 				secure_channel_type = SEC_CHAN_WKSTA;
49 			} else {
50 				d_fprintf(stderr, "net_join: Invalid 2nd argument (%s) must be MEMBER or BDC\n", argv[1]);
51 				return net_join_usage(ctx, argc, argv);
52 			}
53 			break;
54 		default: /* too many args -> fail */
55 			return net_join_usage(ctx, argc, argv);
56 	}
57 
58 	domain_name = tmp;
59 
60 	libnetctx = libnet_context_init(NULL);
61 	if (!libnetctx) {
62 		return -1;
63 	}
64 	libnetctx->cred = ctx->credentials;
65 	r = talloc(ctx->mem_ctx, struct libnet_Join);
66 	if (!r) {
67 		return -1;
68 	}
69 	/* prepare parameters for the join */
70 	r->in.netbios_name		= lp_netbios_name();
71 	r->in.domain_name		= domain_name;
72 	r->in.join_type           	= secure_channel_type;
73 	r->in.level			= LIBNET_JOIN_AUTOMATIC;
74 	r->out.error_string		= NULL;
75 
76 	/* do the domain join */
77 	status = libnet_Join(libnetctx, r, r);
78 
79 	if (!NT_STATUS_IS_OK(status)) {
80 		d_fprintf(stderr, "Joining domain failed: %s\n",
81 			  r->out.error_string ? r->out.error_string : nt_errstr(status));
82 		talloc_free(r);
83 		talloc_free(libnetctx);
84 		return -1;
85 	}
86 	d_printf("Joined domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx->mem_ctx, r->out.domain_sid));
87 
88 	talloc_free(libnetctx);
89 	return 0;
90 }
91 
net_join_usage(struct net_context * ctx,int argc,const char ** argv)92 int net_join_usage(struct net_context *ctx, int argc, const char **argv)
93 {
94 	d_printf("net join <domain> [BDC | MEMBER] [options]\n");
95 	return 0;
96 }
97 
net_join_help(struct net_context * ctx,int argc,const char ** argv)98 int net_join_help(struct net_context *ctx, int argc, const char **argv)
99 {
100 	d_printf("Joins domain as either member or backup domain controller.\n");
101 	return 0;
102 }
103