1/*
2  Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13*/
14
15#ifdef HAVE_CONFIG_H
16#include "config.h"
17#endif /* HAVE_CONFIG_H */
18
19#include <unistd.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <stdarg.h>
24
25#ifdef HAVE_LDAP
26#define LDAP_DEPRECATED 1
27#include <ldap.h>
28#endif
29
30#include <atalk/ldapconfig.h>
31#include <atalk/uuid.h>
32#include <atalk/logger.h>
33
34#define STRNCMP(a, R, b, l) (strncmp(a,b,l) R 0)
35
36static void usage()
37{
38    printf("Usage: afpldaptest -u <user> | -g <group> | -i <UUID>\n");
39}
40
41static void parse_ldapconf()
42{
43    static int inited = 0;
44
45    if (! inited) {
46#ifdef HAVE_LDAP
47        /* Parse afp_ldap.conf */
48        printf("Start parsing afp_ldap.conf\n");
49        acl_ldap_readconfig(_PATH_ACL_LDAPCONF);
50        printf("Finished parsing afp_ldap.conf\n");
51        if (ldap_config_valid) {
52            if (ldap_auth_method == LDAP_AUTH_NONE)
53                printf("afp_ldap.conf is ok. Using anonymous bind.\n");
54            else if (ldap_auth_method == LDAP_AUTH_SIMPLE)
55                printf("afp_ldap.conf is ok. Using simple bind.\n");
56            else {
57                ldap_config_valid = 0;
58                printf("afp_ldap.conf wants SASL which is not yet supported.\n");
59                exit(EXIT_FAILURE);
60            }
61        } else {
62            printf("afp_ldap.conf is not ok, not using LDAP. Only local UUID testing available.\n");
63        }
64#else
65        printf("Built without LDAP support, only local UUID testing available.\n");
66#endif
67        inited = 1;
68    }
69}
70
71int main( int argc, char **argv)
72{
73    int ret, c;
74    int verbose = 0;
75    atalk_uuid_t uuid;
76    int logsetup = 0;
77    uuidtype_t type;
78    char *name = NULL;
79
80    while ((c = getopt(argc, argv, ":vu:g:i:")) != -1) {
81        switch(c) {
82
83        case 'v':
84            if (! verbose) {
85                verbose = 1;
86                setuplog("default log_maxdebug /dev/tty");
87                logsetup = 1;
88            }
89            break;
90
91        case 'u':
92            if (! logsetup)
93                setuplog("default log_info /dev/tty");
94            parse_ldapconf();
95            printf("Searching user: %s\n", optarg);
96            ret = getuuidfromname( optarg, UUID_USER, uuid);
97            if (ret == 0) {
98                printf("User: %s ==> UUID: %s\n", optarg, uuid_bin2string(uuid));
99            } else {
100                printf("User %s not found.\n", optarg);
101            }
102            break;
103
104        case 'g':
105            if (! logsetup)
106                setuplog("default log_info /dev/tty");
107            parse_ldapconf();
108            printf("Searching group: %s\n", optarg);
109            ret = getuuidfromname( optarg, UUID_GROUP, uuid);
110            if (ret == 0) {
111                printf("Group: %s ==> UUID: %s\n", optarg, uuid_bin2string(uuid));
112            } else {
113                printf("Group %s not found.\n", optarg);
114            }
115            break;
116
117        case 'i':
118            if (! logsetup)
119                setuplog("default log_info /dev/tty");
120            parse_ldapconf();
121            printf("Searching uuid: %s\n", optarg);
122            uuid_string2bin(optarg, uuid);
123            ret = getnamefromuuid( uuid, &name, &type);
124            if (ret == 0) {
125                switch (type) {
126                case UUID_USER:
127                    printf("UUID: %s ==> User: %s\n", optarg, name);
128                    break;
129                case UUID_GROUP:
130                    printf("UUID: %s ==> Group: %s\n", optarg, name);
131                    break;
132                default:
133                    printf("???: %s\n", optarg);
134                    break;
135                }
136                free(name);
137            } else {
138                printf("UUID: %s not found.\n", optarg);
139            }
140            break;
141
142        case ':':
143        case '?':
144        case 'h':
145            usage();
146            exit(EXIT_FAILURE);
147        }
148    }
149
150    return 0;
151}
152
153