]> arthur.barton.de Git - netatalk.git/blob - bin/misc/uuidtest.c
Fix build
[netatalk.git] / bin / misc / uuidtest.c
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
36 static void usage()
37 {
38     printf("Usage: afpldaptest -u <user> | -g <group> | -i <UUID>\n");
39 }
40
41 static void parse_ldapconf()
42 {
43     static int inited = 0;
44     dictionary *iniconfig;
45
46     if (! inited) {
47 #ifdef HAVE_LDAP
48         /* Parse afp_ldap.conf */
49         printf("Start parsing afp_ldap.conf\n");
50         iniconfig = iniparser_load(_PATH_CONFDIR "afp.conf");
51         acl_ldap_readconfig(iniconfig);
52         printf("Finished parsing afp_ldap.conf\n");
53         if (ldap_config_valid) {
54             if (ldap_auth_method == LDAP_AUTH_NONE)
55                 printf("afp_ldap.conf is ok. Using anonymous bind.\n");
56             else if (ldap_auth_method == LDAP_AUTH_SIMPLE)
57                 printf("afp_ldap.conf is ok. Using simple bind.\n");
58             else {
59                 ldap_config_valid = 0;
60                 printf("afp_ldap.conf wants SASL which is not yet supported.\n");
61                 exit(EXIT_FAILURE);
62             }
63         } else {
64             printf("afp_ldap.conf is not ok, not using LDAP. Only local UUID testing available.\n");
65         }
66 #else
67         printf("Built without LDAP support, only local UUID testing available.\n");
68 #endif
69         inited = 1;
70     }
71 }
72
73 int main( int argc, char **argv)
74 {
75     int ret, c;
76     int verbose = 0;
77     atalk_uuid_t uuid;
78     int logsetup = 0;
79     uuidtype_t type;
80     char *name = NULL;
81
82     while ((c = getopt(argc, argv, ":vu:g:i:")) != -1) {
83         switch(c) {
84
85         case 'v':
86             if (! verbose) {
87                 verbose = 1;
88                 setuplog("default:maxdebug", "/dev/tty");
89                 logsetup = 1;
90             }
91             break;
92
93         case 'u':
94             if (! logsetup)
95                 setuplog("default:info", "/dev/tty");
96             parse_ldapconf();
97             printf("Searching user: %s\n", optarg);
98             ret = getuuidfromname( optarg, UUID_USER, uuid);
99             if (ret == 0) {
100                 printf("User: %s ==> UUID: %s\n", optarg, uuid_bin2string(uuid));
101             } else {
102                 printf("User %s not found.\n", optarg);
103             }
104             break;
105
106         case 'g':
107             if (! logsetup)
108                 setuplog("default:info", "/dev/tty");
109             parse_ldapconf();
110             printf("Searching group: %s\n", optarg);
111             ret = getuuidfromname( optarg, UUID_GROUP, uuid);
112             if (ret == 0) {
113                 printf("Group: %s ==> UUID: %s\n", optarg, uuid_bin2string(uuid));
114             } else {
115                 printf("Group %s not found.\n", optarg);
116             }
117             break;
118
119         case 'i':
120             if (! logsetup)
121                 setuplog("default:info", "/dev/tty");
122             parse_ldapconf();
123             printf("Searching uuid: %s\n", optarg);
124             uuid_string2bin(optarg, uuid);
125             ret = getnamefromuuid( uuid, &name, &type);
126             if (ret == 0) {
127                 switch (type) {
128                 case UUID_USER:
129                     printf("UUID: %s ==> User: %s\n", optarg, name);
130                     break;
131                 case UUID_GROUP:
132                     printf("UUID: %s ==> Group: %s\n", optarg, name);
133                     break;
134                 default:
135                     printf("???: %s\n", optarg);
136                     break;
137                 }
138                 free(name);
139             } else {
140                 printf("UUID: %s not found.\n", optarg);
141             }
142             break;
143
144         case ':':
145         case '?':
146         case 'h':
147             usage();
148             exit(EXIT_FAILURE);
149         }
150     }
151
152     return 0;
153 }
154