]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
Convert name of objects
[netatalk.git] / bin / ad / ad_util.c
1 /* 
2  * Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3  * Copyright (c) 1991, 1993, 1994
4  * The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.   
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif /* HAVE_CONFIG_H */
34
35 #include <sys/types.h>
36 #include <sys/acl.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48 #include <stdarg.h>
49 #include <string.h>
50
51 #include <atalk/cnid.h>
52 #include <atalk/volinfo.h>
53 #include <atalk/bstrlib.h>
54 #include <atalk/bstradd.h>
55 #include <atalk/util.h>
56 #include <atalk/logger.h>
57 #include <atalk/errchk.h>
58 #include <atalk/unicode.h>
59
60 #include "ad.h"
61
62 int log_verbose;             /* Logging flag */
63
64 void _log(enum logtype lt, char *fmt, ...)
65 {
66     int len;
67     static char logbuffer[1024];
68     va_list args;
69
70     if ( (lt == STD) || (log_verbose == 1)) {
71         va_start(args, fmt);
72         len = vsnprintf(logbuffer, 1023, fmt, args);
73         va_end(args);
74         logbuffer[1023] = 0;
75
76         printf("%s\n", logbuffer);
77     }
78 }
79
80 /*!
81  * Load volinfo and initialize struct vol
82  *
83  * @param path   (r)  path to evaluate
84  * @param vol    (rw) structure to initialize 
85 *
86  * @returns 0 on success, exits on error
87  */
88 int openvol(const char *path, afpvol_t *vol)
89 {
90     int flags = 0;
91
92     memset(vol, 0, sizeof(afpvol_t));
93
94     /* try to find a .AppleDesktop/.volinfo */
95     if (loadvolinfo((char *)path, &vol->volinfo) == 0) {
96
97         if (vol_load_charsets(&vol->volinfo) == -1)
98             ERROR("Error loading charsets!");
99
100         /* Sanity checks to ensure we can touch this volume */
101         if (vol->volinfo.v_adouble != AD_VERSION2)
102             ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
103
104         if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
105             ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
106
107         /* initialize sufficient struct vol for VFS initialisation */
108         vol->volume.v_adouble = AD_VERSION2;
109         vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
110         initvol_vfs(&vol->volume);
111
112         if ((vol->volinfo.v_flags & AFPVOL_NODEV))
113             flags |= CNID_FLAG_NODEV;
114
115         if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_dbpath,
116                                            0000,
117                                            "dbd",
118                                            flags,
119                                            vol->volinfo.v_dbd_host,
120                                            vol->volinfo.v_dbd_port)) == NULL)
121             ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
122
123         cnid_getstamp(vol->volume.v_cdb,
124                       vol->db_stamp,
125                       sizeof(vol->db_stamp));
126     }
127
128     return 0;
129 }
130
131 void closevol(afpvol_t *vol)
132 {
133     if (vol->volume.v_cdb)
134         cnid_close(vol->volume.v_cdb);
135
136     memset(vol, 0, sizeof(afpvol_t));
137 }
138
139 /*
140   Taken form afpd/desktop.c
141 */
142 char *utompath(const struct volinfo *volinfo, const char *upath)
143 {
144     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
145     char         *m;
146     const char   *u;
147     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
148     size_t       outlen;
149
150     if (!upath)
151         return NULL;
152
153     m = mpath;
154     u = upath;
155     outlen = strlen(upath);
156
157     if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
158         flags |= CONV_TOUPPER;
159     else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
160         flags |= CONV_TOLOWER;
161
162     if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
163         flags |= CONV__EILSEQ;
164     }
165
166     /* convert charsets */
167     if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
168                                                  CH_UTF8_MAC,
169                                                  volinfo->v_maccharset,
170                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
171         SLOG("Conversion from %s to %s for %s failed.",
172             volinfo->v_volcodepage, volinfo->v_maccodepage, u);
173         return NULL;
174     }
175
176     return(m);
177 }
178
179 /*!
180  * Build path relativ to volume root
181  *
182  * path might be:
183  * (a) relative:
184  *     "dir/subdir" with cwd: "/afp_volume/topdir"
185  * (b) absolute:
186  *     "/afp_volume/dir/subdir"
187  *
188  * @param path     (r) path relative to cwd() or absolute
189  * @param volpath  (r) volume path that path is a subdir of (has been computed in volinfo funcs) 
190  *
191  * @returns relative path in new bstring, caller must bdestroy it
192  */
193 static bstring rel_path_in_vol(const char *path, const char *volpath)
194 {
195     EC_INIT;
196
197     if (path == NULL || volpath == NULL)
198         return NULL;
199
200     bstring fpath = NULL;
201
202     /* Make path absolute by concetanating for case (a) */
203     if (path[0] != '/') {
204         EC_NULL(fpath = bfromcstr(getcwdpath()));
205         if (bchar(fpath, blength(fpath) - 1) != '/')
206             EC_ZERO(bcatcstr(fpath, "/"));
207         EC_ZERO(bcatcstr(fpath, path));
208         BSTRING_STRIP_SLASH(fpath);
209     } else {
210         EC_NULL(fpath = bfromcstr(path));
211         BSTRING_STRIP_SLASH(fpath);
212     }
213
214     /*
215      * Now we have eg:
216      *   fpath:   /Volume/netatalk/dir/bla
217      *   volpath: /Volume/netatalk/
218      * we want: "dir/bla"
219      */
220     EC_ZERO(bdelete(fpath, 0, strlen(volpath)));
221     return fpath;
222
223 EC_CLEANUP:
224     bdestroy(fpath);
225     return NULL;
226 }
227
228 /*!
229  * Convert dot encoding of basename _in place_
230  *
231  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
232  * possible encoding ".file" as ":2efile" which means the result will be
233  * longer then the original which means provide a big enough buffer.
234  *
235  * @param svol   (r)  source volume
236  * @param dvol   (r)  destinatio volume
237  * @param path   (rw) path to convert _in place_
238  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
239  *
240  * @returns 0 on sucess, -1 on error
241  */
242 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
243 {
244     static charset_t from = (charset_t) -1;
245     static char buf[MAXPATHLEN+2];
246     char *bname = stripped_slashes_basename(path);
247     int pos = bname - path;
248     uint16_t flags = 0;
249
250     if ( ! svol->volinfo.v_path) {
251         /* no source volume: escape special chars (eg ':') */
252         from = dvol->volinfo.v_volcharset; /* src = dst charset */
253         flags |= CONV_ESCAPEHEX;
254     } else {
255         from = svol->volinfo.v_volcharset;
256     }
257
258     if ( (svol->volinfo.v_path)
259          && ! (svol->volinfo.v_flags & AFPVOL_USEDOTS)
260          && (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
261         /* source is without dots, destination is with */
262         flags |= CONV_UNESCAPEHEX;
263     } else if (! (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
264         flags |= CONV_ESCAPEDOTS;
265     }
266
267     int len = convert_charset(from,
268                               dvol->volinfo.v_volcharset,
269                               dvol->volinfo.v_maccharset,
270                               bname, strlen(bname),
271                               buf, MAXPATHLEN,
272                               &flags);
273     if (len == -1)
274         return -1;
275
276     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
277         return -1;
278     return 0;
279 }
280
281 /*!
282  * ResolvesCNID of a given paths
283  *
284  * path might be:
285  * (a) relative:
286  *     "dir/subdir" with cwd: "/afp_volume/topdir"
287  * (b) absolute:
288  *     "/afp_volume/dir/subdir"
289  *
290  * 1) start recursive CNID search with
291  *    a) DID:2 / "topdir"
292  *    b) DID:2 / "dir"
293  * 2) ...until we have the CNID for
294  *    a) "/afp_volume/topdir/dir"
295  *    b) "/afp_volume/dir" (no recursion required)
296  *
297  * @param vi   (r) pointer to volinfo struct
298  * @param vol  (r) pointer to vol struct
299  * @param path (r) path, see above
300  * @param did  (rw) parent CNID of returned CNID
301  *
302  * @returns CNID of path
303  */
304 cnid_t cnid_for_path(const struct volinfo *vi,
305                      const struct vol *vol,
306                      const char *path,
307                      cnid_t *did)
308 {
309     EC_INIT;
310
311     cnid_t cnid;
312     bstring rpath = NULL;
313     bstring statpath = NULL;
314     struct bstrList *l = NULL;
315     struct stat st;
316
317     cnid = *did = htonl(2);
318
319     EC_NULL(rpath = rel_path_in_vol(path, vi->v_path));
320     EC_NULL(statpath = bfromcstr(vi->v_path));
321
322     l = bsplit(rpath, '/');
323     for(int i = 0; i < l->qty ; i++) {
324         *did = cnid;
325         EC_ZERO(bconcat(statpath, l->entry[i]));
326         EC_ZERO_LOG(stat(cfrombstr(statpath), &st));
327
328         cnid = cnid_add(vol->v_cdb,
329                         &st,
330                         *did,
331                         cfrombstr(l->entry[i]),
332                         blength(l->entry[i]),
333                         0);
334
335         EC_ZERO(bcatcstr(statpath, "/"));
336         
337     }
338
339 EC_CLEANUP:
340     bdestroy(rpath);
341     bstrListDestroy(l);
342     bdestroy(statpath);
343     if (ret != 0)
344         return CNID_INVALID;
345
346     return cnid;
347 }
348
349