]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
Convert ad file suite and CNID daemons to use new config code
[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/netatalk_conf.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 ((vol->vol = getvolbypath(path)) == NULL)
107         return -1;
108
109     if (STRCMP(vol->vol->v_cnidscheme, != , "dbd"))
110         ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->vol->v_path);
111
112     /* Sanity checks to ensure we can touch this volume */
113     if (vol->vol->v_adouble != AD_VERSION2)
114         ERROR("Unsupported adouble versions: %u", vol->vol->v_adouble);
115
116     if (vol->vol->v_vfs_ea != AFPVOL_EA_SYS)
117         ERROR("Unsupported Extended Attributes option: %u", vol->vol->v_vfs_ea);
118
119     if ((vol->vol->v_flags & AFPVOL_NODEV))
120         flags |= CNID_FLAG_NODEV;
121
122     if ((vol->vol->v_cdb = cnid_open(vol->vol->v_path,
123                                      0000,
124                                      "dbd",
125                                      flags,
126                                      vol->vol->v_cnidserver,
127                                      vol->vol->v_cnidport)) == NULL)
128         ERROR("Cant initialize CNID database connection for %s", vol->vol->v_path);
129
130     cnid_getstamp(vol->vol->v_cdb,
131                   vol->db_stamp,
132                   sizeof(vol->db_stamp));
133     
134     return 0;
135 }
136
137 void closevol(afpvol_t *vol)
138 {
139     if (vol->vol->v_cdb)
140         cnid_close(vol->vol->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 vol *vol, 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 ((vol->v_casefold & AFPVOL_UTOMUPPER))
164         flags |= CONV_TOUPPER;
165     else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
166         flags |= CONV_TOLOWER;
167
168     if ((vol->v_flags & AFPVOL_EILSEQ)) {
169         flags |= CONV__EILSEQ;
170     }
171
172     /* convert charsets */
173     if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
174                                                  CH_UTF8_MAC,
175                                                  vol->v_maccharset,
176                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
177         SLOG("Conversion from %s to %s for %s failed.",
178              vol->v_volcodepage, vol->v_maccodepage, u);
179         return NULL;
180     }
181
182     return(m);
183 }
184
185
186 /*!
187  * Convert dot encoding of basename _in place_
188  *
189  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
190  * possible encoding ".file" as ":2efile" which means the result will be
191  * longer then the original which means provide a big enough buffer.
192  *
193  * @param svol   (r)  source volume
194  * @param dvol   (r)  destinatio volume
195  * @param path   (rw) path to convert _in place_
196  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
197  *
198  * @returns 0 on sucess, -1 on error
199  */
200 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
201 {
202     static charset_t from = (charset_t) -1;
203     static char buf[MAXPATHLEN+2];
204     char *bname = stripped_slashes_basename(path);
205     int pos = bname - path;
206     uint16_t flags = 0;
207
208     if ( ! svol->vol->v_path) {
209         /* no source volume: escape special chars (eg ':') */
210         from = dvol->vol->v_volcharset; /* src = dst charset */
211         flags |= CONV_ESCAPEHEX;
212     } else {
213         from = svol->vol->v_volcharset;
214     }
215
216     if ( (svol->vol->v_path)
217          && ! (svol->vol->v_flags & AFPVOL_USEDOTS)
218          && (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
219         /* source is without dots, destination is with */
220         flags |= CONV_UNESCAPEHEX;
221     } else if (! (dvol->vol->v_flags & AFPVOL_USEDOTS)) {
222         flags |= CONV_ESCAPEDOTS;
223     }
224
225     int len = convert_charset(from,
226                               dvol->vol->v_volcharset,
227                               dvol->vol->v_maccharset,
228                               bname, strlen(bname),
229                               buf, MAXPATHLEN,
230                               &flags);
231     if (len == -1)
232         return -1;
233
234     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
235         return -1;
236     return 0;
237 }
238
239 /*!
240  * ResolvesCNID of a given paths
241  *
242  * path might be:
243  * (a) relative:
244  *     "dir/subdir" with cwd: "/afp_volume/topdir"
245  * (b) absolute:
246  *     "/afp_volume/dir/subdir"
247  *
248  * path MUST be pointing inside vol, this is usually the case as vol has been build from
249  * path using loadvolinfo and friends.
250  *
251  * @param vol  (r) pointer to afpvol_t
252  * @param path (r) path, see above
253  * @param did  (rw) parent CNID of returned CNID
254  *
255  * @returns CNID of path
256  */
257 cnid_t cnid_for_path(const afpvol_t *vol,
258                      const char *path,
259                      cnid_t *did)
260 {
261     EC_INIT;
262
263     cnid_t cnid;
264     bstring rpath = NULL;
265     bstring statpath = NULL;
266     struct bstrList *l = NULL;
267     struct stat st;
268
269     cnid = htonl(2);
270
271     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
272     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
273     EC_ZERO(bcatcstr(statpath, "/"));
274
275     l = bsplit(rpath, '/');
276     for (int i = 0; i < l->qty ; i++) {
277         *did = cnid;
278
279         EC_ZERO(bconcat(statpath, l->entry[i]));
280         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
281                        "lstat(rpath: %s, elem: %s): %s: %s",
282                        cfrombstr(rpath), cfrombstr(l->entry[i]),
283                        cfrombstr(statpath), strerror(errno));
284
285         if ((cnid = cnid_add(vol->vol->v_cdb,
286                              &st,
287                              *did,
288                              cfrombstr(l->entry[i]),
289                              blength(l->entry[i]),
290                              0)) == CNID_INVALID) {
291             EC_FAIL;
292         }
293         EC_ZERO(bcatcstr(statpath, "/"));
294     }
295
296 EC_CLEANUP:
297     bdestroy(rpath);
298     bstrListDestroy(l);
299     bdestroy(statpath);
300     if (ret != 0)
301         return CNID_INVALID;
302
303     return cnid;
304 }
305
306 /*!
307  * Resolves CNID of a given paths parent directory
308  *
309  * path might be:
310  * (a) relative:
311  *     "dir/subdir" with cwd: "/afp_volume/topdir"
312  * (b) absolute:
313  *     "/afp_volume/dir/subdir"
314  *
315  * path MUST be pointing inside vol, this is usually the case as vol has been build from
316  * path using loadvolinfo and friends.
317  *
318  * @param vol  (r) pointer to afpvol_t
319  * @param path (r) path, see above
320  * @param did  (rw) parent CNID of returned CNID
321  *
322  * @returns CNID of path
323  */
324 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
325                              const char *path,
326                              cnid_t *did)
327 {
328     EC_INIT;
329
330     cnid_t cnid;
331     bstring rpath = NULL;
332     bstring statpath = NULL;
333     struct bstrList *l = NULL;
334     struct stat st;
335
336     *did = htonl(1);
337     cnid = htonl(2);
338
339     EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
340     EC_NULL(statpath = bfromcstr(vol->vol->v_path));
341
342     l = bsplit(rpath, '/');
343     if (l->qty == 1)
344         /* only one path element, means parent dir cnid is volume root = 2 */
345         goto EC_CLEANUP;
346     for (int i = 0; i < (l->qty - 1); i++) {
347         *did = cnid;
348         EC_ZERO(bconcat(statpath, l->entry[i]));
349         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
350                        "lstat(rpath: %s, elem: %s): %s: %s",
351                        cfrombstr(rpath), cfrombstr(l->entry[i]),
352                        cfrombstr(statpath), strerror(errno));
353
354         if ((cnid = cnid_add(vol->vol->v_cdb,
355                              &st,
356                              *did,
357                              cfrombstr(l->entry[i]),
358                              blength(l->entry[i]),
359                              0)) == CNID_INVALID) {
360             EC_FAIL;
361         }
362         EC_ZERO(bcatcstr(statpath, "/"));
363     }
364
365 EC_CLEANUP:
366     bdestroy(rpath);
367     bstrListDestroy(l);
368     bdestroy(statpath);
369     if (ret != 0)
370         return CNID_INVALID;
371
372     return cnid;
373 }
374