732f73dd572556b513d94a7125746d0c6b4714cd
[metze/samba-autobuild/.git] / source3 / lib / netapi / examples / join / getjoinableous.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Join Support (cmdline + netapi)
4  *  Copyright (C) Guenther Deschner 2008
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <inttypes.h>
24
25 #include <netapi.h>
26
27 #include "common.h"
28
29 int main(int argc, const char **argv)
30 {
31         NET_API_STATUS status;
32         const char *host_name = NULL;
33         const char *domain_name = NULL;
34         const char **ous = NULL;
35         uint32_t num_ous = 0;
36         struct libnetapi_ctx *ctx = NULL;
37         int i;
38
39         poptContext pc;
40         int opt;
41
42         struct poptOption long_options[] = {
43                 POPT_AUTOHELP
44                 { "domain", 0, POPT_ARG_STRING, NULL, 'D', "Domain name", "DOMAIN" },
45                 POPT_COMMON_LIBNETAPI_EXAMPLES
46                 POPT_TABLEEND
47         };
48
49         status = libnetapi_init(&ctx);
50         if (status != 0) {
51                 return status;
52         }
53
54         pc = poptGetContext("getjoinableous", argc, argv, long_options, 0);
55
56         poptSetOtherOptionHelp(pc, "hostname domainname");
57         while((opt = poptGetNextOpt(pc)) != -1) {
58                 switch (opt) {
59                         case 'D':
60                                 domain_name = poptGetOptArg(pc);
61                                 break;
62                 }
63         }
64
65         if (!poptPeekArg(pc)) {
66                 poptPrintHelp(pc, stderr, 0);
67                 goto out;
68         }
69         host_name = poptGetArg(pc);
70
71         /* NetGetJoinableOUs */
72
73         status = NetGetJoinableOUs(host_name,
74                                    domain_name,
75                                    ctx->username,
76                                    ctx->password,
77                                    &num_ous,
78                                    &ous);
79         if (status != 0) {
80                 printf("failed with: %s\n",
81                         libnetapi_get_error_string(ctx, status));
82         } else {
83                 printf("Successfully queried joinable ous:\n");
84                 for (i=0; i<num_ous; i++) {
85                         printf("ou: %s\n", ous[i]);
86                 }
87         }
88
89  out:
90         NetApiBufferFree(ous);
91         libnetapi_free(ctx);
92         poptFreeContext(pc);
93
94         return status;
95 }