]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/uuid.c
8993acc21f61fa38bc0c58dda11bd720eebc0cf4
[netatalk.git] / libatalk / acl / uuid.c
1 /*
2   Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <grp.h>
27
28 #include <atalk/logger.h>
29 #include <atalk/afp.h>
30 #include <atalk/uuid.h>
31 #include <atalk/util.h>
32
33 #include "aclldap.h"
34 #include "cache.h"
35
36 char *uuidtype[] = {"NULL","USER", "GROUP", "LOCAL"};
37
38 /********************************************************
39  * Public helper function
40  ********************************************************/
41
42 /* 
43  * convert ascii string that can include dashes to binary uuid.
44  * caller must provide a buffer.
45  */
46 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
47     int nibble = 1;
48     int i = 0;
49     unsigned char c, val = 0;
50
51     while (*uuidstring) {
52         c = *uuidstring;
53         if (c == '-') {
54             uuidstring++;
55             continue;
56         }
57         else if (c <= '9')      /* 0-9 */
58             c -= '0';
59         else if (c <= 'F')  /* A-F */
60             c -= 'A' - 10;
61         else if (c <= 'f')      /* a-f */
62             c-= 'a' - 10;
63
64         if (nibble)
65             val = c * 16;
66         else
67             uuid[i++] = val + c;
68
69         nibble ^= 1;
70         uuidstring++;
71     }
72
73 }
74
75 /*! 
76  * Convert 16 byte binary uuid to neat ascii represantation including dashes.
77  * 
78  * Returns pointer to static buffer.
79  */
80 const char *uuid_bin2string(unsigned char *uuid) {
81     static char uuidstring[UUID_STRINGSIZE + 1];
82
83     int i = 0;
84     unsigned char c;
85
86     while (i < UUID_STRINGSIZE) {
87         c = *uuid;
88         uuid++;
89         sprintf(uuidstring + i, "%02X", c);
90         i += 2;
91         if (i==8 || i==13 || i==18 || i==23)
92             uuidstring[i++] = '-';
93     }
94     uuidstring[i] = 0;
95     return uuidstring;
96 }
97
98 /********************************************************
99  * Interface
100  ********************************************************/
101
102 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
103                                            0xab, 0xcd, 0xef,
104                                            0xab, 0xcd, 0xef, 
105                                            0xab, 0xcd, 0xef};
106
107 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
108                                           0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
109
110 /*
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
115  */  
116 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
117     int ret = 0;
118 #ifdef HAVE_LDAP
119     char *uuid_string = NULL;
120 #endif
121     ret = search_cachebyname( name, type, uuid);
122     if (ret == 0) {
123         /* found in cache */
124         LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
125             name, uuidtype[type], uuid_bin2string(uuid));
126     } else  {
127         /* if not found in cache */
128 #ifdef HAVE_LDAP
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));
133         } else {
134             LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
135                 name, type);
136         }
137 #endif
138         if (ret != 0) {
139             /* Build a local UUID */
140             if (type == UUID_USER) {
141                 memcpy(uuid, local_user_uuid, 12);
142                 struct passwd *pwd;
143                 if ((pwd = getpwnam(name)) == NULL) {
144                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
145                         name, uuidtype[type]);
146                     goto cleanup;
147                 }
148                 uint32_t id = pwd->pw_uid;
149                 id = htonl(id);
150                 memcpy(uuid + 12, &id, 4);
151             } else {
152                 memcpy(uuid, &local_group_uuid, 12);
153                 struct group *grp;
154                 if ((grp = getgrnam(name)) == NULL) {
155                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
156                         name, uuidtype[type]);
157                     goto cleanup;
158                 }
159                 uint32_t id = grp->gr_gid;
160                 id = htonl(id);
161                 memcpy(uuid + 12, &id, 4);
162             }
163             LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
164                 name, uuidtype[type], uuid_bin2string(uuid));
165         }
166         ret = 0;
167         add_cachebyname( name, uuid, type, 0);
168     }
169
170 cleanup:
171 #ifdef HAVE_LDAP
172     if (uuid_string) free(uuid_string);
173 #endif
174     return ret;
175 }
176
177
178 /*
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
183  *
184  * Caller must free name appropiately.
185  */
186 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
187     int ret;
188
189     ret = search_cachebyuuid( uuidp, name, type);
190     if (ret == 0) {
191         /* found in cache */
192         LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
193             uuid_bin2string(uuidp), *name, uuidtype[*type]);
194     } else {
195         /* not found in cache */
196
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)));
202             *type = UUID_LOCAL;
203             *name = strdup("UUID_LOCAL");
204             return 0;
205         }
206
207 #ifdef HAVE_LDAP
208         ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
209         if (ret != 0) {
210             LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
211                 uuid_bin2string(uuidp));
212             goto cleanup;
213         }
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]);
217 #endif
218     }
219
220 cleanup:
221     return ret;
222 }