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