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 */
24 #include <sys/types.h>
27 #include <arpa/inet.h>
29 #include <atalk/logger.h>
30 #include <atalk/afp.h>
31 #include <atalk/uuid.h>
32 #include <atalk/ldapconfig.h>
33 #include <atalk/util.h>
38 char *uuidtype[] = {"", "USER", "GROUP", "LOCAL"};
40 /********************************************************
41 * Public helper function
42 ********************************************************/
44 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
49 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
50 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
52 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
58 memcpy(buf, local_group_uuid, 12);
62 memcpy(buf, local_user_uuid, 12);
67 memcpy(buf + 12, &tmp, 4);
73 * convert ascii string that can include dashes to binary uuid.
74 * caller must provide a buffer.
76 void uuid_string2bin( const char *uuidstring, unsigned char *uuid) {
79 unsigned char c, val = 0;
81 while (*uuidstring && i < UUID_BINSIZE) {
87 else if (c <= '9') /* 0-9 */
89 else if (c <= 'F') /* A-F */
91 else if (c <= 'f') /* a-f */
106 * Convert 16 byte binary uuid to neat ascii represantation including dashes.
107 * Use defined or default ascii mask for dash placement
108 * Returns pointer to static buffer.
110 const char *uuid_bin2string(const unsigned char *uuid) {
111 static char uuidstring[64];
112 const char *uuidmask;
117 if (ldap_uuid_string)
118 uuidmask = ldap_uuid_string;
121 uuidmask = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
123 LOG(log_debug, logtype_afpd, "uuid_bin2string{uuid}: mask: %s", uuidmask);
125 while (i < strlen(uuidmask)) {
128 sprintf(uuidstring + i, "%02X", c);
130 if (uuidmask[i] == '-')
131 uuidstring[i++] = '-';
137 /********************************************************
139 ********************************************************/
142 * name: give me his name
143 * type: and type (UUID_USER or UUID_GROUP)
144 * uuid: pointer to uuid_t storage that the caller must provide
145 * returns 0 on success !=0 on errror
147 int getuuidfromname( const char *name, uuidtype_t type, unsigned char *uuid) {
149 uuidtype_t mytype = type;
150 char nulluuid[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
152 char *uuid_string = NULL;
155 ret = search_cachebyname(name, &mytype, uuid);
159 LOG(log_debug, logtype_afpd,
160 "getuuidfromname{cache}: name: %s, type%s: %s -> UUID: %s",
162 (mytype & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
163 uuidtype[type & UUIDTYPESTR_MASK],
164 uuid_bin2string(uuid));
165 if ((mytype & UUID_ENOENT) == UUID_ENOENT)
168 /* if not found in cache */
170 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
171 uuid_string2bin( uuid_string, uuid);
172 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",
173 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
175 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
180 /* Build a local UUID */
181 if (type == UUID_USER) {
183 if ((pwd = getpwnam(name)) == NULL) {
184 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
185 name, uuidtype[type & UUIDTYPESTR_MASK]);
186 mytype |= UUID_ENOENT;
187 memcpy(uuid, nulluuid, 16);
189 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
191 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
192 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
196 if ((grp = getgrnam(name)) == NULL) {
197 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
198 name, uuidtype[type & UUIDTYPESTR_MASK]);
199 mytype |= UUID_ENOENT;
200 memcpy(uuid, nulluuid, 16);
202 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
204 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
205 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
209 add_cachebyname(name, uuid, mytype, 0);
213 if (uuid_string) free(uuid_string);
220 * uuidp: pointer to a uuid
221 * name: returns allocated buffer from ldap_getnamefromuuid
222 * type: returns USER, GROUP or LOCAL
223 * return 0 on success !=0 on errror
225 * Caller must free name appropiately.
227 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
235 if (search_cachebyuuid(uuidp, name, type) == 0) {
237 LOG(log_debug, logtype_afpd,
238 "getnamefromuuid{cache}: UUID: %s -> name: %s, type%s: %s",
239 uuid_bin2string(uuidp),
241 (*type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
242 uuidtype[(*type) & UUIDTYPESTR_MASK]);
243 if ((*type & UUID_ENOENT) == UUID_ENOENT)
248 /* not found in cache */
250 /* Check if UUID is a client local one */
251 if (memcmp(uuidp, local_user_uuid, 12) == 0) {
253 memcpy(&tmp, uuidp + 12, sizeof(uint32_t));
255 if ((pwd = getpwuid(uid)) == NULL) {
256 /* not found, add negative entry to cache */
257 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
260 *name = strdup(pwd->pw_name);
261 add_cachebyuuid(uuidp, *name, *type, 0);
264 LOG(log_debug, logtype_afpd,
265 "getnamefromuuid{local}: UUID: %s -> name: %s, type:%s",
266 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
268 } else if (memcmp(uuidp, local_group_uuid, 12) == 0) {
270 memcpy(&tmp, uuidp + 12, sizeof(uint32_t));
272 if ((grp = getgrgid(gid)) == NULL) {
273 /* not found, add negative entry to cache */
274 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
277 *name = strdup(grp->gr_name);
278 add_cachebyuuid(uuidp, *name, *type, 0);
285 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
291 LOG(log_debug, logtype_afpd, "getnamefromuuid(%s): not found",
292 uuid_bin2string(uuidp));
293 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
297 add_cachebyuuid(uuidp, *name, *type, 0);
299 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
300 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);