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/util.h>
37 char *uuidtype[] = {"NULL","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, unsigned char *uuid) {
78 unsigned char c, val = 0;
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.
107 * Returns pointer to static buffer.
109 const char *uuid_bin2string(const unsigned char *uuid) {
110 static char uuidstring[UUID_STRINGSIZE + 1];
115 while (i < UUID_STRINGSIZE) {
118 sprintf(uuidstring + i, "%02X", c);
120 if (i==8 || i==13 || i==18 || i==23)
121 uuidstring[i++] = '-';
127 /********************************************************
129 ********************************************************/
132 * name: give me his name
133 * type: and type (UUID_USER or UUID_GROUP)
134 * uuid: pointer to uuid_t storage that the caller must provide
135 * returns 0 on success !=0 on errror
137 int getuuidfromname( const char *name, uuidtype_t type, unsigned char *uuid) {
140 char *uuid_string = NULL;
142 ret = search_cachebyname( name, type, uuid);
145 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
146 name, uuidtype[type], uuid_bin2string(uuid));
148 /* if not found in cache */
150 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
151 uuid_string2bin( uuid_string, uuid);
152 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
153 name, uuidtype[type], uuid_bin2string(uuid));
155 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
160 /* Build a local UUID */
161 if (type == UUID_USER) {
163 if ((pwd = getpwnam(name)) == NULL) {
164 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
165 name, uuidtype[type]);
168 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
171 if ((grp = getgrnam(name)) == NULL) {
172 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
173 name, uuidtype[type]);
176 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
178 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
179 name, uuidtype[type], uuid_bin2string(uuid));
182 add_cachebyname( name, uuid, type, 0);
187 if (uuid_string) free(uuid_string);
194 * uuidp: pointer to a uuid
195 * name: returns allocated buffer from ldap_getnamefromuuid
196 * type: returns USER, GROUP or LOCAL
197 * return 0 on success !=0 on errror
199 * Caller must free name appropiately.
201 int getnamefromuuid(uuidp_t uuidp, char **name, uuidtype_t *type) {
204 ret = search_cachebyuuid( uuidp, name, type);
207 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
208 uuid_bin2string(uuidp), *name, uuidtype[*type]);
210 /* not found in cache */
212 /* Check if UUID is a client local one */
213 if (memcmp(uuidp, local_user_uuid, 12) == 0
214 || memcmp(uuidp, local_group_uuid, 12) == 0) {
215 LOG(log_debug, logtype_afpd, "getnamefromuuid: local UUID: %" PRIu32 "",
216 ntohl(*(uint32_t *)(uuidp + 12)));
218 *name = strdup("UUID_LOCAL");
223 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
225 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
226 uuid_bin2string(uuidp));
229 add_cachebyuuid( uuidp, *name, *type, 0);
230 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
231 uuid_bin2string(uuidp), *name, uuidtype[*type]);