]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
Merge remote branch 'origin/product-2-2' into develop
[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/bstrlib.h>
63 #include <atalk/bstradd.h>
64 #include <atalk/logger.h>
65 #include <atalk/errchk.h>
66 #include <atalk/unicode.h>
67 #include <atalk/globals.h>
68 #include <atalk/netatalk_conf.h>
69
70
71 #include "ad.h"
72
73 int log_verbose;             /* Logging flag */
74
75 void _log(enum logtype lt, char *fmt, ...)
76 {
77     int len;
78     static char logbuffer[1024];
79     va_list args;
80
81     if ( (lt == STD) || (log_verbose == 1)) {
82         va_start(args, fmt);
83         len = vsnprintf(logbuffer, 1023, fmt, args);
84         va_end(args);
85         logbuffer[1023] = 0;
86
87         printf("%s\n", logbuffer);
88     }
89 }
90
91 /*!
92  * Load volinfo and initialize struct vol
93  *
94  * Only opens "dbd" volumes !
95  *
96  * @param path   (r)  path to evaluate
97  * @param vol    (rw) structure to initialize
98  *
99  * @returns 0 on success, exits on error
100  */
101 int openvol(AFPObj *obj, const char *path, afpvol_t *vol)
102 {
103     int flags = 0;
104
105     memset(vol, 0, sizeof(afpvol_t));
106
107     if ((vol->vol = getvolbypath(obj, path)) == NULL)
108         return -1;
109
110     if (STRCMP(vol->vol->v_cnidscheme, != , "dbd"))
111         ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->vol->v_path);
112
113     /* Sanity checks to ensure we can touch this volume */
114     if (vol->vol->v_adouble != AD_VERSION2
115         && vol->vol->v_adouble != AD_VERSION_EA)
116         ERROR("Unsupported adouble versions: %u", vol->vol->v_adouble);
117
118     if (vol->vol->v_vfs_ea != AFPVOL_EA_SYS)
119         ERROR("Unsupported Extended Attributes option: %u", vol->vol->v_vfs_ea);
120
121     if ((vol->vol->v_flags & AFPVOL_NODEV))
122         flags |= CNID_FLAG_NODEV;
123
124     if ((vol->vol->v_cdb = cnid_open(vol->vol->v_path,
125                                      0000,
126                                      "dbd",
127                                      flags,
128                                      vol->vol->v_cnidserver,
129                                      vol->vol->v_cnidport)) == NULL)
130         ERROR("Cant initialize CNID database connection for %s", vol->vol->v_path);
131
132     cnid_getstamp(vol->vol->v_cdb,
133                   vol->db_stamp,
134                   sizeof(vol->db_stamp));
135     
136     return 0;
137 }
138
139 void closevol(afpvol_t *vol)
140 {
141     if (vol->vol->v_cdb) {
142         cnid_close(vol->vol->v_cdb);
143         vol->vol->v_cdb = NULL;
144     }
145     memset(vol, 0, sizeof(afpvol_t));
146 }
147
148 /*
149   Taken form afpd/desktop.c
150 */
151 char *utompath(const struct vol *vol, const char *upath)
152 {
153     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
154     char         *m;
155     const char   *u;
156     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
157     size_t       outlen;
158
159     if (!upath)
160         return NULL;
161
162     m = mpath;
163     u = upath;
164     outlen = strlen(upath);
165
166     if ((vol->v_casefold & AFPVOL_UTOMUPPER))
167         flags |= CONV_TOUPPER;
168     else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
169         flags |= CONV_TOLOWER;
170
171     if ((vol->v_flags & AFPVOL_EILSEQ)) {
172         flags |= CONV__EILSEQ;
173     }
174
175     /* convert charsets */
176     if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
177                                                  CH_UTF8_MAC,
178                                                  vol->v_maccharset,
179                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
180         SLOG("Conversion from %s to %s for %s failed.",
181              vol->v_volcodepage, vol->v_maccodepage, u);
182         return NULL;
183     }
184
185     return(m);
186 }
187
188
189 /*!
190  * Convert dot encoding of basename _in place_
191  *
192  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
193  * possible encoding ".file" as ":2efile" which means the result will be
194  * longer then the original which means provide a big enough buffer.
195  *
196  * @param svol   (r)  source volume
197  * @param dvol   (r)  destinatio volume
198  * @param path   (rw) path to convert _in place_
199  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
200  *
201  * @returns 0 on sucess, -1 on error
202  */
203 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
204 {
205     static charset_t from = (charset_t) -1;
206     static char buf[MAXPATHLEN+2];
207     char *bname = stripped_slashes_basename(path);
208     int pos = bname - path;
209     uint16_t flags = 0;
210
211     if ( ! svol->vol->v_path) {
212         /* no source volume: escape special chars (eg ':') */
213         from = dvol->vol->v_volcharset; /* src = dst charset */
214         flags |= CONV_ESCAPEHEX;
215     } else {
216         from = svol->vol->v_volcharset;
217     }
218
219     if ( (svol->vol->v_path)
220          && ! (svol->vol->v_flags & AFPVOL_USEDOTS)
221          && (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
222         /* source is without dots, destination is with */
223         flags |= CONV_UNESCAPEHEX;
224     } else if (! (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
225         flags |= CONV_ESCAPEDOTS;
226     }
227
228     int len = convert_charset(from,
229                               dvol->vol->v_volcharset,
230                               dvol->vol->v_maccharset,
231                               bname, strlen(bname),
232                               buf, MAXPATHLEN,
233                               &flags);
234     if (len == -1)
235         return -1;
236
237     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
238         return -1;
239     return 0;
240 }
241
242 /*!
243  * ResolvesCNID of a given paths
244  *
245  * path might be:
246  * (a) relative:
247  *     "dir/subdir" with cwd: "/afp_volume/topdir"
248  * (b) absolute:
249  *     "/afp_volume/dir/subdir"
250  *
251  * path MUST be pointing inside vol, this is usually the case as vol has been build from
252  * path using loadvolinfo and friends.
253  *
254  * @param vol  (r) pointer to afpvol_t
255  * @param path (r) path, see above
256  * @param did  (rw) parent CNID of returned CNID
257  *
258  * @returns CNID of path
259  */
260 cnid_t cnid_for_path(const afpvol_t *vol,
261                      const char *path,
262                      cnid_t *did)
263 {
264     EC_INIT;
265
266     cnid_t cnid;
267     bstring rpath = NULL;
268     bstring statpath = NULL;
269     struct bstrList *l = NULL;
270     struct stat st;
271
272     cnid = htonl(2);
273
274     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
275     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
276     EC_ZERO(bcatcstr(statpath, "/"));
277
278     l = bsplit(rpath, '/');
279     for (int i = 0; i < l->qty ; i++) {
280         *did = cnid;
281
282         EC_ZERO(bconcat(statpath, l->entry[i]));
283         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
284                        "lstat(rpath: %s, elem: %s): %s: %s",
285                        cfrombstr(rpath), cfrombstr(l->entry[i]),
286                        cfrombstr(statpath), strerror(errno));
287
288         if ((cnid = cnid_add(vol->vol->v_cdb,
289                              &st,
290                              *did,
291                              cfrombstr(l->entry[i]),
292                              blength(l->entry[i]),
293                              0)) == CNID_INVALID) {
294             EC_FAIL;
295         }
296         EC_ZERO(bcatcstr(statpath, "/"));
297     }
298
299 EC_CLEANUP:
300     bdestroy(rpath);
301     bstrListDestroy(l);
302     bdestroy(statpath);
303     if (ret != 0)
304         return CNID_INVALID;
305
306     return cnid;
307 }
308
309 /*!
310  * Resolves CNID of a given paths parent directory
311  *
312  * path might be:
313  * (a) relative:
314  *     "dir/subdir" with cwd: "/afp_volume/topdir"
315  * (b) absolute:
316  *     "/afp_volume/dir/subdir"
317  *
318  * path MUST be pointing inside vol, this is usually the case as vol has been build from
319  * path using loadvolinfo and friends.
320  *
321  * @param vol  (r) pointer to afpvol_t
322  * @param path (r) path, see above
323  * @param did  (rw) parent CNID of returned CNID
324  *
325  * @returns CNID of path
326  */
327 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
328                              const char *path,
329                              cnid_t *did)
330 {
331     EC_INIT;
332
333     cnid_t cnid;
334     bstring rpath = NULL;
335     bstring statpath = NULL;
336     struct bstrList *l = NULL;
337     struct stat st;
338
339     *did = htonl(1);
340     cnid = htonl(2);
341
342     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
343     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
344
345     l = bsplit(rpath, '/');
346     if (l->qty == 1)
347         /* only one path element, means parent dir cnid is volume root = 2 */
348         goto EC_CLEANUP;
349     for (int i = 0; i < (l->qty - 1); 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->vol->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