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