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 ********************************************************/
39 * convert ascii string that can include dashes to binary uuid.
40 * caller must provide a buffer.
42 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
45 unsigned char c, val = 0;
53 else if (c <= '9') /* 0-9 */
55 else if (c <= 'F') /* A-F */
57 else if (c <= 'f') /* a-f */
72 * convert 16 byte binary uuid to neat ascii represantation including dashes
73 * string is allocated and pointer returned. caller must freee.
75 int uuid_bin2string( uuidp_t uuid, char **uuidstring) {
76 char ascii[16] = { "0123456789ABCDEF" };
82 *uuidstring = calloc(1, UUID_STRINGSIZE + 1);
83 if (*uuidstring == NULL) {
84 LOG(log_error, logtype_default, "uuid_bin2string: %s: error calloc'ing",strerror(errno));
89 while (i < UUID_STRINGSIZE) {
100 if (i==8 || i==13 || i==18 || i==23)
106 /********************************************************
108 ********************************************************/
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) {
118 char *uuid_string = NULL;
120 ret = search_cachebyname( name, type, uuid);
121 if (ret == 0) { /* found in cache */
123 uuid_bin2string( uuid, &uuid_string);
124 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
125 name, uuidtype[type], uuid_string);
127 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s",
128 name, uuidtype[type]);
130 } else { /* if not found in cache */
131 ret = ldap_getuuidfromname( name, type, &uuid_string);
133 LOG(log_info, logtype_afpd, "getuuidfromname: no result from ldap_getuuidfromname");
136 uuid_string2bin( uuid_string, uuid);
137 add_cachebyname( name, uuid, type, 0);
138 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",name, uuidtype[type], uuid_string);
147 * uuidp: pointer to a uuid
148 * name: returns allocated buffer from ldap_getnamefromuuid
149 * type: returns USER or GROUP
150 * return 0 on success !=0 on errror
152 * Caller must free name appropiately.
154 int getnamefromuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
156 char *uuid_string = NULL;
158 ret = search_cachebyuuid( uuidp, name, type);
159 if (ret == 0) { /* found in cache */
161 uuid_bin2string( uuidp, &uuid_string);
162 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
163 uuid_string, *name, uuidtype[*type]);
167 } else { /* if not found in cache */
168 uuid_bin2string( uuidp, &uuid_string);
169 ret = ldap_getnamefromuuid( uuid_string, name, type);
171 LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
175 add_cachebyuuid( uuidp, *name, *type, 0);
176 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
177 uuid_string, *name, uuidtype[*type]);