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/util.h>
36 char *uuidtype[] = {"USER", "GROUP", "LOCAL"};
38 /********************************************************
39 * Public helper function
40 ********************************************************/
42 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
47 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
48 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
50 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
56 memcpy(buf, local_group_uuid, 12);
60 memcpy(buf, local_user_uuid, 12);
65 memcpy(buf + 12, &tmp, 4);
71 * convert ascii string that can include dashes to binary uuid.
72 * caller must provide a buffer.
74 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
77 unsigned char c, val = 0;
85 else if (c <= '9') /* 0-9 */
87 else if (c <= 'F') /* A-F */
89 else if (c <= 'f') /* a-f */
104 * Convert 16 byte binary uuid to neat ascii represantation including dashes.
106 * Returns pointer to static buffer.
108 const char *uuid_bin2string(unsigned char *uuid) {
109 static char uuidstring[UUID_STRINGSIZE + 1];
114 while (i < UUID_STRINGSIZE) {
117 sprintf(uuidstring + i, "%02X", c);
119 if (i==8 || i==13 || i==18 || i==23)
120 uuidstring[i++] = '-';
126 /********************************************************
128 ********************************************************/
131 * name: give me his name
132 * type: and type (UUID_USER or UUID_GROUP)
133 * uuid: pointer to uuid_t storage that the caller must provide
134 * returns 0 on success !=0 on errror
136 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
138 uuidtype_t mytype = type;
139 char nulluuid[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
141 char *uuid_string = NULL;
144 ret = search_cachebyname(name, &mytype, uuid);
148 LOG(log_debug, logtype_afpd,
149 "getuuidfromname{cache}: name: %s, type%s: %s -> UUID: %s",
151 (mytype & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
152 uuidtype[type & UUIDTYPESTR_MASK],
153 uuid_bin2string(uuid));
154 if ((mytype & UUID_ENOENT) == UUID_ENOENT)
157 /* if not found in cache */
159 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
160 uuid_string2bin( uuid_string, uuid);
161 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",
162 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
164 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
169 /* Build a local UUID */
170 if (type == UUID_USER) {
172 if ((pwd = getpwnam(name)) == NULL) {
173 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
174 name, uuidtype[type & UUIDTYPESTR_MASK]);
175 mytype |= UUID_ENOENT;
176 memcpy(uuid, nulluuid, 16);
178 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
180 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
181 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
185 if ((grp = getgrnam(name)) == NULL) {
186 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
187 name, uuidtype[type & UUIDTYPESTR_MASK]);
188 mytype |= UUID_ENOENT;
189 memcpy(uuid, nulluuid, 16);
191 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
193 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
194 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
198 add_cachebyname(name, uuid, mytype, 0);
203 if (uuid_string) free(uuid_string);
210 * uuidp: pointer to a uuid
211 * name: returns allocated buffer from ldap_getnamefromuuid
212 * type: returns USER, GROUP or LOCAL
213 * return 0 on success !=0 on errror
215 * Caller must free name appropiately.
217 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
224 if (search_cachebyuuid(uuidp, name, type) == 0) {
226 LOG(log_debug, logtype_afpd,
227 "getnamefromuuid{cache}: UUID: %s -> name: %s, type%s: %s",
228 uuid_bin2string(uuidp),
230 (*type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
231 uuidtype[(*type) & UUIDTYPESTR_MASK]);
232 if ((*type & UUID_ENOENT) == UUID_ENOENT)
237 /* not found in cache */
239 /* Check if UUID is a client local one */
240 if (memcmp(uuidp, local_user_uuid, 12) == 0) {
242 uid = ntohl(*(uint32_t *)(uuidp + 12));
243 if ((pwd = getpwuid(uid)) == NULL) {
244 /* not found, add negative entry to cache */
245 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
248 *name = strdup(pwd->pw_name);
249 add_cachebyuuid(uuidp, *name, *type, 0);
252 LOG(log_debug, logtype_afpd,
253 "getnamefromuuid{local}: UUID: %s -> name: %s, type:%s",
254 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
256 } else if (memcmp(uuidp, local_group_uuid, 12) == 0) {
258 gid = ntohl(*(uint32_t *)(uuidp + 12));
259 if ((grp = getgrgid(gid)) == NULL) {
260 /* not found, add negative entry to cache */
261 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
264 *name = strdup(grp->gr_name);
265 add_cachebyuuid(uuidp, *name, *type, 0);
272 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
274 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
275 uuid_bin2string(uuidp));
276 add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
281 add_cachebyuuid(uuidp, *name, *type, 0);
283 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
284 uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);