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