Use popt in libetapi example code.
authorGünther Deschner <gd@samba.org>
Tue, 8 Apr 2008 12:34:30 +0000 (14:34 +0200)
committerGünther Deschner <gd@samba.org>
Tue, 8 Apr 2008 12:34:30 +0000 (14:34 +0200)
Guenther
(This used to be commit 6f239df3f5a57c9549f1637e53fd42d2ed604c3f)

source3/lib/netapi/examples/Makefile.in
source3/lib/netapi/examples/common.c [new file with mode: 0644]
source3/lib/netapi/examples/common.h [new file with mode: 0644]
source3/lib/netapi/examples/getdc/getdc.c
source3/lib/netapi/examples/getjoinableous/getjoinableous.c
source3/lib/netapi/examples/netdomjoin/netdomjoin.c

index 000eef118b1ff02b3f2cb0d2daac51d6fb14050b..9020d602244ef1a324a8b84707f87a968cdfda84 100644 (file)
@@ -10,6 +10,8 @@ CC=@CC@
 PICFLAG=@PICFLAG@
 LDFLAGS=@PIE_LDFLAGS@ @LDFLAGS@
 DYNEXP=@DYNEXP@
+NETAPI_LIBS=$(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+CMDLINE_LIBS=$(NETAPI_LIBS) @POPTLIBS@
 
 # Compile a source file.
 COMPILE_CC = $(CC) -I. $(FLAGS) $(PICFLAG) -c $< -o $@
@@ -46,22 +48,23 @@ bin/.dummy:
                echo "$(COMPILE_CC)" 1>&2;\
                $(COMPILE_CC) >/dev/null 2>&1
 
-GETDC_OBJ = getdc/getdc.o
-NETDOMJOIN_OBJ = netdomjoin/netdomjoin.o
+CMDLINE_OBJ = common.o
+GETDC_OBJ = getdc/getdc.o $(CMDLINE_OBJ)
+NETDOMJOIN_OBJ = netdomjoin/netdomjoin.o $(CMDLINE_OBJ)
 NETDOMJOIN_GUI_OBJ = netdomjoin-gui/netdomjoin-gui.o
-GETJOINABLEOUS_OBJ = getjoinableous/getjoinableous.o
+GETJOINABLEOUS_OBJ = getjoinableous/getjoinableous.o $(CMDLINE_OBJ)
 
 bin/getdc@EXEEXT@: $(BINARY_PREREQS) $(GETDC_OBJ)
        @echo Linking $@
-       @$(CC) $(FLAGS) -o $@ $(GETDC_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+       @$(CC) $(FLAGS) -o $@ $(GETDC_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/getjoinableous@EXEEXT@: $(BINARY_PREREQS) $(GETJOINABLEOUS_OBJ)
        @echo Linking $@
-       @$(CC) $(FLAGS) -o $@ $(GETJOINABLEOUS_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+       @$(CC) $(FLAGS) -o $@ $(GETJOINABLEOUS_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/netdomjoin@EXEEXT@: $(BINARY_PREREQS) $(NETDOMJOIN_OBJ)
        @echo Linking $@
-       @$(CC) $(FLAGS) -o $@ $(NETDOMJOIN_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+       @$(CC) $(FLAGS) -o $@ $(NETDOMJOIN_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/netdomjoin-gui@EXEEXT@: $(BINARY_PREREQS) $(NETDOMJOIN_GUI_OBJ)
        @echo Linking $@
diff --git a/source3/lib/netapi/examples/common.c b/source3/lib/netapi/examples/common.c
new file mode 100644 (file)
index 0000000..db9ab0a
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <string.h>
+#include <netapi.h>
+#include <popt.h>
+
+void popt_common_callback(poptContext con,
+                        enum poptCallbackReason reason,
+                        const struct poptOption *opt,
+                        const char *arg, const void *data)
+{
+       struct libnetapi_ctx *ctx = NULL;
+
+       libnetapi_getctx(&ctx);
+
+       if (reason == POPT_CALLBACK_REASON_PRE) {
+       }
+
+       if (reason == POPT_CALLBACK_REASON_POST) {
+       }
+
+       if (!opt) {
+               return;
+       }
+       switch (opt->val) {
+               case 'U': {
+                       char *puser = strdup(arg);
+                       char *p = NULL;
+
+                       if ((p = strchr(puser,'%'))) {
+                               size_t len;
+                               *p = 0;
+                               libnetapi_set_username(ctx, puser);
+                               libnetapi_set_password(ctx, p+1);
+                               len = strlen(p+1);
+                               memset(strchr(arg,'%')+1,'X',len);
+                       } else {
+                               libnetapi_set_username(ctx, puser);
+                       }
+                       free(puser);
+                       break;
+               }
+               case 'd':
+                       libnetapi_set_debuglevel(ctx, arg);
+                       break;
+               case 'p':
+                       libnetapi_set_password(ctx, arg);
+                       break;
+       }
+}
+
+struct poptOption popt_common_netapi_examples[] = {
+       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
+       { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Username used for connection", "USERNAME" },
+       { "password", 'p', POPT_ARG_STRING, NULL, 'p', "Password used for connection", "PASSWORD" },
+       { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Debuglevel", "DEBUGLEVEL" },
+       POPT_TABLEEND
+};
+
diff --git a/source3/lib/netapi/examples/common.h b/source3/lib/netapi/examples/common.h
new file mode 100644 (file)
index 0000000..85df51d
--- /dev/null
@@ -0,0 +1,11 @@
+#include <popt.h>
+
+void popt_common_callback(poptContext con,
+                        enum poptCallbackReason reason,
+                        const struct poptOption *opt,
+                        const char *arg, const void *data);
+
+extern struct poptOption popt_common_netapi_examples[];
+
+#define POPT_COMMON_LIBNETAPI_EXAMPLES { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_netapi_examples, 0, "Common samba netapi example options:", NULL },
+
index 272ba1088ebf36553507a64f9207037110f3dc6d..98bb6a13b245cae9c0e989f099094caedf606792 100644 (file)
 
 #include <netapi.h>
 
-int main(int argc, char **argv)
+#include "common.h"
+
+int main(int argc, const char **argv)
 {
        NET_API_STATUS status;
        struct libnetapi_ctx *ctx = NULL;
+
+       const char *hostname = NULL;
+       const char *domain = NULL;
        uint8_t *buffer = NULL;
 
-       if (argc < 3) {
-               printf("usage: getdc <hostname> <domain>\n");
-               return -1;
-       }
+       poptContext pc;
+       int opt;
+
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               POPT_COMMON_LIBNETAPI_EXAMPLES
+               POPT_TABLEEND
+       };
 
        status = libnetapi_init(&ctx);
        if (status != 0) {
                return status;
        }
 
-       libnetapi_set_username(ctx, "");
-       libnetapi_set_password(ctx, "");
+       pc = poptGetContext("getdc", argc, argv, long_options, 0);
+
+       poptSetOtherOptionHelp(pc, "hostname domainname");
+       while((opt = poptGetNextOpt(pc)) != -1) {
+       }
+
+       if (!poptPeekArg(pc)) {
+               poptPrintHelp(pc, stderr, 0);
+               goto out;
+       }
+       hostname = poptGetArg(pc);
+
+       if (!poptPeekArg(pc)) {
+               poptPrintHelp(pc, stderr, 0);
+               goto out;
+       }
+       domain = poptGetArg(pc);
+
+       /* NetGetDCName */
 
-       status = NetGetDCName(argv[1], argv[2], &buffer);
+       status = NetGetDCName(hostname, domain, &buffer);
        if (status != 0) {
                printf("GetDcName failed with: %s\n", libnetapi_errstr(status));
        } else {
                printf("%s\n", (char *)buffer);
        }
+
+ out:
        NetApiBufferFree(buffer);
        libnetapi_free(ctx);
+       poptFreeContext(pc);
 
        return status;
 }
index be95198bcf91d2527d9f8ad72c5419f5fa65d3d8..732f73dd572556b513d94a7125746d0c6b4714cd 100644 (file)
 
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
 #include <inttypes.h>
 
 #include <netapi.h>
 
-char *get_string_param(const char *param)
-{
-       char *p;
-
-       p = strchr(param, '=');
-       if (!p) {
-               return NULL;
-       }
-
-       return (p+1);
-}
+#include "common.h"
 
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
        NET_API_STATUS status;
-       const char *server_name = NULL;
+       const char *host_name = NULL;
        const char *domain_name = NULL;
-       const char *account = NULL;
-       const char *password = NULL;
        const char **ous = NULL;
        uint32_t num_ous = 0;
        struct libnetapi_ctx *ctx = NULL;
        int i;
 
+       poptContext pc;
+       int opt;
+
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               { "domain", 0, POPT_ARG_STRING, NULL, 'D', "Domain name", "DOMAIN" },
+               POPT_COMMON_LIBNETAPI_EXAMPLES
+               POPT_TABLEEND
+       };
+
        status = libnetapi_init(&ctx);
        if (status != 0) {
                return status;
        }
 
-       if (argc < 2) {
-               printf("usage: getjoinableous\n");
-               printf("\t<hostname> [domain=DOMAIN] <user=USER> <password=PASSWORD>\n");
-               return 0;
-       }
+       pc = poptGetContext("getjoinableous", argc, argv, long_options, 0);
 
-       if (argc > 2) {
-               server_name = argv[1];
+       poptSetOtherOptionHelp(pc, "hostname domainname");
+       while((opt = poptGetNextOpt(pc)) != -1) {
+               switch (opt) {
+                       case 'D':
+                               domain_name = poptGetOptArg(pc);
+                               break;
+               }
        }
 
-       for (i=0; i<argc; i++) {
-               if (strncasecmp(argv[i], "domain", strlen("domain"))== 0) {
-                       domain_name = get_string_param(argv[i]);
-               }
-               if (strncasecmp(argv[i], "user", strlen("user"))== 0) {
-                       account = get_string_param(argv[i]);
-                       libnetapi_set_username(ctx, account);
-               }
-               if (strncasecmp(argv[i], "password", strlen("password"))== 0) {
-                       password = get_string_param(argv[i]);
-                       libnetapi_set_password(ctx, password);
-               }
-               if (strncasecmp(argv[i], "debug", strlen("debug"))== 0) {
-                       const char *str = NULL;
-                       str = get_string_param(argv[i]);
-                       libnetapi_set_debuglevel(ctx, str);
-               }
+       if (!poptPeekArg(pc)) {
+               poptPrintHelp(pc, stderr, 0);
+               goto out;
        }
+       host_name = poptGetArg(pc);
+
+       /* NetGetJoinableOUs */
 
-       status = NetGetJoinableOUs(server_name,
+       status = NetGetJoinableOUs(host_name,
                                   domain_name,
-                                  account,
-                                  password,
+                                  ctx->username,
+                                  ctx->password,
                                   &num_ous,
                                   &ous);
        if (status != 0) {
@@ -97,9 +86,10 @@ int main(int argc, char **argv)
                }
        }
 
+ out:
        NetApiBufferFree(ous);
-
        libnetapi_free(ctx);
+       poptFreeContext(pc);
 
        return status;
 }
index 29f66a17a2dfa76271d5fbe524f27306fc5308e8..bd7c36382a85a0272fb8d32f7b491593ca126923 100644 (file)
 
 #include <netapi.h>
 
-char *get_string_param(const char *param)
-{
-       char *p;
+#include "common.h"
 
-       p = strchr(param, '=');
-       if (!p) {
-               return NULL;
-       }
+enum {
+       OPT_OU = 1000
+};
 
-       return (p+1);
-}
-
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
        NET_API_STATUS status;
-       const char *server_name = NULL;
+       const char *host_name = NULL;
        const char *domain_name = NULL;
        const char *account_ou = NULL;
-       const char *Account = NULL;
+       const char *account = NULL;
        const char *password = NULL;
-       uint32_t join_flags = 3;
+       uint32_t join_flags = 0x00000023;
        struct libnetapi_ctx *ctx = NULL;
-       int i;
+
+       poptContext pc;
+       int opt;
+
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               { "ou", 0, POPT_ARG_STRING, &account_ou, 'U', "Account ou", "ACCOUNT_OU" },
+               { "domain", 0, POPT_ARG_STRING, &domain_name, 'U', "Domain name (required)", "DOMAIN" },
+               { "userd", 0, POPT_ARG_STRING, &account, 'U', "Domain admin account", "USERNAME" },
+               { "passwordd", 0, POPT_ARG_STRING, &password, 'U', "Domain admin password", "PASSWORD" },
+               POPT_COMMON_LIBNETAPI_EXAMPLES
+               POPT_TABLEEND
+       };
+
 
        status = libnetapi_init(&ctx);
        if (status != 0) {
                return status;
        }
 
-       if (argc < 2) {
-               printf("usage: netdomjoin\n");
-               printf("\t[hostname] [domain=DOMAIN] <ou=OU> "
-                      "<usero=USERO> <passwordo=PASSWORDO> "
-                      "<userd=USERD> <passwordd=PASSWORDD> "
-                      "<debug=DEBUGLEVEL>\n");
-               return 0;
+       pc = poptGetContext("netdomjoin", argc, argv, long_options, 0);
+
+       poptSetOtherOptionHelp(pc, "hostname");
+       while((opt = poptGetNextOpt(pc)) != -1) {
        }
 
-       if (argc > 2) {
-               server_name = argv[1];
+       if (!poptPeekArg(pc)) {
+               poptPrintHelp(pc, stderr, 0);
+               goto out;
        }
+       host_name = poptGetArg(pc);
 
-       for (i=0; i<argc; i++) {
-               if (strncasecmp(argv[i], "ou", strlen("ou")) == 0) {
-                       account_ou = get_string_param(argv[i]);
-               }
-               if (strncasecmp(argv[i], "domain", strlen("domain"))== 0) {
-                       domain_name = get_string_param(argv[i]);
-               }
-               if (strncasecmp(argv[i], "userd", strlen("userd"))== 0) {
-                       Account = get_string_param(argv[i]);
-               }
-               if (strncasecmp(argv[i], "passwordd", strlen("passwordd"))== 0) {
-                       password = get_string_param(argv[i]);
-               }
-               if (strncasecmp(argv[i], "usero", strlen("usero"))== 0) {
-                       const char *str = NULL;
-                       str = get_string_param(argv[i]);
-                       libnetapi_set_username(ctx, str);
-               }
-               if (strncasecmp(argv[i], "passwordo", strlen("passwordo"))== 0) {
-                       const char *str = NULL;
-                       str = get_string_param(argv[i]);
-                       libnetapi_set_password(ctx, str);
-               }
-               if (strncasecmp(argv[i], "debug", strlen("debug"))== 0) {
-                       const char *str = NULL;
-                       str = get_string_param(argv[i]);
-                       libnetapi_set_debuglevel(ctx, str);
-               }
+       if (!domain_name) {
+               poptPrintHelp(pc, stderr, 0);
+               goto out;
        }
 
-       status = NetJoinDomain(server_name,
+       /* NetJoinDomain */
+
+       status = NetJoinDomain(host_name,
                               domain_name,
                               account_ou,
-                              Account,
+                              account,
                               password,
                               join_flags);
        if (status != 0) {
                const char *errstr = NULL;
                errstr = libnetapi_get_error_string(ctx, status);
-               if (!errstr) {
-                       errstr = libnetapi_errstr(status);
-               }
                printf("Join failed with: %s\n", errstr);
        } else {
                printf("Successfully joined\n");
        }
 
+ out:
        libnetapi_free(ctx);
+       poptFreeContext(pc);
 
        return status;
 }