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;
116 if (ldap_uuid_string)
117 uuidmask = ldap_uuid_string;
119 uuidmask = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
121 LOG(log_debug, logtype_afpd, "uuid_bin2string{uuid}: mask: %s", uuidmask);
123 while (i < strlen(uuidmask)) {
126 sprintf(uuidstring + i, "%02X", c);
128 if (uuidmask[i] == '-')
129 uuidstring[i++] = '-';
135 /********************************************************
137 ********************************************************/
140 * name: give me his name
141 * type: and type (UUID_USER or UUID_GROUP)
142 * uuid: pointer to uuid_t storage that the caller must provide
143 * returns 0 on success !=0 on errror
145 int getuuidfromname( const char *name, uuidtype_t type, unsigned char *uuid) {
147 uuidtype_t mytype = type;
148 char nulluuid[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
150 char *uuid_string = NULL;
153 ret = search_cachebyname(name, &mytype, uuid);
157 LOG(log_debug, logtype_afpd,
158 "getuuidfromname{cache}: name: %s, type%s: %s -> UUID: %s",
160 (mytype & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
161 uuidtype[type & UUIDTYPESTR_MASK],
162 uuid_bin2string(uuid));
163 if ((mytype & UUID_ENOENT) == UUID_ENOENT)
166 /* if not found in cache */
168 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
169 uuid_string2bin( uuid_string, uuid);
170 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",
171 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
173 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
178 /* Build a local UUID */
179 if (type == UUID_USER) {
181 if ((pwd = getpwnam(name)) == NULL) {
182 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
183 name, uuidtype[type & UUIDTYPESTR_MASK]);
184 mytype |= UUID_ENOENT;
185 memcpy(uuid, nulluuid, 16);
187 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
189 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
190 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
194 if ((grp = getgrnam(name)) == NULL) {
195 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
196 name, uuidtype[type & UUIDTYPESTR_MASK]);
197 mytype |= UUID_ENOENT;
198 memcpy(uuid, nulluuid, 16);
200 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
202 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
203 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
207 add_cachebyname(name, uuid, mytype, 0);
212 if (uuid_string) free(uuid_string);
219 * uuidp: pointer to a uuid
220 * name: returns allocated buffer from ldap_getnamefromuuid
221 * type: returns USER, GROUP or LOCAL
222 * return 0 on success !=0 on errror
224 * Caller must free name appropiately.
226 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
233 if (search_cachebyuuid(uuidp, name, type) == 0) {
235 LOG(log_debug, logtype_afpd,
236 "getnamefromuuid{cache}: UUID: %s -> name: %s, type%s: %s",
237 uuid_bin2string(uuidp),
239 (*type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
240 uuidtype[(*type) & UUIDTYPESTR_MASK]);
241 if ((*type & UUID_ENOENT) == UUID_ENOENT)
246 /* not found in cache */
248 /* Check if UUID is a client local one */
249 if (memcmp(uuidp, local_user_uuid, 12) == 0) {
251 uid = ntohl(*(uint32_t *)(uuidp + 12));
252 if ((pwd = getpwuid(uid)) == NULL) {
253 /* not found, add negative entry to cache */
254 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
257 *name = strdup(pwd->pw_name);
258 add_cachebyuuid(uuidp, *name, *type, 0);
261 LOG(log_debug, logtype_afpd,
262 "getnamefromuuid{local}: UUID: %s -> name: %s, type:%s",
263 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
265 } else if (memcmp(uuidp, local_group_uuid, 12) == 0) {
267 gid = ntohl(*(uint32_t *)(uuidp + 12));
268 if ((grp = getgrgid(gid)) == NULL) {
269 /* not found, add negative entry to cache */
270 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
273 *name = strdup(grp->gr_name);
274 add_cachebyuuid(uuidp, *name, *type, 0);
281 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
287 LOG(log_debug, logtype_afpd, "getnamefromuuid(%s): not found",
288 uuid_bin2string(uuidp));
289 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
293 add_cachebyuuid(uuidp, *name, *type, 0);
295 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
296 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);