2 Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
25 #include <atalk/logger.h>
26 #include <atalk/afp.h>
27 #include <atalk/uuid.h>
28 #include <atalk/util.h>
33 char *uuidtype[] = {"NULL","USER", "GROUP", "LOCAL"};
35 /********************************************************
36 * Public helper function
37 ********************************************************/
40 * convert ascii string that can include dashes to binary uuid.
41 * caller must provide a buffer.
43 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
46 unsigned char c, val = 0;
54 else if (c <= '9') /* 0-9 */
56 else if (c <= 'F') /* A-F */
58 else if (c <= 'f') /* a-f */
73 * Convert 16 byte binary uuid to neat ascii represantation including dashes.
75 * Returns pointer to static buffer.
77 const char *uuid_bin2string(char *uuid) {
78 static char uuidstring[UUID_STRINGSIZE + 1];
79 char ascii[16] = { "0123456789ABCDEF" };
84 while (i < UUID_STRINGSIZE) {
92 uuidstring[i] = ascii[c];
95 if (i==8 || i==13 || i==18 || i==23)
96 uuidstring[i++] = '-';
102 /********************************************************
104 ********************************************************/
107 * name: give me his name
108 * type: and type (UUID_USER or UUID_GROUP)
109 * uuid: pointer to uuid_t storage that the caller must provide
110 * returns 0 on success !=0 on errror
112 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
114 char *uuid_string = NULL;
116 ret = search_cachebyname( name, type, uuid);
119 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
120 name, uuidtype[type], uuid_bin2string(uuid));
122 /* if not found in cache */
123 ret = ldap_getuuidfromname( name, type, &uuid_string);
125 LOG(log_note, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
129 uuid_string2bin( uuid_string, uuid);
130 add_cachebyname( name, uuid, type, 0);
131 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",name, uuidtype[type], uuid_string);
135 if (uuid_string) free(uuid_string);
139 static char local_group_uuid[] = {0xab, 0xcd, 0xef,
144 static char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
145 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
148 * uuidp: pointer to a uuid
149 * name: returns allocated buffer from ldap_getnamefromuuid
150 * type: returns USER, GROUP or LOCAL
151 * return 0 on success !=0 on errror
153 * Caller must free name appropiately.
155 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
158 ret = search_cachebyuuid( uuidp, name, type);
161 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
162 uuid_bin2string(uuidp), *name, uuidtype[*type]);
164 /* not found in cache */
166 /* Check if UUID is a client local one */
167 if (memcmp(uuidp, local_user_uuid, 12) == 0
168 || memcmp(uuidp, local_group_uuid, 12) == 0) {
169 LOG(log_debug, logtype_afpd, "getnamefromuuid: local UUID: %" PRIu32 "",
170 ntohl(*(uint32_t *)(uuidp + 12)));
172 *name = strdup("UUID_LOCAL");
176 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
178 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
179 uuid_bin2string(uuidp));
182 add_cachebyuuid( uuidp, *name, *type, 0);
183 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
184 uuid_bin2string(uuidp), *name, uuidtype[*type]);