]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/uuid.c
Merge master
[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 #include <arpa/inet.h>
28
29 #include <atalk/logger.h>
30 #include <atalk/afp.h>
31 #include <atalk/uuid.h>
32 #include <atalk/util.h>
33
34 #include "aclldap.h"
35 #include "cache.h"
36
37 char *uuidtype[] = {"USER", "GROUP", "LOCAL"};
38
39 /********************************************************
40  * Public helper function
41  ********************************************************/
42
43 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
44                                            0xab, 0xcd, 0xef,
45                                            0xab, 0xcd, 0xef,
46                                            0xab, 0xcd, 0xef};
47
48 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
49                                           0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
50
51 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
52 {
53     uint32_t tmp;
54
55     switch (type) {
56     case UUID_GROUP:
57         memcpy(buf, local_group_uuid, 12);
58         break;
59     case UUID_USER:
60     default:
61         memcpy(buf, local_user_uuid, 12);
62         break;
63     }
64
65     tmp = htonl(id);
66     memcpy(buf + 12, &tmp, 4);
67
68     return;
69 }
70
71 /*
72  * convert ascii string that can include dashes to binary uuid.
73  * caller must provide a buffer.
74  */
75 void uuid_string2bin( const char *uuidstring, unsigned char *uuid) {
76     int nibble = 1;
77     int i = 0;
78     unsigned char c, val = 0;
79
80     while (*uuidstring) {
81         c = *uuidstring;
82         if (c == '-') {
83             uuidstring++;
84             continue;
85         }
86         else if (c <= '9')      /* 0-9 */
87             c -= '0';
88         else if (c <= 'F')  /* A-F */
89             c -= 'A' - 10;
90         else if (c <= 'f')      /* a-f */
91             c-= 'a' - 10;
92
93         if (nibble)
94             val = c * 16;
95         else
96             uuid[i++] = val + c;
97
98         nibble ^= 1;
99         uuidstring++;
100     }
101
102 }
103
104 /*!
105  * Convert 16 byte binary uuid to neat ascii represantation including dashes.
106  *
107  * Returns pointer to static buffer.
108  */
109 const char *uuid_bin2string(const unsigned char *uuid) {
110     static char uuidstring[UUID_STRINGSIZE + 1];
111
112     int i = 0;
113     unsigned char c;
114
115     while (i < UUID_STRINGSIZE) {
116         c = *uuid;
117         uuid++;
118         sprintf(uuidstring + i, "%02X", c);
119         i += 2;
120         if (i==8 || i==13 || i==18 || i==23)
121             uuidstring[i++] = '-';
122     }
123     uuidstring[i] = 0;
124     return uuidstring;
125 }
126
127 /********************************************************
128  * Interface
129  ********************************************************/
130
131 /*
132  *   name: give me his name
133  *   type: and type (UUID_USER or UUID_GROUP)
134  *   uuid: pointer to uuid_t storage that the caller must provide
135  * returns 0 on success !=0 on errror
136  */
137 int getuuidfromname( const char *name, uuidtype_t type, unsigned char *uuid) {
138     int ret = 0;
139     uuidtype_t mytype = type;
140     char nulluuid[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
141 #ifdef HAVE_LDAP
142     char *uuid_string = NULL;
143 #endif
144
145     ret = search_cachebyname(name, &mytype, uuid);
146
147     if (ret == 0) {
148         /* found in cache */
149         LOG(log_debug, logtype_afpd,
150             "getuuidfromname{cache}: name: %s, type%s: %s -> UUID: %s",
151             name,
152             (mytype & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
153             uuidtype[type & UUIDTYPESTR_MASK],
154             uuid_bin2string(uuid));
155         if ((mytype & UUID_ENOENT) == UUID_ENOENT)
156             return -1;
157     } else  {
158         /* if not found in cache */
159 #ifdef HAVE_LDAP
160         if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
161             uuid_string2bin( uuid_string, uuid);
162             LOG(log_debug, logtype_afpd, "getuuidfromname{LDAP}: name: %s, type: %s -> UUID: %s",
163                 name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
164         } else {
165             LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
166                 name, type);
167         }
168 #endif
169         if (ret != 0) {
170             /* Build a local UUID */
171             if (type == UUID_USER) {
172                 struct passwd *pwd;
173                 if ((pwd = getpwnam(name)) == NULL) {
174                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
175                         name, uuidtype[type & UUIDTYPESTR_MASK]);
176                     mytype |= UUID_ENOENT;
177                     memcpy(uuid, nulluuid, 16);
178                 } else {
179                     localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
180                     ret = 0;
181                     LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
182                         name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
183                 }
184             } else {
185                 struct group *grp;
186                 if ((grp = getgrnam(name)) == NULL) {
187                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
188                         name, uuidtype[type & UUIDTYPESTR_MASK]);
189                     mytype |= UUID_ENOENT;
190                     memcpy(uuid, nulluuid, 16);
191                 } else {
192                     localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
193                     ret = 0;
194                     LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
195                         name, uuidtype[type & UUIDTYPESTR_MASK], uuid_bin2string(uuid));
196                 }
197             }
198         }
199         add_cachebyname(name, uuid, mytype, 0);
200     }
201
202 cleanup:
203 #ifdef HAVE_LDAP
204     if (uuid_string) free(uuid_string);
205 #endif
206     return ret;
207 }
208
209
210 /*
211  * uuidp: pointer to a uuid
212  * name: returns allocated buffer from ldap_getnamefromuuid
213  * type: returns USER, GROUP or LOCAL
214  * return 0 on success !=0 on errror
215  *
216  * Caller must free name appropiately.
217  */
218 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
219     int ret = 0;
220     uid_t uid;
221     gid_t gid;
222     struct passwd *pwd;
223     struct group *grp;
224
225     if (search_cachebyuuid(uuidp, name, type) == 0) {
226         /* found in cache */
227         LOG(log_debug, logtype_afpd,
228             "getnamefromuuid{cache}: UUID: %s -> name: %s, type%s: %s",
229             uuid_bin2string(uuidp),
230             *name,
231             (*type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
232             uuidtype[(*type) & UUIDTYPESTR_MASK]);
233         if ((*type & UUID_ENOENT) == UUID_ENOENT)
234             return -1;
235         return 0;
236     }
237
238     /* not found in cache */
239
240     /* Check if UUID is a client local one */
241     if (memcmp(uuidp, local_user_uuid, 12) == 0) {
242         *type = UUID_USER;
243         uid = ntohl(*(uint32_t *)(uuidp + 12));
244         if ((pwd = getpwuid(uid)) == NULL) {
245             /* not found, add negative entry to cache */
246             add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
247             ret = -1;
248         } else {
249             *name = strdup(pwd->pw_name);
250             add_cachebyuuid(uuidp, *name, *type, 0);
251             ret = 0;
252         }
253         LOG(log_debug, logtype_afpd,
254             "getnamefromuuid{local}: UUID: %s -> name: %s, type:%s",
255             uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
256         return ret;
257     } else if (memcmp(uuidp, local_group_uuid, 12) == 0) {
258         *type = UUID_GROUP;
259         gid = ntohl(*(uint32_t *)(uuidp + 12));
260         if ((grp = getgrgid(gid)) == NULL) {
261             /* not found, add negative entry to cache */
262             add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
263             ret = -1;
264         } else {
265             *name = strdup(grp->gr_name);
266             add_cachebyuuid(uuidp, *name, *type, 0);
267             ret = 0;
268         }
269         return ret;
270     }
271
272 #ifdef HAVE_LDAP
273     ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
274     if (ret != 0) {
275         LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
276             uuid_bin2string(uuidp));
277         add_cachebyuuid(uuidp, "UUID_ENOENT", UUID_ENOENT, 0);
278         return -1;
279     }
280 #endif
281
282     add_cachebyuuid(uuidp, *name, *type, 0);
283
284     LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
285         uuid_bin2string(uuidp), *name, uuidtype[(*type) & UUIDTYPESTR_MASK]);
286
287     return 0;
288 }