]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
0143a1e4c7abb635c4e8e5c6f4cbdd19d5e204c2
[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
144     memset(vol, 0, sizeof(afpvol_t));
145 }
146
147 /*
148   Taken form afpd/desktop.c
149 */
150 char *utompath(const struct vol *vol, const char *upath)
151 {
152     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
153     char         *m;
154     const char   *u;
155     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
156     size_t       outlen;
157
158     if (!upath)
159         return NULL;
160
161     m = mpath;
162     u = upath;
163     outlen = strlen(upath);
164
165     if ((vol->v_casefold & AFPVOL_UTOMUPPER))
166         flags |= CONV_TOUPPER;
167     else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
168         flags |= CONV_TOLOWER;
169
170     if ((vol->v_flags & AFPVOL_EILSEQ)) {
171         flags |= CONV__EILSEQ;
172     }
173
174     /* convert charsets */
175     if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
176                                                  CH_UTF8_MAC,
177                                                  vol->v_maccharset,
178                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
179         SLOG("Conversion from %s to %s for %s failed.",
180              vol->v_volcodepage, vol->v_maccodepage, u);
181         return NULL;
182     }
183
184     return(m);
185 }
186
187
188 /*!
189  * Convert dot encoding of basename _in place_
190  *
191  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
192  * possible encoding ".file" as ":2efile" which means the result will be
193  * longer then the original which means provide a big enough buffer.
194  *
195  * @param svol   (r)  source volume
196  * @param dvol   (r)  destinatio volume
197  * @param path   (rw) path to convert _in place_
198  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
199  *
200  * @returns 0 on sucess, -1 on error
201  */
202 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
203 {
204     static charset_t from = (charset_t) -1;
205     static char buf[MAXPATHLEN+2];
206     char *bname = stripped_slashes_basename(path);
207     int pos = bname - path;
208     uint16_t flags = 0;
209
210     if ( ! svol->vol->v_path) {
211         /* no source volume: escape special chars (eg ':') */
212         from = dvol->vol->v_volcharset; /* src = dst charset */
213         flags |= CONV_ESCAPEHEX;
214     } else {
215         from = svol->vol->v_volcharset;
216     }
217
218     if ( (svol->vol->v_path)
219          && ! (svol->vol->v_flags & AFPVOL_USEDOTS)
220          && (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
221         /* source is without dots, destination is with */
222         flags |= CONV_UNESCAPEHEX;
223     } else if (! (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
224         flags |= CONV_ESCAPEDOTS;
225     }
226
227     int len = convert_charset(from,
228                               dvol->vol->v_volcharset,
229                               dvol->vol->v_maccharset,
230                               bname, strlen(bname),
231                               buf, MAXPATHLEN,
232                               &flags);
233     if (len == -1)
234         return -1;
235
236     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
237         return -1;
238     return 0;
239 }
240
241 /*!
242  * ResolvesCNID of a given paths
243  *
244  * path might be:
245  * (a) relative:
246  *     "dir/subdir" with cwd: "/afp_volume/topdir"
247  * (b) absolute:
248  *     "/afp_volume/dir/subdir"
249  *
250  * path MUST be pointing inside vol, this is usually the case as vol has been build from
251  * path using loadvolinfo and friends.
252  *
253  * @param vol  (r) pointer to afpvol_t
254  * @param path (r) path, see above
255  * @param did  (rw) parent CNID of returned CNID
256  *
257  * @returns CNID of path
258  */
259 cnid_t cnid_for_path(const afpvol_t *vol,
260                      const char *path,
261                      cnid_t *did)
262 {
263     EC_INIT;
264
265     cnid_t cnid;
266     bstring rpath = NULL;
267     bstring statpath = NULL;
268     struct bstrList *l = NULL;
269     struct stat st;
270
271     cnid = htonl(2);
272
273     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
274     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
275     EC_ZERO(bcatcstr(statpath, "/"));
276
277     l = bsplit(rpath, '/');
278     for (int i = 0; i < l->qty ; i++) {
279         *did = cnid;
280
281         EC_ZERO(bconcat(statpath, l->entry[i]));
282         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
283                        "lstat(rpath: %s, elem: %s): %s: %s",
284                        cfrombstr(rpath), cfrombstr(l->entry[i]),
285                        cfrombstr(statpath), strerror(errno));
286
287         if ((cnid = cnid_add(vol->vol->v_cdb,
288                              &st,
289                              *did,
290                              cfrombstr(l->entry[i]),
291                              blength(l->entry[i]),
292                              0)) == CNID_INVALID) {
293             EC_FAIL;
294         }
295         EC_ZERO(bcatcstr(statpath, "/"));
296     }
297
298 EC_CLEANUP:
299     bdestroy(rpath);
300     bstrListDestroy(l);
301     bdestroy(statpath);
302     if (ret != 0)
303         return CNID_INVALID;
304
305     return cnid;
306 }
307
308 /*!
309  * Resolves CNID of a given paths parent directory
310  *
311  * path might be:
312  * (a) relative:
313  *     "dir/subdir" with cwd: "/afp_volume/topdir"
314  * (b) absolute:
315  *     "/afp_volume/dir/subdir"
316  *
317  * path MUST be pointing inside vol, this is usually the case as vol has been build from
318  * path using loadvolinfo and friends.
319  *
320  * @param vol  (r) pointer to afpvol_t
321  * @param path (r) path, see above
322  * @param did  (rw) parent CNID of returned CNID
323  *
324  * @returns CNID of path
325  */
326 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
327                              const char *path,
328                              cnid_t *did)
329 {
330     EC_INIT;
331
332     cnid_t cnid;
333     bstring rpath = NULL;
334     bstring statpath = NULL;
335     struct bstrList *l = NULL;
336     struct stat st;
337
338     *did = htonl(1);
339     cnid = htonl(2);
340
341     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
342     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
343
344     l = bsplit(rpath, '/');
345     if (l->qty == 1)
346         /* only one path element, means parent dir cnid is volume root = 2 */
347         goto EC_CLEANUP;
348     for (int i = 0; i < (l->qty - 1); i++) {
349         *did = cnid;
350         EC_ZERO(bconcat(statpath, l->entry[i]));
351         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
352                        "lstat(rpath: %s, elem: %s): %s: %s",
353                        cfrombstr(rpath), cfrombstr(l->entry[i]),
354                        cfrombstr(statpath), strerror(errno));
355
356         if ((cnid = cnid_add(vol->vol->v_cdb,
357                              &st,
358                              *did,
359                              cfrombstr(l->entry[i]),
360                              blength(l->entry[i]),
361                              0)) == CNID_INVALID) {
362             EC_FAIL;
363         }
364         EC_ZERO(bcatcstr(statpath, "/"));
365     }
366
367 EC_CLEANUP:
368     bdestroy(rpath);
369     bstrListDestroy(l);
370     bdestroy(statpath);
371     if (ret != 0)
372         return CNID_INVALID;
373
374     return cnid;
375 }
376