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 ********************************************************/
43 * convert ascii string that can include dashes to binary uuid.
44 * caller must provide a buffer.
46 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
49 unsigned char c, val = 0;
57 else if (c <= '9') /* 0-9 */
59 else if (c <= 'F') /* A-F */
61 else if (c <= 'f') /* a-f */
76 * Convert 16 byte binary uuid to neat ascii represantation including dashes.
78 * Returns pointer to static buffer.
80 const char *uuid_bin2string(unsigned char *uuid) {
81 static char uuidstring[UUID_STRINGSIZE + 1];
86 while (i < UUID_STRINGSIZE) {
89 sprintf(uuidstring + i, "%02X", c);
91 if (i==8 || i==13 || i==18 || i==23)
92 uuidstring[i++] = '-';
98 /********************************************************
100 ********************************************************/
102 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
107 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
108 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
111 * name: give me his name
112 * type: and type (UUID_USER or UUID_GROUP)
113 * uuid: pointer to uuid_t storage that the caller must provide
114 * returns 0 on success !=0 on errror
116 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
119 char *uuid_string = NULL;
121 ret = search_cachebyname( name, type, uuid);
124 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
125 name, uuidtype[type], uuid_bin2string(uuid));
127 /* if not found in cache */
129 if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
130 uuid_string2bin( uuid_string, uuid);
131 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
132 name, uuidtype[type], uuid_bin2string(uuid));
134 LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
139 /* Build a local UUID */
140 if (type == UUID_USER) {
141 memcpy(uuid, local_user_uuid, 12);
143 if ((pwd = getpwnam(name)) == NULL) {
144 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
145 name, uuidtype[type]);
148 uint32_t id = pwd->pw_uid;
150 memcpy(uuid + 12, &id, 4);
152 memcpy(uuid, &local_group_uuid, 12);
154 if ((grp = getgrnam(name)) == NULL) {
155 LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
156 name, uuidtype[type]);
159 uint32_t id = grp->gr_gid;
161 memcpy(uuid + 12, &id, 4);
163 LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
164 name, uuidtype[type], uuid_bin2string(uuid));
167 add_cachebyname( name, uuid, type, 0);
172 if (uuid_string) free(uuid_string);
179 * uuidp: pointer to a uuid
180 * name: returns allocated buffer from ldap_getnamefromuuid
181 * type: returns USER, GROUP or LOCAL
182 * return 0 on success !=0 on errror
184 * Caller must free name appropiately.
186 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
189 ret = search_cachebyuuid( uuidp, name, type);
192 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
193 uuid_bin2string(uuidp), *name, uuidtype[*type]);
195 /* not found in cache */
197 /* Check if UUID is a client local one */
198 if (memcmp(uuidp, local_user_uuid, 12) == 0
199 || memcmp(uuidp, local_group_uuid, 12) == 0) {
200 LOG(log_debug, logtype_afpd, "getnamefromuuid: local UUID: %" PRIu32 "",
201 ntohl(*(uint32_t *)(uuidp + 12)));
203 *name = strdup("UUID_LOCAL");
208 ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
210 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
211 uuid_bin2string(uuidp));
214 add_cachebyuuid( uuidp, *name, *type, 0);
215 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
216 uuid_bin2string(uuidp), *name, uuidtype[*type]);