]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/uuid.c
use log_debug9 rather than log_debug for LOG inside ifdef DEBUG
[netatalk.git] / libatalk / acl / uuid.c
1 /*
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>
4
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.
9  
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.
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <atalk/logger.h>
26 #include <atalk/afp.h>
27 #include <atalk/uuid.h>
28
29 #include "aclldap.h"
30 #include "cache.h"
31
32 char *uuidtype[] = {"NULL","USER", "GROUP"};
33
34 /********************************************************
35  * Public helper function
36  ********************************************************/
37
38 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
39     int nibble = 1;
40     int i = 0;
41     unsigned char c, val = 0;
42
43     while (*uuidstring) {
44         c = *uuidstring;
45         if (c == '-') {
46             uuidstring++;
47             continue;
48         }
49         else if (c <= '9')              /* 0-9 */
50             c -= '0';
51         else if (c <= 'F')      /* A-F */
52             c -= 'A' - 10;
53         else if (c <= 'f')      /* a-f */
54             c-= 'a' - 10;
55
56         if (nibble)
57             val = c * 16;
58         else
59             uuid[i++] = val + c;
60
61         nibble ^= 1;
62         uuidstring++;
63     }
64
65 }
66
67 int uuid_bin2string( uuidp_t uuid, char **uuidstring) {
68     char ascii[16] = { "0123456789abcdef" };
69     int nibble = 1;
70     int i = 0;
71     unsigned char c;
72     char *s;
73
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));
77         return -1;
78     }
79     s = *uuidstring;
80
81     while (i < UUID_STRINGSIZE) {
82         c = *uuid;
83         if (nibble)
84             c = c >> 4;
85         else {
86             c &= 0x0f;
87             uuid++;
88         }
89         s[i] = ascii[c];
90         nibble ^= 1;
91         i++;
92         if (i==8 || i==13 || i==18 || i==23)
93             s[i++] = '-';
94     }
95     return 0;
96 }
97
98 /******************************************************** 
99  * Interface
100  ********************************************************/
101
102 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
103     int ret = 0;
104     char *uuid_string = NULL;
105
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);
112         if (ret != 0) {
113             LOG(log_error, logtype_afpd, "getuuidfromname: no result from ldap_getuuidfromname");
114             goto cleanup;
115         }
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);
119     }
120
121 cleanup:
122     free(uuid_string);
123     return ret;
124 }
125
126 int getnamefromuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
127     int ret;
128     char *uuid_string = NULL;
129     
130     ret = search_cachebyuuid( uuidp, name, type);
131     if (ret == 0) {             /* found in cache */
132 #ifdef DEBUG
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]);
135 #endif
136     } else  {                   /* if not found in cache */
137         uuid_bin2string( uuidp, &uuid_string);
138         ret = ldap_getnamefromuuid( uuid_string, name, type);
139         if (ret != 0) {
140             LOG(log_error, logtype_afpd, "getnamefromuuid: no result from ldap_getuuidfromname");
141             goto cleanup;
142         }
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]);
145     }
146
147 cleanup:
148     free(uuid_string);
149     return ret;
150 }