2 $Id: uuid.c,v 1.2 2009-10-29 13:38:15 didg Exp $
3 Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 #endif /* HAVE_CONFIG_H */
25 #include <atalk/logger.h>
26 #include <atalk/afp.h>
27 #include <atalk/uuid.h>
32 char *uuidtype[] = {"NULL","USER", "GROUP"};
34 /********************************************************
35 * Public helper function
36 ********************************************************/
38 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
41 unsigned char c, val = 0;
49 else if (c <= '9') /* 0-9 */
51 else if (c <= 'F') /* A-F */
53 else if (c <= 'f') /* a-f */
67 int uuid_bin2string( uuidp_t uuid, char **uuidstring) {
68 char ascii[16] = { "0123456789abcdef" };
74 *uuidstring = calloc(1, UUID_STRINGSIZE + 1);
75 if (*uuidstring == NULL) {
76 LOG(log_error, logtype_default, "uuid_bin2string: %s: error calloc'ing",strerror(errno));
81 while (i < UUID_STRINGSIZE) {
92 if (i==8 || i==13 || i==18 || i==23)
98 /********************************************************
100 ********************************************************/
102 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
104 char *uuid_string = NULL;
106 ret = search_cachebyname( name, type, uuid);
107 if (ret == 0) { /* found in cache */
108 uuid_bin2string( uuid, &uuid_string);
109 LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",name, uuidtype[type], uuid_string);
110 } else { /* if not found in cache */
111 ret = ldap_getuuidfromname( name, type, &uuid_string);
113 LOG(log_error, logtype_afpd, "getuuidfromname: no result from ldap_getuuidfromname");
116 uuid_string2bin( uuid_string, uuid);
117 add_cachebyname( name, uuid, type, 0);
118 LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",name, uuidtype[type], uuid_string);
126 int getnamefromuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
128 char *uuid_string = NULL;
130 ret = search_cachebyuuid( uuidp, name, type);
131 if (ret == 0) { /* found in cache */
133 uuid_bin2string( uuidp, &uuid_string);
134 LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s", uuid_string, *name, uuidtype[*type]);
136 } else { /* if not found in cache */
137 uuid_bin2string( uuidp, &uuid_string);
138 ret = ldap_getnamefromuuid( uuid_string, name, type);
140 LOG(log_error, logtype_afpd, "getnamefromuuid: no result from ldap_getuuidfromname");
143 add_cachebyuuid( uuidp, *name, *type, 0);
144 LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",uuid_string, *name, uuidtype[*type]);