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>
28 #include <atalk/logger.h>
29 #include <atalk/afp.h>
30 #include <atalk/uuid.h>
31 #include <atalk/ldapconfig.h>
32 #include <atalk/util.h>
37 char *uuidtype[] = {"", "USER", "GROUP", "LOCAL"};
39 /********************************************************
40 * Public helper function
41 ********************************************************/
43 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
48 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
49 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
51 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
57 memcpy(buf, local_group_uuid, 12);
61 memcpy(buf, local_user_uuid, 12);
66 memcpy(buf + 12, &tmp, 4);
72 * convert ascii string that can include dashes to binary uuid.
73 * caller must provide a buffer.
75 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
78 unsigned char c, val = 0;
80 while (*uuidstring && i < UUID_BINSIZE) {
86 else if (c <= '9') /* 0-9 */
88 else if (c <= 'F') /* A-F */
90 else if (c <= 'f') /* a-f */
105 * Convert 16 byte binary uuid to neat ascii represantation including dashes.
106 * Use defined or default ascii mask for dash placement
107 * Returns pointer to static buffer.
109 const char *uuid_bin2string(unsigned char *uuid) {
110 static char uuidstring[64];
111 const char *uuidmask;
116 if (ldap_uuid_string)
117 uuidmask = ldap_uuid_string;
120 uuidmask = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
122 LOG(log_debug, logtype_afpd, "uuid_bin2string{uuid}: mask: %s", uuidmask);
124 while (i < strlen(uuidmask)) {
127 sprintf(uuidstring + i, "%02X", c);
129 if (uuidmask[i] == '-')
130 uuidstring[i++] = '-';
136 /********************************************************
138 ********************************************************/
141 * name: give me his name
142 * type: and type (UUID_USER or UUID_GROUP)
143 * uuid: pointer to uuid_t storage that the caller must provide
144 * returns 0 on success !=0 on errror
146 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
148 uuidtype_t mytype = type;
149 char nulluuid[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
151 char *uuid_string = NULL;
154 ret = search_cachebyname(name, &mytype, uuid);
158 LOG(log_debug, logtype_afpd,
159 "getuuidfromname{cache}: name: %s, type%s: %s -> UUID: %s",
161 (mytype & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
162 uuidtype[type & UUIDTYPESTR_MASK],
163 uuid_bin2string(uuid));
164 if ((mytype & UUID_ENOENT) == UUID_ENOENT)
167 /* if not found in cache */
169 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
170 uuid_string2bin( uuid_string, uuid);
171 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",
172 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
174 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
179 /* Build a local UUID */
180 if (type == UUID_USER) {
182 if ((pwd = getpwnam(name)) == NULL) {
183 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
184 name, uuidtype[type & UUIDTYPESTR_MASK]);
185 mytype |= UUID_ENOENT;
186 memcpy(uuid, nulluuid, 16);
188 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
190 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
191 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
195 if ((grp = getgrnam(name)) == NULL) {
196 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
197 name, uuidtype[type & UUIDTYPESTR_MASK]);
198 mytype |= UUID_ENOENT;
199 memcpy(uuid, nulluuid, 16);
201 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
203 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
204 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
208 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) {
234 if (search_cachebyuuid(uuidp, name, type) == 0) {
236 LOG(log_debug, logtype_afpd,
237 "getnamefromuuid{cache}: UUID: %s -> name: %s, type%s: %s",
238 uuid_bin2string(uuidp),
240 (*type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
241 uuidtype[(*type) & UUIDTYPESTR_MASK]);
242 if ((*type & UUID_ENOENT) == UUID_ENOENT)
247 /* not found in cache */
249 /* Check if UUID is a client local one */
250 if (memcmp(uuidp, local_user_uuid, 12) == 0) {
252 uid = ntohl(*(uint32_t *)(uuidp + 12));
253 if ((pwd = getpwuid(uid)) == NULL) {
254 /* not found, add negative entry to cache */
255 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
258 *name = strdup(pwd->pw_name);
259 add_cachebyuuid(uuidp, *name, *type, 0);
262 LOG(log_debug, logtype_afpd,
263 "getnamefromuuid{local}: UUID: %s -> name: %s, type:%s",
264 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
266 } else if (memcmp(uuidp, local_group_uuid, 12) == 0) {
268 gid = ntohl(*(uint32_t *)(uuidp + 12));
269 if ((grp = getgrgid(gid)) == NULL) {
270 /* not found, add negative entry to cache */
271 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
274 *name = strdup(grp->gr_name);
275 add_cachebyuuid(uuidp, *name, *type, 0);
282 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
288 LOG(log_debug, logtype_afpd, "getnamefromuuid(%s): not found",
289 uuid_bin2string(uuidp));
290 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
294 add_cachebyuuid(uuidp, *name, *type, 0);
296 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
297 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);