fixed the event context for net vampire
[metze/samba/wip.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 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 #include "param/param.h"
27 #include "lib/events/events.h"
28
29 int net_join(struct net_context *ctx, int argc, const char **argv) 
30 {
31         NTSTATUS status;
32         struct libnet_context *libnetctx;
33         struct libnet_Join *r;
34         char *tmp;
35         const char *domain_name;
36         enum netr_SchannelType secure_channel_type = SEC_CHAN_WKSTA;
37
38         switch (argc) {
39                 case 0: /* no args -> fail */
40                         return net_join_usage(ctx, argc, argv);
41                 case 1: /* only DOMAIN */
42                         tmp = talloc_strdup(ctx, argv[0]);
43                         break;
44                 case 2: /* DOMAIN and role */
45                         tmp = talloc_strdup(ctx, argv[0]);
46                         if (strcasecmp(argv[1], "BDC") == 0) {
47                                 secure_channel_type = SEC_CHAN_BDC;
48                         } else if (strcasecmp(argv[1], "MEMBER") == 0) {
49                                 secure_channel_type = SEC_CHAN_WKSTA;
50                         } else {
51                                 d_fprintf(stderr, "net_join: Invalid 2nd argument (%s) must be MEMBER or BDC\n", argv[1]);
52                                 return net_join_usage(ctx, argc, argv);
53                         }
54                         break;
55                 default: /* too many args -> fail */
56                         return net_join_usage(ctx, argc, argv);
57         }
58
59         domain_name = tmp;
60
61         libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
62         if (!libnetctx) {
63                 return -1;      
64         }
65         libnetctx->cred = ctx->credentials;
66         r = talloc(ctx, struct libnet_Join);
67         if (!r) {
68                 return -1;
69         }
70         /* prepare parameters for the join */
71         r->in.netbios_name              = lp_netbios_name(ctx->lp_ctx);
72         r->in.domain_name               = domain_name;
73         r->in.join_type                 = secure_channel_type;
74         r->in.level                     = LIBNET_JOIN_AUTOMATIC;
75         r->out.error_string             = NULL;
76
77         /* do the domain join */
78         status = libnet_Join(libnetctx, r, r);
79         
80         if (!NT_STATUS_IS_OK(status)) {
81                 d_fprintf(stderr, "Joining domain failed: %s\n",
82                           r->out.error_string ? r->out.error_string : nt_errstr(status));
83                 talloc_free(r);
84                 talloc_free(libnetctx);
85                 return -1;
86         }
87         d_printf("Joined domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx, r->out.domain_sid));
88
89         talloc_free(libnetctx);
90         return 0;
91 }
92
93 int net_join_usage(struct net_context *ctx, int argc, const char **argv)
94 {
95         d_printf("net join <domain> [BDC | MEMBER] [options]\n");
96         return 0;       
97 }
98
99 int net_join_help(struct net_context *ctx, int argc, const char **argv)
100 {
101         d_printf("Joins domain as either member or backup domain controller.\n");
102         return 0;       
103 }
104
105 int net_vampire(struct net_context *ctx, int argc, const char **argv) 
106 {
107         NTSTATUS status;
108         struct libnet_context *libnetctx;
109         struct libnet_Vampire *r;
110         char *tmp, *targetdir = NULL;
111         const char *domain_name;
112
113         switch (argc) {
114                 case 0: /* no args -> fail */
115                         return net_vampire_usage(ctx, argc, argv);
116                 case 1: /* only DOMAIN */
117                         tmp = talloc_strdup(ctx, argv[0]);
118                         break;
119                 case 2: /* domain and target dir */
120                         tmp = talloc_strdup(ctx, argv[0]);
121                         targetdir = talloc_strdup(ctx, argv[1]);
122                         break;
123                 default: /* too many args -> fail */
124                         return net_vampire_usage(ctx, argc, argv);
125         }
126
127         domain_name = tmp;
128
129         libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
130         if (!libnetctx) {
131                 return -1;      
132         }
133         libnetctx->cred = ctx->credentials;
134         r = talloc(ctx, struct libnet_Vampire);
135         if (!r) {
136                 return -1;
137         }
138         /* prepare parameters for the vampire */
139         r->in.netbios_name  = lp_netbios_name(ctx->lp_ctx);
140         r->in.domain_name   = domain_name;
141         r->in.targetdir     = targetdir;
142         r->out.error_string = NULL;
143
144         /* do the domain vampire */
145         status = libnet_Vampire(libnetctx, r, r);
146         
147         if (!NT_STATUS_IS_OK(status)) {
148                 d_fprintf(stderr, "Vampire of domain failed: %s\n",
149                           r->out.error_string ? r->out.error_string : nt_errstr(status));
150                 talloc_free(r);
151                 talloc_free(libnetctx);
152                 return -1;
153         }
154         d_printf("Vampired domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx, r->out.domain_sid));
155
156         talloc_free(libnetctx);
157         return 0;
158 }
159
160 int net_vampire_usage(struct net_context *ctx, int argc, const char **argv)
161 {
162         d_printf("net vampire <domain> [options]\n");
163         return 0;       
164 }
165
166 int net_vampire_help(struct net_context *ctx, int argc, const char **argv)
167 {
168         d_printf("Vampires domain as either member or backup domain controller.\n");
169         return 0;       
170 }