selftest/output/plain.pm: report the testuite name on error
[Samba.git] / source4 / utils / net / net_join.c
blob316bc129f1854d4c1cd85bab6fb0ae9d4dc2f9cc
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) 2004 Stefan Metzmacher <metze@samba.org>
6 Copyright (C) 2005 Andrew Bartlett <abartlet@samba.org>
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.
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.
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/>.
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"
29 int net_join(struct net_context *ctx, int argc, const char **argv)
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;
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);
54 break;
55 default: /* too many args -> fail */
56 return net_join_usage(ctx, argc, argv);
59 domain_name = tmp;
61 libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
62 if (!libnetctx) {
63 return -1;
65 libnetctx->cred = ctx->credentials;
66 r = talloc(ctx, struct libnet_Join);
67 if (!r) {
68 return -1;
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;
77 /* do the domain join */
78 status = libnet_Join(libnetctx, r, r);
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;
87 d_printf("Joined domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx, r->out.domain_sid));
89 talloc_free(libnetctx);
90 return 0;
93 int net_join_usage(struct net_context *ctx, int argc, const char **argv)
95 d_printf("net join <domain> [BDC | MEMBER] [options]\n");
96 return 0;
99 int net_join_help(struct net_context *ctx, int argc, const char **argv)
101 d_printf("Joins domain as either member or backup domain controller.\n");
102 return 0;