Restructure inner workings of libnetapi a bit.
[samba.git] / source / lib / netapi / examples / getjoinableous / 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 <inttypes.h>
23
24 #include <netapi.h>
25
26 char *get_string_param(const char *param)
27 {
28         char *p;
29
30         p = strchr(param, '=');
31         if (!p) {
32                 return NULL;
33         }
34
35         return (p+1);
36 }
37
38 int main(int argc, char **argv)
39 {
40         NET_API_STATUS status;
41         const char *server_name = NULL;
42         const char *domain_name = NULL;
43         const char *account = NULL;
44         const char *password = NULL;
45         const char **ous = NULL;
46         uint32_t num_ous = 0;
47         struct libnetapi_ctx *ctx = NULL;
48         int i;
49
50         status = libnetapi_init(&ctx);
51         if (status != 0) {
52                 return status;
53         }
54
55         if (argc < 2) {
56                 printf("usage: getjoinableous\n");
57                 printf("\t<hostname> [domain=DOMAIN] <user=USER> <password=PASSWORD>\n");
58                 return 0;
59         }
60
61         if (argc > 2) {
62                 server_name = argv[1];
63         }
64
65         for (i=0; i<argc; i++) {
66                 if (strncasecmp(argv[i], "domain", strlen("domain"))== 0) {
67                         domain_name = get_string_param(argv[i]);
68                 }
69                 if (strncasecmp(argv[i], "user", strlen("user"))== 0) {
70                         account = get_string_param(argv[i]);
71                         libnetapi_set_username(ctx, account);
72                 }
73                 if (strncasecmp(argv[i], "password", strlen("password"))== 0) {
74                         password = get_string_param(argv[i]);
75                         libnetapi_set_password(ctx, password);
76                 }
77                 if (strncasecmp(argv[i], "debug", strlen("debug"))== 0) {
78                         const char *str = NULL;
79                         str = get_string_param(argv[i]);
80                         libnetapi_set_debuglevel(ctx, str);
81                 }
82         }
83
84         status = NetGetJoinableOUs(server_name,
85                                    domain_name,
86                                    account,
87                                    password,
88                                    &num_ous,
89                                    &ous);
90         if (status != 0) {
91                 printf("failed with: %s\n",
92                         libnetapi_get_error_string(ctx, status));
93         } else {
94                 printf("Successfully queried joinable ous:\n");
95                 for (i=0; i<num_ous; i++) {
96                         printf("ou: %s\n", ous[i]);
97                 }
98         }
99
100         NetApiBufferFree(ous);
101
102         libnetapi_free(ctx);
103
104         return status;
105 }