]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/volinfo.c
0c56f6f4125ebd7553ee3a79f9604d465a4a304e
[netatalk.git] / libatalk / util / volinfo.c
1 /*
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15
16    .volinfo file handling, command line utilities
17    copyright Bjoern Fernhomberg, 2004
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 #ifdef STDC_HEADERS
34 #include <string.h>
35 #endif
36 #include <sys/param.h>
37
38 #include <atalk/adouble.h>
39 #include <atalk/util.h>
40 #include <atalk/logger.h>
41 #include <atalk/volinfo.h>
42 #include <atalk/volume.h>
43 #ifdef CNID_DB
44 #include <atalk/cnid.h>
45 #endif /* CNID_DB*/
46
47 /* Global: eg volume.c needs em */
48
49 const vol_opt_name_t vol_opt_names[] = {
50     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
51     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
52     {AFPVOL_NOADOUBLE,  "NOADOUBLE"},   /* don't create .AppleDouble by default */
53     {AFPVOL_RO,         "READONLY"},    /* read-only volume */
54     {AFPVOL_MSWINDOWS,  "MSWINDOWS"},   /* deal with ms-windows yuckiness. this is going away. */
55     {AFPVOL_NOHEX,      "NOHEX"},       /* don't do :hex translation */
56     {AFPVOL_USEDOTS,    "USEDOTS"},     /* use real dots */
57     {AFPVOL_LIMITSIZE,  "LIMITSIZE"},   /* limit size for older macs */
58     {AFPVOL_MAPASCII,   "MAPASCII"},    /* map the ascii range as well */
59     {AFPVOL_DROPBOX,    "DROPBOX"},     /* dropkludge dropbox support */
60     {AFPVOL_NOFILEID,   "NOFILEID"},    /* don't advertise createid resolveid and deleteid calls */
61     {AFPVOL_NOSTAT,     "NOSTAT"},      /* advertise the volume even if we can't stat() it
62                                          * maybe because it will be mounted later in preexec */
63     {AFPVOL_UNIX_PRIV,  "UNIXPRIV"},    /* support unix privileges */
64     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
65     {AFPVOL_CASEINSEN,  "CASEINSENSITIVE"}, /* volume is case insensitive */
66     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},  /* encode illegal sequence */
67     {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
68     {AFPVOL_INV_DOTS,   "INVISIBLEDOTS"}, 
69     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
70     {AFPVOL_TM,         "TM"},          /* Set "kSupportsTMLockSteal" is volume attributes */
71     {0, NULL}
72 };
73
74 const vol_opt_name_t vol_opt_casefold[] = {
75     {AFPVOL_MTOUUPPER,  "MTOULOWER"},
76     {AFPVOL_MTOULOWER,  "MTOULOWER"},
77     {AFPVOL_UTOMUPPER,  "UTOMUPPER"},
78     {AFPVOL_UTOMLOWER,  "UTOMLOWER"},
79     {0, NULL}
80 };
81
82 typedef struct {
83     const char *name;
84     int type;
85 } info_option_t;
86
87 #define MAC_CHARSET 0
88 #define VOL_CHARSET 1
89 #define ADOUBLE_VER 2
90 #define CNIDBACKEND 3
91 #define CNIDDBDHOST 4
92 #define CNIDDBDPORT 5
93 #define CNID_DBPATH 6
94 #define VOLUME_OPTS 7
95 #define VOLCASEFOLD 8
96
97 static const info_option_t info_options[] = {
98     {"MAC_CHARSET", MAC_CHARSET},
99     {"VOL_CHARSET", VOL_CHARSET},
100     {"ADOUBLE_VER", ADOUBLE_VER},
101     {"CNIDBACKEND", CNIDBACKEND},
102     {"CNIDDBDHOST", CNIDDBDHOST},
103     {"CNIDDBDPORT", CNIDDBDPORT},
104     {"CNID_DBPATH", CNID_DBPATH},
105     {"VOLUME_OPTS", VOLUME_OPTS},
106     {"VOLCASEFOLD", VOLCASEFOLD},
107     {NULL, 0}
108 };
109
110 static char* find_in_path( char *path, char *subdir, size_t maxlen)
111 {
112     char        *pos;
113     struct stat st;
114
115     strlcat(path, subdir, maxlen);
116     pos = strrchr(path, '/');
117
118     while ( stat(path, &st) != 0) {
119         path[pos-path]=0;
120         if ((pos = strrchr(path, '/'))) {
121             path[pos-path]=0;
122             strlcat(path, subdir, maxlen);
123         }
124         else {
125             return NULL;
126         }
127     }
128
129     path[pos-path] = '/';
130     path[pos-path+1] = 0;
131
132     return path;
133 }
134
135 static char * make_path_absolute(char *path, size_t bufsize)
136 {
137     struct      stat st;
138     char        savecwd[MAXPATHLEN];
139     char        abspath[MAXPATHLEN];
140     char        *p;
141
142     if (stat(path, &st) != 0) {
143         return NULL;
144     }
145
146     strlcpy (abspath, path, sizeof(abspath));
147
148     if (!S_ISDIR(st.st_mode)) {
149         if (NULL == (p=strrchr(abspath, '/')) )
150             strcpy(abspath, ".");
151         else
152             *p = '\0';
153     }
154
155     if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)        
156         return NULL;
157
158     if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
159         return NULL;
160     
161     if (strlen(abspath) > bufsize)
162         return NULL;
163
164     strlcpy(path, abspath, bufsize);
165     return path;
166 }
167
168 static char * find_volumeroot(char *path, size_t maxlen)
169 {
170     char *volume = make_path_absolute(path, maxlen);
171         
172     if (volume == NULL)
173        return NULL;
174
175     if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
176        return NULL;
177
178     return volume;
179 }
180
181 int vol_load_charsets( struct volinfo *vol)
182 {
183     if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
184         fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
185         return (-1);
186     }
187
188     if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
189         fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
190         return (-1);
191     }
192
193     return 0;
194 }
195
196 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
197 {
198     char *p, *q;
199     const vol_opt_name_t *op;
200
201     q = p = buf; 
202
203     while ( *p != '\0') {
204         if (*p == ' ') {
205             *p = '\0';
206             op = options;
207             for (;op->name; op++) {
208                 if ( !strcmp(op->name, q )) {
209                     *flags |= op->option;
210                     break;
211                 }
212             }
213             q = p+1;
214         }
215         p++;
216     }
217
218     return 0;
219
220             
221
222
223 static int parseline ( char *buf, struct volinfo *vol)
224 {
225     char *value;
226     size_t len;
227     int  option=-1;
228     const info_option_t  *p  = &info_options[0];
229
230     if (NULL == ( value = strchr(buf, ':')) )
231         return 1;
232
233     *value = '\0';
234     value++;
235
236     if ( 0 == (len = strlen(value)) )
237         return 0;
238
239     if (value[len-1] == '\n')
240         value[len-1] = '\0';
241
242     for (;p->name; p++) {
243         if ( !strcmp(p->name, buf )) {
244             option=p->type;
245             break;
246         }
247     }
248
249     switch (option) {
250       case MAC_CHARSET:
251         if ((vol->v_maccodepage = strdup(value)) == NULL) {
252             fprintf (stderr, "strdup: %s", strerror(errno));
253             return -1;
254         }
255         break;
256       case VOL_CHARSET:
257         if ((vol->v_volcodepage = strdup(value)) == NULL) {
258             fprintf (stderr, "strdup: %s", strerror(errno));
259             return -1;
260         }
261         break;
262       case CNIDBACKEND:
263         if ((vol->v_cnidscheme = strdup(value)) == NULL) {
264             fprintf (stderr, "strdup: %s", strerror(errno));
265             return -1;
266         }
267         break;
268       case CNIDDBDHOST:
269         if ((vol->v_dbd_host = strdup(value)) == NULL) {
270             fprintf (stderr, "strdup: %s", strerror(errno));
271             return -1;
272         }
273         break;
274       case CNIDDBDPORT:
275         vol->v_dbd_port = atoi(value);
276         break;
277       case CNID_DBPATH:
278         if ((vol->v_dbpath = strdup(value)) == NULL) {
279             fprintf (stderr, "strdup: %s", strerror(errno));
280             return -1;
281         }
282         break;
283       case ADOUBLE_VER:
284         if (strcasecmp(value, "v1") == 0) {
285             vol->v_adouble = AD_VERSION1;
286             vol->ad_path = ad_path;
287         }
288 #if AD_VERSION == AD_VERSION2
289         else if (strcasecmp(value, "v2") == 0) {
290             vol->ad_path = ad_path;
291             vol->v_adouble = AD_VERSION2;
292         }
293         else if (strcasecmp(value, "osx") == 0) {
294             vol->v_adouble = AD_VERSION2_OSX;
295             vol->ad_path = ad_path_osx;
296         }
297 #endif
298         else  {
299             fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
300             return -1;
301         }
302         break;
303       case VOLUME_OPTS:
304         parse_options(value, &vol->v_flags, &vol_opt_names[0]);
305         break;
306       case VOLCASEFOLD:
307         parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
308         break;
309       default:
310         fprintf (stderr, "unknown volume information: %s, %s", buf, value);
311         return (-1);
312         break;
313     }
314         
315     return 0;
316 }
317     
318
319 int loadvolinfo (char *path, struct volinfo *vol)
320 {
321
322     char   volinfofile[MAXPATHLEN];
323     char   buf[MAXPATHLEN];
324     struct flock lock;
325     int    fd;
326     FILE   *fp;
327
328     if ( !path || !vol)
329         return -1;
330
331     memset(vol, 0, sizeof(struct volinfo));
332     strlcpy(volinfofile, path, sizeof(volinfofile));
333
334     /* volinfo file is in .AppleDesktop */ 
335     if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
336         return -1;
337
338     if ((vol->v_path = strdup(volinfofile)) == NULL ) {
339         fprintf (stderr, "strdup: %s", strerror(errno));
340         return (-1);
341     }
342     strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
343     strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
344
345     /* open the file read only */
346     if ( NULL == (fp = fopen( volinfofile, "r")) )  {
347         fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
348         return (-1);
349     }
350     fd = fileno(fp); 
351
352     /* try to get a read lock */ 
353     lock.l_start  = 0;
354     lock.l_whence = SEEK_SET;
355     lock.l_len    = 0;
356     lock.l_type   = F_RDLCK;
357
358     /* wait for read lock */
359     if (fcntl(fd, F_SETLKW, &lock) < 0) {
360         fclose(fp);
361         return (-1);
362     }
363
364     /* read the file */
365     while (NULL != fgets(buf, sizeof(buf), fp)) {
366         parseline(buf, vol);
367     }
368
369     /* unlock file */
370     lock.l_type = F_UNLCK;
371     fcntl(fd, F_SETLK, &lock);
372
373     /* Translate vol options to ad options like afp/volume.c does it */
374     vol->v_ad_options = 0;
375     if ((vol->v_flags & AFPVOL_NODEV))
376         vol->v_ad_options |= ADVOL_NODEV;
377     if ((vol->v_flags & AFPVOL_CACHE))
378         vol->v_ad_options |= ADVOL_CACHE;
379     if ((vol->v_flags & AFPVOL_UNIX_PRIV))
380         vol->v_ad_options |= ADVOL_UNIXPRIV;
381     if ((vol->v_flags & AFPVOL_INV_DOTS))
382         vol->v_ad_options |= ADVOL_INVDOTS;
383
384     fclose(fp);
385     return 0;
386 }
387
388 /*
389  * Save the volume options to a file, used by shell utilities. Writing the file
390  * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
391  */
392 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
393 {
394     char buf[16348];
395     char item[MAXPATHLEN];
396     int fd;
397     int ret = 0;
398     struct flock lock;
399     const vol_opt_name_t *op = &vol_opt_names[0];
400     const vol_opt_name_t *cf = &vol_opt_casefold[0];
401
402     strlcpy (item, vol->v_path, sizeof(item));
403     strlcat (item, "/.AppleDesktop/", sizeof(item));
404     strlcat (item, VOLINFOFILE, sizeof(item));
405
406     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
407         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
408         return (-1);
409     }
410
411     /* try to get a lock */
412     lock.l_start  = 0;
413     lock.l_whence = SEEK_SET;
414     lock.l_len    = 0;
415     lock.l_type   = F_WRLCK;
416
417     if (fcntl(fd, F_SETLK, &lock) < 0) {
418         if (errno == EACCES || errno == EAGAIN) {
419             /* ignore, other process already writing the file */
420             return 0;
421         } else {
422             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
423             return (-1);
424         }
425     }
426
427     /* write volume options */
428     snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
429     snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
430     strlcat(buf, item, sizeof(buf));
431
432     switch (vol->v_adouble) {
433         case AD_VERSION1:
434             strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
435             break;
436         case AD_VERSION2:
437             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
438             break;
439         case AD_VERSION2_OSX:
440             strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
441             break;
442         case AD_VERSION1_SFM:
443             strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
444             break;
445     }
446
447     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
448     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
449     strlcat(buf, "\n", sizeof(buf));
450
451     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
452     strlcat(buf, Cnid_srv, sizeof(buf));
453     strlcat(buf, "\n", sizeof(buf));
454
455     strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
456     strlcat(buf, Cnid_port, sizeof(buf));
457     strlcat(buf, "\n", sizeof(buf));
458
459     strcpy(item, "CNID_DBPATH:");
460     if (vol->v_dbpath)
461         strlcat(item, vol->v_dbpath, sizeof(item));
462     else
463         strlcat(item, vol->v_path, sizeof(item));
464     strlcat(item, "\n", sizeof(item));
465     strlcat(buf, item, sizeof(buf));
466
467     /* volume flags */
468     strcpy(item, "VOLUME_OPTS:");
469     for (;op->name; op++) {
470         if ( (vol->v_flags & op->option) ) {
471             strlcat(item, op->name, sizeof(item));
472             strlcat(item, " ", sizeof(item));
473         }
474     }
475     strlcat(item, "\n", sizeof(item));
476     strlcat(buf, item, sizeof(buf));
477
478     /* casefold flags */
479     strcpy(item, "VOLCASEFOLD:");
480     for (;cf->name; cf++) {
481         if ( (vol->v_casefold & cf->option) ) {
482             strlcat(item, cf->name, sizeof(item));
483             strlcat(item, " ", sizeof(item));
484         }
485     }
486     strlcat(item, "\n", sizeof(item));
487     strlcat(buf, item, sizeof(buf));
488
489     if (strlen(buf) >= sizeof(buf)-1)
490         LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
491
492
493    if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
494        LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
495    }
496
497    lock.l_type = F_UNLCK;
498    fcntl(fd, F_SETLK, &lock);
499    close (fd);
500    return ret;
501 }