From 8644d276bad3bda67ce4152a765402361d11f07a Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Thu, 2 Feb 2012 08:55:28 +0100 Subject: [PATCH 05/10] AUTOFS: a command-line test client A very simply binary that can be used to test getting data from the library via SSSD in pretty much the same way SSSD would. A required positional parameter specifies the map name and the tool would print out all the key/value pairs using _sss_getautomntent_r(). You can also specify -n to query a specific key using _sss_getautomntbyname_r(). --- .gitignore | 1 + Makefile.am | 8 ++ src/sss_client/autofs/autofs_test_client.c | 165 ++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 0 deletions(-) create mode 100644 src/sss_client/autofs/autofs_test_client.c diff --git a/.gitignore b/.gitignore index dff3ae248172ad2ebb3dbf47f8a64c69bb617da7..7510e61c9ae23e813a8cf18f5add4a7e7bd2d9b9 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,4 @@ auth-tests check_and_open-tests sssd_sudo sss_sudo_cli +autofs_test_client diff --git a/Makefile.am b/Makefile.am index a764649da7392685b267c2ec8fefb703ca024834..77a3f2c61846c432061e9b140635724bcfa1f22c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -844,10 +844,18 @@ noinst_PROGRAMS = pam_test_client if BUILD_SUDO noinst_PROGRAMS += sss_sudo_cli endif +if BUILD_AUTOFS +noinst_PROGRAMS += autofs_test_client +endif pam_test_client_SOURCES = src/sss_client/pam_test_client.c pam_test_client_LDFLAGS = -lpam -lpam_misc +if BUILD_AUTOFS +autofs_test_client_SOURCES = src/sss_client/autofs/autofs_test_client.c +autofs_test_client_LDFLAGS = -ldl -lpopt +endif + #################### # Client Libraries # #################### diff --git a/src/sss_client/autofs/autofs_test_client.c b/src/sss_client/autofs/autofs_test_client.c new file mode 100644 index 0000000000000000000000000000000000000000..c62dbd090cef6d92116c5abf98dd376c5f47a292 --- /dev/null +++ b/src/sss_client/autofs/autofs_test_client.c @@ -0,0 +1,165 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2012 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#include "util/util.h" + +/* FIXME - get from autoconf */ +#define LIBPATH "./.libs/libsss_autofs.so" + +#define SETAUTOMTENT_FN "_sss_setautomntent" +#define GETAUTOMTENT_FN "_sss_getautomntent_r" +#define GETAUTOMTBYNAME_FN "_sss_getautomntbyname_r" +#define ENDAUTOMTENT_FN "_sss_endautomntent" + +struct automtent { + const char *mapname; + size_t cursor; +}; + +struct autofs_client_ctx { + void *dlhandle; + errno_t (*setautomntent)(const char *, void **); + errno_t (*getautomntent_r)(char **, char **, void *); + errno_t (*endautomntent)(void **); + errno_t (*getautomntbyname_r)(char *, char **, void *); +}; + +int main(int argc, const char *argv[]) +{ + char *error; + struct autofs_client_ctx context; + void *ctx; + errno_t ret; + const char *mapname; + char *key; + char *value; + char *pc_key = NULL; + struct poptOption long_options[] = { + POPT_AUTOHELP + { "by-name", 'n', POPT_ARG_STRING, &pc_key, 0, "Request map by name", NULL }, + POPT_TABLEEND + }; + poptContext pc = NULL; + + pc = poptGetContext(NULL, argc, argv, long_options, 0); + poptSetOtherOptionHelp(pc, "MAPNAME"); + + while ((ret = poptGetNextOpt(pc)) > 0) + ; + + mapname = poptGetArg(pc); + if (mapname == NULL) { + poptPrintUsage(pc, stderr, 0); + fprintf(stderr, "Please specify the automounter map name\n"); + exit(EXIT_FAILURE); + } + + context.dlhandle = dlopen(LIBPATH, RTLD_LAZY); + if (!context.dlhandle) { + fprintf(stderr, "%s\n", dlerror()); + exit(EXIT_FAILURE); + } + + context.setautomntent = dlsym(context.dlhandle, SETAUTOMTENT_FN); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(EXIT_FAILURE); + } + + context.getautomntent_r = dlsym(context.dlhandle, GETAUTOMTENT_FN); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(EXIT_FAILURE); + } + + context.getautomntbyname_r = dlsym(context.dlhandle, GETAUTOMTBYNAME_FN); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(EXIT_FAILURE); + } + + context.endautomntent = dlsym(context.dlhandle, ENDAUTOMTENT_FN); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(EXIT_FAILURE); + } + printf("symbol table loaded\n"); + + ret = context.setautomntent(mapname, &ctx); + if (ret) { + fprintf(stderr, "setautomntent failed [%d]: %s\n", + ret, strerror(ret)); + exit(EXIT_FAILURE); + } + printf("setautomntent done for %s\n", mapname); + + if (!pc_key) { + key = NULL; + value = NULL; + do { + ret = context.getautomntent_r(&key, &value, ctx); + if (ret == 0) { + printf("key: %s\t\tvalue: %s\n", key, value); + } + free(key); + free(value); + } while(ret == 0); + + if (ret != 0 && ret != ENOENT) { + fprintf(stderr, "context.getautomntent_r failed [%d]: %s\n", + ret, strerror(ret)); + exit(EXIT_FAILURE); + } + } else { + ret = context.getautomntbyname_r(pc_key, &value, ctx); + if (ret == ENOENT) { + fprintf(stderr, "no such entry in map\n"); + } else if (ret != 0) { + fprintf(stderr, "context.getautomntent_r failed [%d]: %s\n", + ret, strerror(ret)); + exit(EXIT_FAILURE); + } else { + printf("key: %s\t\tvalue: %s\n", pc_key, value); + } + free(value); + } + + ret = context.endautomntent(&ctx); + if (ret) { + fprintf(stderr, "endautomntent failed [%d]: %s\n", + ret, strerror(ret)); + exit(EXIT_FAILURE); + } + printf("endautomntent done for %s\n", mapname); + + poptFreeContext(pc); + dlclose(context.dlhandle); + return 0; +} -- 1.7.7.6