summaryrefslogtreecommitdiffstats
path: root/src/sss_client/autofs/autofs_test_client.c
blob: f4395ff7c2dd81cb9ff2218101fef09e8ad0b549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
    Authors:
        Jakub Hrozek <jhrozek@redhat.com>

    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 <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
#include <popt.h>

#include "util/util.h"
#include "sss_client/autofs/sss_autofs_private.h"

struct automtent {
    const char *mapname;
    size_t cursor;
};

int main(int argc, const char *argv[])
{
    void *ctx;
    errno_t ret;
    const char *mapname;
    char *key = NULL;
    char *value = NULL;
    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");
        poptFreeContext(pc);
        exit(EXIT_FAILURE);
    }

    poptFreeContext(pc);

    ret = _sss_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) {
        do {
            ret = _sss_getautomntent_r(&key, &value, ctx);
            if (ret == 0) {
                if (!key || !value) {
                    fprintf(stderr,
                            "getautomntent returned success but no data?\n");
                    goto end;
                }

                printf("key: %s\t\tvalue: %s\n", key, value);
                free(key);
                key = NULL;
                free(value);
                value = NULL;
            }
        } while(ret == 0);

        if (ret != 0 && ret != ENOENT) {
            fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
                    ret, strerror(ret));
            goto end;
        }
    } else {
        ret = _sss_getautomntbyname_r(pc_key, &value, ctx);
        if (ret == ENOENT) {
            fprintf(stderr, "no such entry in map\n");
        } else if (ret != 0) {
            fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
                    ret, strerror(ret));
            goto end;
        } else {
            if (!value) {
                fprintf(stderr, "_sss_getautomntbyname_r "
                        "returned success but no data?\n");
                goto end;
            }

            printf("key: %s\t\tvalue: %s\n", pc_key, value);
            free(value);
        }
    }

end:
    ret = _sss_endautomntent(&ctx);
    if (ret) {
        fprintf(stderr, "endautomntent failed [%d]: %s\n",
                ret, strerror(ret));
        exit(EXIT_FAILURE);
    }
    printf("endautomntent done for %s\n", mapname);
    return 0;
}