6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek/*
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek Authors:
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek Copyright (C) 2012 Red Hat
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek This program is free software; you can redistribute it and/or modify
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek it under the terms of the GNU Lesser General Public License as published by
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek the Free Software Foundation; either version 3 of the License, or
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek (at your option) any later version.
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek This program is distributed in the hope that it will be useful,
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek GNU Lesser General Public License for more details.
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek You should have received a copy of the GNU Lesser General Public License
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek*/
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#ifdef HAVE_CONFIG_H
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <config.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#endif
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <stdio.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <stdlib.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <dlfcn.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <errno.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include <popt.h>
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include "util/util.h"
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek#include "sss_client/autofs/sss_autofs_private.h"
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozekstruct automtent {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek const char *mapname;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek size_t cursor;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek};
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozekint main(int argc, const char *argv[])
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek{
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek void *ctx;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek errno_t ret;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek const char *mapname;
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek char *key = NULL;
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek char *value = NULL;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek char *pc_key = NULL;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek struct poptOption long_options[] = {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek POPT_AUTOHELP
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek { "by-name", 'n', POPT_ARG_STRING, &pc_key, 0, "Request map by name", NULL },
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek POPT_TABLEEND
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek };
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek poptContext pc = NULL;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek pc = poptGetContext(NULL, argc, argv, long_options, 0);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek poptSetOtherOptionHelp(pc, "MAPNAME");
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek while ((ret = poptGetNextOpt(pc)) > 0)
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek mapname = poptGetArg(pc);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (mapname == NULL) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek poptPrintUsage(pc, stderr, 0);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "Please specify the automounter map name\n");
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek poptFreeContext(pc);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek exit(EXIT_FAILURE);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek poptFreeContext(pc);
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret = _sss_setautomntent(mapname, &ctx);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (ret) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "setautomntent failed [%d]: %s\n",
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret, strerror(ret));
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek exit(EXIT_FAILURE);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek printf("setautomntent done for %s\n", mapname);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (!pc_key) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek do {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret = _sss_getautomntent_r(&key, &value, ctx);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (ret == 0) {
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek if (!key || !value) {
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek fprintf(stderr,
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek "getautomntent returned success but no data?\n");
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek goto end;
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek }
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek printf("key: %s\t\tvalue: %s\n", key, value);
bd72b3510105599b5bdaf00c203df1164b304238Jakub Hrozek free(key);
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek key = NULL;
bd72b3510105599b5bdaf00c203df1164b304238Jakub Hrozek free(value);
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek value = NULL;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek } while(ret == 0);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (ret != 0 && ret != ENOENT) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret, strerror(ret));
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek goto end;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek } else {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret = _sss_getautomntbyname_r(pc_key, &value, ctx);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (ret == ENOENT) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "no such entry in map\n");
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek } else if (ret != 0) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret, strerror(ret));
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek goto end;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek } else {
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek if (!value) {
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek fprintf(stderr, "_sss_getautomntbyname_r "
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek "returned success but no data?\n");
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek goto end;
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek }
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozek
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek printf("key: %s\t\tvalue: %s\n", pc_key, value);
bd72b3510105599b5bdaf00c203df1164b304238Jakub Hrozek free(value);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek
612231a05770a79b8ff53f91d85f5be7595ed785Jakub Hrozekend:
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret = _sss_endautomntent(&ctx);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek if (ret) {
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek fprintf(stderr, "endautomntent failed [%d]: %s\n",
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek ret, strerror(ret));
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek exit(EXIT_FAILURE);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek }
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek printf("endautomntent done for %s\n", mapname);
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek return 0;
6e8238868a4d17030bb4f01494961d0354a953bfJakub Hrozek}