]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/uuid.c
Use function from 2-1 for local uuid generation
[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 static unsigned char local_group_uuid[] = {0xab, 0xcd, 0xef,
43                                            0xab, 0xcd, 0xef,
44                                            0xab, 0xcd, 0xef, 
45                                            0xab, 0xcd, 0xef};
46
47 static unsigned char local_user_uuid[] = {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd,
48                                           0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa};
49
50 void localuuid_from_id(unsigned char *buf, uuidtype_t type, unsigned int id)
51 {
52     uint32_t tmp;
53
54     switch (type) {
55     case UUID_GROUP:
56         memcpy(buf, local_group_uuid, 12);
57         break;
58     case UUID_USER:
59     default:
60         memcpy(buf, local_user_uuid, 12);
61         break;
62     }
63
64     tmp = htonl(id);
65     memcpy(buf + 12, &tmp, 4);
66
67     return;
68 }
69
70 /* 
71  * convert ascii string that can include dashes to binary uuid.
72  * caller must provide a buffer.
73  */
74 void uuid_string2bin( const char *uuidstring, uuidp_t uuid) {
75     int nibble = 1;
76     int i = 0;
77     unsigned char c, val = 0;
78
79     while (*uuidstring) {
80         c = *uuidstring;
81         if (c == '-') {
82             uuidstring++;
83             continue;
84         }
85         else if (c <= '9')      /* 0-9 */
86             c -= '0';
87         else if (c <= 'F')  /* A-F */
88             c -= 'A' - 10;
89         else if (c <= 'f')      /* a-f */
90             c-= 'a' - 10;
91
92         if (nibble)
93             val = c * 16;
94         else
95             uuid[i++] = val + c;
96
97         nibble ^= 1;
98         uuidstring++;
99     }
100
101 }
102
103 /*! 
104  * Convert 16 byte binary uuid to neat ascii represantation including dashes.
105  * 
106  * Returns pointer to static buffer.
107  */
108 const char *uuid_bin2string(unsigned char *uuid) {
109     static char uuidstring[UUID_STRINGSIZE + 1];
110
111     int i = 0;
112     unsigned char c;
113
114     while (i < UUID_STRINGSIZE) {
115         c = *uuid;
116         uuid++;
117         sprintf(uuidstring + i, "%02X", c);
118         i += 2;
119         if (i==8 || i==13 || i==18 || i==23)
120             uuidstring[i++] = '-';
121     }
122     uuidstring[i] = 0;
123     return uuidstring;
124 }
125
126 /********************************************************
127  * Interface
128  ********************************************************/
129
130 /*
131  *   name: give me his name
132  *   type: and type (UUID_USER or UUID_GROUP)
133  *   uuid: pointer to uuid_t storage that the caller must provide
134  * returns 0 on success !=0 on errror
135  */  
136 int getuuidfromname( const char *name, uuidtype_t type, uuidp_t uuid) {
137     int ret = 0;
138 #ifdef HAVE_LDAP
139     char *uuid_string = NULL;
140 #endif
141     ret = search_cachebyname( name, type, uuid);
142     if (ret == 0) {
143         /* found in cache */
144         LOG(log_debug, logtype_afpd, "getuuidfromname{cache}: name: %s, type: %s -> UUID: %s",
145             name, uuidtype[type], uuid_bin2string(uuid));
146     } else  {
147         /* if not found in cache */
148 #ifdef HAVE_LDAP
149         if ((ret = ldap_getuuidfromname( name, type, &uuid_string)) == 0) {
150             uuid_string2bin( uuid_string, uuid);
151             LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
152                 name, uuidtype[type], uuid_bin2string(uuid));
153         } else {
154             LOG(log_debug, logtype_afpd, "getuuidfromname(\"%s\",t:%u): no result from ldap search",
155                 name, type);
156         }
157 #endif
158         if (ret != 0) {
159             /* Build a local UUID */
160             if (type == UUID_USER) {
161                 struct passwd *pwd;
162                 if ((pwd = getpwnam(name)) == NULL) {
163                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
164                         name, uuidtype[type]);
165                     goto cleanup;
166                 }
167                 localuuid_from_id(uuid, UUID_USER, pwd->pw_uid);
168             } else {
169                 struct group *grp;
170                 if ((grp = getgrnam(name)) == NULL) {
171                     LOG(log_error, logtype_afpd, "getuuidfromname(\"%s\",t:%u): unknown user",
172                         name, uuidtype[type]);
173                     goto cleanup;
174                 }
175                 localuuid_from_id(uuid, UUID_GROUP, grp->gr_gid);
176             }
177             LOG(log_debug, logtype_afpd, "getuuidfromname{local}: name: %s, type: %s -> UUID: %s",
178                 name, uuidtype[type], uuid_bin2string(uuid));
179         }
180         ret = 0;
181         add_cachebyname( name, uuid, type, 0);
182     }
183
184 cleanup:
185 #ifdef HAVE_LDAP
186     if (uuid_string) free(uuid_string);
187 #endif
188     return ret;
189 }
190
191
192 /*
193  * uuidp: pointer to a uuid
194  * name: returns allocated buffer from ldap_getnamefromuuid
195  * type: returns USER, GROUP or LOCAL
196  * return 0 on success !=0 on errror
197  *
198  * Caller must free name appropiately.
199  */
200 int getnamefromuuid(const uuidp_t uuidp, char **name, uuidtype_t *type) {
201     int ret;
202
203     ret = search_cachebyuuid( uuidp, name, type);
204     if (ret == 0) {
205         /* found in cache */
206         LOG(log_debug9, logtype_afpd, "getnamefromuuid{cache}: UUID: %s -> name: %s, type:%s",
207             uuid_bin2string(uuidp), *name, uuidtype[*type]);
208     } else {
209         /* not found in cache */
210
211         /* Check if UUID is a client local one */
212         if (memcmp(uuidp, local_user_uuid, 12) == 0
213             || memcmp(uuidp, local_group_uuid, 12) == 0) {
214             LOG(log_debug, logtype_afpd, "getnamefromuuid: local UUID: %" PRIu32 "",
215                 ntohl(*(uint32_t *)(uuidp + 12)));
216             *type = UUID_LOCAL;
217             *name = strdup("UUID_LOCAL");
218             return 0;
219         }
220
221 #ifdef HAVE_LDAP
222         ret = ldap_getnamefromuuid(uuid_bin2string(uuidp), name, type);
223         if (ret != 0) {
224             LOG(log_warning, logtype_afpd, "getnamefromuuid(%s): no result from ldap_getnamefromuuid",
225                 uuid_bin2string(uuidp));
226             goto cleanup;
227         }
228         add_cachebyuuid( uuidp, *name, *type, 0);
229         LOG(log_debug, logtype_afpd, "getnamefromuuid{LDAP}: UUID: %s -> name: %s, type:%s",
230             uuid_bin2string(uuidp), *name, uuidtype[*type]);
231 #endif
232     }
233
234 cleanup:
235     return ret;
236 }