r5400: Slightly better handling of help messages in net tool.
[bbaumbach/samba-autobuild/.git] / source4 / 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 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 "librpc/gen_ndr/ndr_samr.h"
27
28 int net_join(struct net_context *ctx, int argc, const char **argv) 
29 {
30         NTSTATUS status;
31         struct libnet_context *libnetctx;
32         union 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                                 DEBUG(0, ("net_join: 2nd argument must be MEMBER or BDC\n"));
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();
61         if (!libnetctx) {
62                 return -1;      
63         }
64         libnetctx->user.account_name    = ctx->user.account_name;
65         libnetctx->user.domain_name     = ctx->user.domain_name;
66         libnetctx->user.password        = ctx->user.password;
67
68         /* prepare password change */
69         r.generic.level                  = LIBNET_JOIN_GENERIC;
70         r.generic.in.domain_name         = domain_name;
71         r.generic.in.secure_channel_type = secure_channel_type;
72         r.generic.out.error_string       = NULL;
73
74         /* do the domain join */
75         status = libnet_Join(libnetctx, ctx->mem_ctx, &r);
76         if (!NT_STATUS_IS_OK(status)) {
77                 DEBUG(0,("libnet_Join returned %s: %s\n",
78                          nt_errstr(status),
79                          r.generic.out.error_string));
80                 return -1;
81         }
82
83         libnet_context_destroy(&libnetctx);
84
85         return 0;
86 }
87
88 int net_join_usage(struct net_context *ctx, int argc, const char **argv)
89 {
90         d_printf("net join <domain> [BDC | MEMBER] [options]\n");
91         return 0;       
92 }
93
94 int net_join_help(struct net_context *ctx, int argc, const char **argv)
95 {
96         d_printf("Joins domain as either member or backup domain controller.\n");
97         return 0;       
98 }