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[] = {"NULL","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) {
139 char *uuid_string = NULL;
141 ret = search_cachebyname( name, type, uuid);
144 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
145 name, uuidtype[type], uuid_bin2string(uuid));
147 /* if not found in cache */
149 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
150 uuid_string2bin( uuid_string, uuid);
151 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
152 name, uuidtype[type], uuid_bin2string(uuid));
154 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
159 /* Build a local UUID */
160 if (type == UUID_USER) {
162 if ((pwd = getpwnam(name)) == NULL) {
163 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
164 name, uuidtype[type]);
167 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
170 if ((grp = getgrnam(name)) == NULL) {
171 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
172 name, uuidtype[type]);
175 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
177 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
178 name, uuidtype[type], uuid_bin2string(uuid));
181 add_cachebyname( name, uuid, type, 0);
186 if (uuid_string) free(uuid_string);
193 * uuidp: pointer to a uuid
194 * name: returns allocated buffer from ldap_getnamefromuuid
195 * type: returns USER, GROUP or LOCAL
196 * return 0 on success !=0 on errror
198 * Caller must free name appropiately.
200 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
203 ret = search_cachebyuuid( uuidp, name, type);
206 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
207 uuid_bin2string(uuidp), *name, uuidtype[*type]);
209 /* not found in cache */
211 /* Check if UUID is a client local one */
212 if (memcmp(uuidp, local_user_uuid, 12) == 0
213 || memcmp(uuidp, local_group_uuid, 12) == 0) {
214 LOG(log_debug, logtype_afpd, "getnamefromuuid: local UUID: %" PRIu32 "",
215 ntohl(*(uint32_t *)(uuidp + 12)));
217 *name = strdup("UUID_LOCAL");
222 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
224 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
225 uuid_bin2string(uuidp));
228 add_cachebyuuid( uuidp, *name, *type, 0);
229 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
230 uuid_bin2string(uuidp), *name, uuidtype[*type]);