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 <atalk/logger.h>
25 #include <atalk/afp.h>
26 #include <atalk/uuid.h>
27 #include <atalk/util.h>
32 char *uuidtype[] = {"NULL","USER", "GROUP"};
34 /********************************************************
35 * Public helper function
36 ********************************************************/
38 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
43 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
44 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
46 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
52 memcpy(buf, local_group_uuid, 12);
56 memcpy(buf, local_user_uuid, 12);
61 memcpy(buf + 12, &tmp, 4);
67 * convert ascii string that can include dashes to binary uuid.
68 * caller must provide a buffer.
70 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
73 unsigned char c, val = 0;
81 else if (c <= '9') /* 0-9 */
83 else if (c <= 'F') /* A-F */
85 else if (c <= 'f') /* a-f */
100 * convert 16 byte binary uuid to neat ascii represantation including dashes
101 * string is allocated and pointer returned. caller must freee.
103 int uuid_bin2string( uuidp_t uuid, char **uuidstring) {
104 char ascii[16] = { "0123456789ABCDEF" };
110 *uuidstring = calloc(1, UUID_STRINGSIZE + 1);
111 if (*uuidstring == NULL) {
112 LOG(log_error, logtype_default, "uuid_bin2string: %s: error calloc'ing",strerror(errno));
117 while (i < UUID_STRINGSIZE) {
128 if (i==8 || i==13 || i==18 || i==23)
134 /********************************************************
136 ********************************************************/
139 * name: give me his name
140 * type: and type (UUID_USER or UUID_GROUP)
141 * uuid: pointer to uuid_t storage that the caller must provide
142 * returns 0 on success !=0 on errror
144 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
146 char *uuid_string = NULL;
148 ret = search_cachebyname( name, type, uuid);
149 if (ret == 0) { /* found in cache */
151 uuid_bin2string( uuid, &uuid_string);
152 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
153 name, uuidtype[type], uuid_string);
155 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s",
156 name, uuidtype[type]);
158 } else { /* if not found in cache */
159 ret = ldap_getuuidfromname( name, type, &uuid_string);
161 LOG(log_info, logtype_afpd, "getuuidfromname: no result from ldap_getuuidfromname");
164 uuid_string2bin( uuid_string, uuid);
165 add_cachebyname( name, uuid, type, 0);
166 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",name, uuidtype[type], uuid_string);
175 * uuidp: pointer to a uuid
176 * name: returns allocated buffer from ldap_getnamefromuuid
177 * type: returns USER or GROUP
178 * return 0 on success !=0 on errror
180 * Caller must free name appropiately.
182 int getnamefromuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
184 char *uuid_string = NULL;
186 ret = search_cachebyuuid( uuidp, name, type);
187 if (ret == 0) { /* found in cache */
189 uuid_bin2string( uuidp, &uuid_string);
190 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
191 uuid_string, *name, uuidtype[*type]);
195 } else { /* if not found in cache */
196 uuid_bin2string( uuidp, &uuid_string);
197 ret = ldap_getnamefromuuid( uuid_string, name, type);
199 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
203 add_cachebyuuid( uuidp, *name, *type, 0);
204 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
205 uuid_string, *name, uuidtype[*type]);