r23792: convert Samba4 to GPLv3
[jelmer/samba4-debian.git] / source / utils / net / net_join.c
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "libcli/security/security.h"
26
27 int net_join(struct net_context *ctx, int argc, const char **argv) 
28 {
29         NTSTATUS status;
30         struct libnet_context *libnetctx;
31         struct libnet_Join *r;
32         char *tmp;
33         const char *domain_name;
34         enum netr_SchannelType secure_channel_type = SEC_CHAN_WKSTA;
35
36         switch (argc) {
37                 case 0: /* no args -> fail */
38                         return net_join_usage(ctx, argc, argv);
39                 case 1: /* only DOMAIN */
40                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
41                         break;
42                 case 2: /* DOMAIN and role */
43                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
44                         if (strcasecmp(argv[1], "BDC") == 0) {
45                                 secure_channel_type = SEC_CHAN_BDC;
46                         } else if (strcasecmp(argv[1], "MEMBER") == 0) {
47                                 secure_channel_type = SEC_CHAN_WKSTA;
48                         } else {
49                                 d_fprintf(stderr, "net_join: Invalid 2nd argument (%s) must be MEMBER or BDC\n", argv[1]);
50                                 return net_join_usage(ctx, argc, argv);
51                         }
52                         break;
53                 default: /* too many args -> fail */
54                         return net_join_usage(ctx, argc, argv);
55         }
56
57         domain_name = tmp;
58
59         libnetctx = libnet_context_init(NULL);
60         if (!libnetctx) {
61                 return -1;      
62         }
63         libnetctx->cred = ctx->credentials;
64         r = talloc(ctx->mem_ctx, struct libnet_Join);
65         if (!r) {
66                 return -1;
67         }
68         /* prepare parameters for the join */
69         r->in.netbios_name              = lp_netbios_name();
70         r->in.domain_name               = domain_name;
71         r->in.join_type                 = secure_channel_type;
72         r->in.level                     = LIBNET_JOIN_AUTOMATIC;
73         r->out.error_string             = NULL;
74
75         /* do the domain join */
76         status = libnet_Join(libnetctx, r, r);
77         
78         if (!NT_STATUS_IS_OK(status)) {
79                 d_fprintf(stderr, "Joining domain failed: %s\n",
80                           r->out.error_string ? r->out.error_string : nt_errstr(status));
81                 talloc_free(r);
82                 talloc_free(libnetctx);
83                 return -1;
84         }
85         d_printf("Joined domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx->mem_ctx, r->out.domain_sid));
86
87         talloc_free(libnetctx);
88         return 0;
89 }
90
91 int net_join_usage(struct net_context *ctx, int argc, const char **argv)
92 {
93         d_printf("net join <domain> [BDC | MEMBER] [options]\n");
94         return 0;       
95 }
96
97 int net_join_help(struct net_context *ctx, int argc, const char **argv)
98 {
99         d_printf("Joins domain as either member or backup domain controller.\n");
100         return 0;       
101 }