]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
Removing links didn't work
[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 #include <libgen.h>
51
52 #include <atalk/util.h>
53 #include <atalk/cnid.h>
54 #include <atalk/volinfo.h>
55 #include <atalk/bstrlib.h>
56 #include <atalk/bstradd.h>
57 #include <atalk/logger.h>
58 #include <atalk/errchk.h>
59 #include <atalk/unicode.h>
60
61 #include "ad.h"
62
63 int log_verbose;             /* Logging flag */
64
65 void _log(enum logtype lt, char *fmt, ...)
66 {
67     int len;
68     static char logbuffer[1024];
69     va_list args;
70
71     if ( (lt == STD) || (log_verbose == 1)) {
72         va_start(args, fmt);
73         len = vsnprintf(logbuffer, 1023, fmt, args);
74         va_end(args);
75         logbuffer[1023] = 0;
76
77         printf("%s\n", logbuffer);
78     }
79 }
80
81 /*!
82  * Load volinfo and initialize struct vol
83  *
84  * Only opens "dbd" volumes ! 
85  *
86  * @param path   (r)  path to evaluate
87  * @param vol    (rw) structure to initialize 
88  *
89  * @returns 0 on success, exits on error
90  */
91 int openvol(const char *path, afpvol_t *vol)
92 {
93     int flags = 0;
94
95     memset(vol, 0, sizeof(afpvol_t));
96
97     /* try to find a .AppleDesktop/.volinfo */
98     if (loadvolinfo((char *)path, &vol->volinfo) == 0) {
99
100         if (STRCMP(vol->volinfo.v_cnidscheme, != , "dbd"))
101             ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->volinfo.v_path);
102
103         if (vol_load_charsets(&vol->volinfo) == -1)
104             ERROR("Error loading charsets!");
105
106         /* Sanity checks to ensure we can touch this volume */
107         if (vol->volinfo.v_adouble != AD_VERSION2)
108             ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
109
110         if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
111             ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
112
113         /* initialize sufficient struct vol for VFS initialisation */
114         vol->volume.v_adouble = AD_VERSION2;
115         vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
116         initvol_vfs(&vol->volume);
117
118         if ((vol->volinfo.v_flags & AFPVOL_NODEV))
119             flags |= CNID_FLAG_NODEV;
120
121         if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_dbpath,
122                                            0000,
123                                            "dbd",
124                                            flags,
125                                            vol->volinfo.v_dbd_host,
126                                            vol->volinfo.v_dbd_port)) == NULL)
127             ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
128
129         cnid_getstamp(vol->volume.v_cdb,
130                       vol->db_stamp,
131                       sizeof(vol->db_stamp));
132     }
133
134     return 0;
135 }
136
137 void closevol(afpvol_t *vol)
138 {
139     if (vol->volume.v_cdb)
140         cnid_close(vol->volume.v_cdb);
141
142     memset(vol, 0, sizeof(afpvol_t));
143 }
144
145 /*
146   Taken form afpd/desktop.c
147 */
148 char *utompath(const struct volinfo *volinfo, const char *upath)
149 {
150     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
151     char         *m;
152     const char   *u;
153     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
154     size_t       outlen;
155
156     if (!upath)
157         return NULL;
158
159     m = mpath;
160     u = upath;
161     outlen = strlen(upath);
162
163     if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
164         flags |= CONV_TOUPPER;
165     else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
166         flags |= CONV_TOLOWER;
167
168     if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
169         flags |= CONV__EILSEQ;
170     }
171
172     /* convert charsets */
173     if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
174                                                  CH_UTF8_MAC,
175                                                  volinfo->v_maccharset,
176                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
177         SLOG("Conversion from %s to %s for %s failed.",
178             volinfo->v_volcodepage, volinfo->v_maccodepage, u);
179         return NULL;
180     }
181
182     return(m);
183 }
184
185 /*!
186  * Build path relativ to volume root
187  *
188  * path might be:
189  * (a) relative:
190  *     "dir/subdir" with cwd: "/afp_volume/topdir"
191  * (b) absolute:
192  *     "/afp_volume/dir/subdir"
193  *
194  * @param path     (r) path relative to cwd() or absolute
195  * @param volpath  (r) volume path that path is a subdir of (has been computed in volinfo funcs) 
196  *
197  * @returns relative path in new bstring, caller must bdestroy it
198  */
199 static bstring rel_path_in_vol(const char *path, const char *volpath)
200 {
201     EC_INIT;
202     int cwd = -1;
203     bstring fpath = NULL;
204     char *dname = NULL;
205     struct stat st;
206
207     if (path == NULL || volpath == NULL)
208         return NULL;
209
210     EC_NEG1_LOG(cwd = open(".", O_RDONLY));
211
212     EC_ZERO_LOGSTR(lstat(path, &st), "lstat(%s): %s", path, strerror(errno));
213     switch (S_IFMT & st.st_mode) {
214
215     case S_IFREG:
216     case S_IFLNK:
217         EC_NULL_LOG(dname = strdup(path));
218         EC_ZERO_LOGSTR(chdir(dirname(dname)), "chdir(%s): %s", dirname, strerror(errno));
219         free(dname);
220         dname = NULL;
221         EC_NULL(fpath = bfromcstr(getcwdpath()));
222         BSTRING_STRIP_SLASH(fpath);
223         EC_ZERO(bcatcstr(fpath, "/"));
224         EC_NULL_LOG(dname = strdup(path));
225         EC_ZERO(bcatcstr(fpath, basename(dname)));
226         break;
227
228     case S_IFDIR:
229         EC_ZERO_LOGSTR(chdir(path), "chdir(%s): %s", path, strerror(errno));
230         EC_NULL(fpath = bfromcstr(getcwdpath()));
231         break;
232
233     default:
234         SLOG("special: %s", path);
235         EC_FAIL;
236     }
237
238     BSTRING_STRIP_SLASH(fpath);
239
240     /*
241      * Now we have eg:
242      *   fpath:   /Volume/netatalk/dir/bla
243      *   volpath: /Volume/netatalk/
244      * we want: "dir/bla"
245      */
246     EC_ZERO(bdelete(fpath, 0, strlen(volpath)));
247
248 EC_CLEANUP:
249     if (dname) free(dname);
250     if (cwd != -1) {
251         fchdir(cwd);
252         close(cwd);
253     }
254     if (ret != 0)
255         return NULL;
256     return fpath;
257 }
258
259 /*!
260  * Convert dot encoding of basename _in place_
261  *
262  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
263  * possible encoding ".file" as ":2efile" which means the result will be
264  * longer then the original which means provide a big enough buffer.
265  *
266  * @param svol   (r)  source volume
267  * @param dvol   (r)  destinatio volume
268  * @param path   (rw) path to convert _in place_
269  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
270  *
271  * @returns 0 on sucess, -1 on error
272  */
273 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
274 {
275     static charset_t from = (charset_t) -1;
276     static char buf[MAXPATHLEN+2];
277     char *bname = stripped_slashes_basename(path);
278     int pos = bname - path;
279     uint16_t flags = 0;
280
281     if ( ! svol->volinfo.v_path) {
282         /* no source volume: escape special chars (eg ':') */
283         from = dvol->volinfo.v_volcharset; /* src = dst charset */
284         flags |= CONV_ESCAPEHEX;
285     } else {
286         from = svol->volinfo.v_volcharset;
287     }
288
289     if ( (svol->volinfo.v_path)
290          && ! (svol->volinfo.v_flags & AFPVOL_USEDOTS)
291          && (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
292         /* source is without dots, destination is with */
293         flags |= CONV_UNESCAPEHEX;
294     } else if (! (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
295         flags |= CONV_ESCAPEDOTS;
296     }
297
298     int len = convert_charset(from,
299                               dvol->volinfo.v_volcharset,
300                               dvol->volinfo.v_maccharset,
301                               bname, strlen(bname),
302                               buf, MAXPATHLEN,
303                               &flags);
304     if (len == -1)
305         return -1;
306
307     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
308         return -1;
309     return 0;
310 }
311
312 /*!
313  * ResolvesCNID of a given paths
314  *
315  * path might be:
316  * (a) relative:
317  *     "dir/subdir" with cwd: "/afp_volume/topdir"
318  * (b) absolute:
319  *     "/afp_volume/dir/subdir"
320  *
321  * path MUST be pointing inside vol, this is usually the case as vol has been build from
322  * path using loadvolinfo and friends.
323  *
324  * @param vol  (r) pointer to afpvol_t
325  * @param path (r) path, see above
326  * @param did  (rw) parent CNID of returned CNID
327  *
328  * @returns CNID of path
329  */
330 cnid_t cnid_for_path(const afpvol_t *vol,
331                      const char *path,
332                      cnid_t *did)
333 {
334     EC_INIT;
335
336     cnid_t cnid;
337     bstring rpath = NULL;
338     bstring statpath = NULL;
339     struct bstrList *l = NULL;
340     struct stat st;
341
342     *did = htonl(1);
343     cnid = htonl(2);
344
345     EC_NULL(rpath = rel_path_in_vol(path, vol->volinfo.v_path));
346     EC_NULL(statpath = bfromcstr(vol->volinfo.v_path));
347
348     l = bsplit(rpath, '/');
349     for (int i = 0; i < l->qty ; i++) {
350         *did = cnid;
351         EC_ZERO(bconcat(statpath, l->entry[i]));
352         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
353                        "lstat(rpath: %s, elem: %s): %s: %s",
354                        cfrombstr(rpath), cfrombstr(l->entry[i]),
355                        cfrombstr(statpath), strerror(errno));
356
357         if ((cnid = cnid_add(vol->volume.v_cdb,
358                         &st,
359                         *did,
360                         cfrombstr(l->entry[i]),
361                         blength(l->entry[i]),
362                              0)) == CNID_INVALID) {
363             EC_FAIL;
364         }
365         EC_ZERO(bcatcstr(statpath, "/"));        
366     }
367
368 EC_CLEANUP:
369     bdestroy(rpath);
370     bstrListDestroy(l);
371     bdestroy(statpath);
372     if (ret != 0)
373         return CNID_INVALID;
374
375     return cnid;
376 }
377
378 /*!
379  * Resolves CNID of a given paths parent directory
380  *
381  * path might be:
382  * (a) relative:
383  *     "dir/subdir" with cwd: "/afp_volume/topdir"
384  * (b) absolute:
385  *     "/afp_volume/dir/subdir"
386  *
387  * path MUST be pointing inside vol, this is usually the case as vol has been build from
388  * path using loadvolinfo and friends.
389  *
390  * @param vol  (r) pointer to afpvol_t
391  * @param path (r) path, see above
392  * @param did  (rw) parent CNID of returned CNID
393  *
394  * @returns CNID of path
395  */
396 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
397                              const char *path,
398                              cnid_t *did)
399 {
400     EC_INIT;
401
402     cnid_t cnid;
403     bstring rpath = NULL;
404     bstring statpath = NULL;
405     struct bstrList *l = NULL;
406     struct stat st;
407
408     *did = htonl(1);
409     cnid = htonl(2);
410
411     EC_NULL(rpath = rel_path_in_vol(path, vol->volinfo.v_path));
412     EC_NULL(statpath = bfromcstr(vol->volinfo.v_path));
413
414     l = bsplit(rpath, '/');
415     if (l->qty == 1)
416         /* only one path element, means parent dir cnid is volume root = 2 */
417         goto EC_CLEANUP;
418     for (int i = 0; i < (l->qty - 1); i++) {
419         *did = cnid;
420         EC_ZERO(bconcat(statpath, l->entry[i]));
421         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
422                        "lstat(rpath: %s, elem: %s): %s: %s",
423                        cfrombstr(rpath), cfrombstr(l->entry[i]),
424                        cfrombstr(statpath), strerror(errno));
425
426         if ((cnid = cnid_add(vol->volume.v_cdb,
427                         &st,
428                         *did,
429                         cfrombstr(l->entry[i]),
430                         blength(l->entry[i]),
431                              0)) == CNID_INVALID) {
432             EC_FAIL;
433         }
434         EC_ZERO(bcatcstr(statpath, "/"));        
435     }
436
437 EC_CLEANUP:
438     bdestroy(rpath);
439     bstrListDestroy(l);
440     bdestroy(statpath);
441     if (ret != 0)
442         return CNID_INVALID;
443
444     return cnid;
445 }
446