netapi: use NETSETUP join flags in examples.
[Samba.git] / source / lib / netapi / examples / netdomjoin / netdomjoin.c
blob08ce71b938ff50fb7c6b54a543154795d282dcc1
1 /*
2 * Unix SMB/CIFS implementation.
3 * Join Support (cmdline + netapi)
4 * Copyright (C) Guenther Deschner 2007-2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <netapi.h>
28 #include "common.h"
30 enum {
31 OPT_OU = 1000
34 int main(int argc, const char **argv)
36 NET_API_STATUS status;
37 const char *host_name = NULL;
38 const char *domain_name = NULL;
39 const char *account_ou = NULL;
40 const char *account = NULL;
41 const char *password = NULL;
42 uint32_t join_flags = NETSETUP_JOIN_DOMAIN |
43 NETSETUP_ACCT_CREATE |
44 NETSETUP_DOMAIN_JOIN_IF_JOINED;
45 struct libnetapi_ctx *ctx = NULL;
47 poptContext pc;
48 int opt;
50 struct poptOption long_options[] = {
51 POPT_AUTOHELP
52 { "ou", 0, POPT_ARG_STRING, &account_ou, 'U', "Account ou", "ACCOUNT_OU" },
53 { "domain", 0, POPT_ARG_STRING, &domain_name, 'U', "Domain name (required)", "DOMAIN" },
54 { "userd", 0, POPT_ARG_STRING, &account, 'U', "Domain admin account", "USERNAME" },
55 { "passwordd", 0, POPT_ARG_STRING, &password, 'U', "Domain admin password", "PASSWORD" },
56 POPT_COMMON_LIBNETAPI_EXAMPLES
57 POPT_TABLEEND
61 status = libnetapi_init(&ctx);
62 if (status != 0) {
63 return status;
66 pc = poptGetContext("netdomjoin", argc, argv, long_options, 0);
68 poptSetOtherOptionHelp(pc, "hostname");
69 while((opt = poptGetNextOpt(pc)) != -1) {
72 if (!poptPeekArg(pc)) {
73 poptPrintHelp(pc, stderr, 0);
74 goto out;
76 host_name = poptGetArg(pc);
78 if (!domain_name) {
79 poptPrintHelp(pc, stderr, 0);
80 goto out;
83 /* NetJoinDomain */
85 status = NetJoinDomain(host_name,
86 domain_name,
87 account_ou,
88 account,
89 password,
90 join_flags);
91 if (status != 0) {
92 const char *errstr = NULL;
93 errstr = libnetapi_get_error_string(ctx, status);
94 printf("Join failed with: %s\n", errstr);
95 } else {
96 printf("Successfully joined\n");
99 out:
100 libnetapi_free(ctx);
101 poptFreeContext(pc);
103 return status;