From cccaef9e0896e9a3fcf9b860283543fd35d3f248 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Tue, 8 Apr 2008 14:34:30 +0200 Subject: Use popt in libetapi example code. Guenther (This used to be commit 6f239df3f5a57c9549f1637e53fd42d2ed604c3f) --- source3/lib/netapi/examples/Makefile.in | 15 ++-- source3/lib/netapi/examples/common.c | 58 +++++++++++++ source3/lib/netapi/examples/common.h | 11 +++ source3/lib/netapi/examples/getdc/getdc.c | 45 ++++++++-- .../examples/getjoinableous/getjoinableous.c | 76 ++++++++--------- .../lib/netapi/examples/netdomjoin/netdomjoin.c | 96 +++++++++------------- 6 files changed, 187 insertions(+), 114 deletions(-) create mode 100644 source3/lib/netapi/examples/common.c create mode 100644 source3/lib/netapi/examples/common.h (limited to 'source3/lib') diff --git a/source3/lib/netapi/examples/Makefile.in b/source3/lib/netapi/examples/Makefile.in index 000eef118b..9020d60224 100644 --- a/source3/lib/netapi/examples/Makefile.in +++ b/source3/lib/netapi/examples/Makefile.in @@ -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 index 0000000000..db9ab0a2c9 --- /dev/null +++ b/source3/lib/netapi/examples/common.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +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 index 0000000000..85df51d868 --- /dev/null +++ b/source3/lib/netapi/examples/common.h @@ -0,0 +1,11 @@ +#include + +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 }, + diff --git a/source3/lib/netapi/examples/getdc/getdc.c b/source3/lib/netapi/examples/getdc/getdc.c index 272ba1088e..98bb6a13b2 100644 --- a/source3/lib/netapi/examples/getdc/getdc.c +++ b/source3/lib/netapi/examples/getdc/getdc.c @@ -25,33 +25,62 @@ #include -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 \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; } diff --git a/source3/lib/netapi/examples/getjoinableous/getjoinableous.c b/source3/lib/netapi/examples/getjoinableous/getjoinableous.c index be95198bcf..732f73dd57 100644 --- a/source3/lib/netapi/examples/getjoinableous/getjoinableous.c +++ b/source3/lib/netapi/examples/getjoinableous/getjoinableous.c @@ -19,72 +19,61 @@ #include #include +#include #include #include -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 [domain=DOMAIN] \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; iusername, + 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; } diff --git a/source3/lib/netapi/examples/netdomjoin/netdomjoin.c b/source3/lib/netapi/examples/netdomjoin/netdomjoin.c index 29f66a17a2..bd7c36382a 100644 --- a/source3/lib/netapi/examples/netdomjoin/netdomjoin.c +++ b/source3/lib/netapi/examples/netdomjoin/netdomjoin.c @@ -25,96 +25,78 @@ #include -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] " - " " - " " - "\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