]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/volinfo.c
Merge master
[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 #include <string.h>
31 #include <sys/param.h>
32
33 #include <atalk/adouble.h>
34 #include <atalk/util.h>
35 #include <atalk/logger.h>
36 #include <atalk/volinfo.h>
37 #include <atalk/volume.h>
38 #include <atalk/compat.h>
39 #ifdef CNID_DB
40 #include <atalk/cnid.h>
41 #endif /* CNID_DB*/
42
43 static const vol_opt_name_t vol_opt_names[] = {
44     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
45     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
46     {AFPVOL_NOADOUBLE,  "NOADOUBLE"},   /* don't create .AppleDouble by default */
47     {AFPVOL_RO,         "READONLY"},    /* read-only volume */
48     {AFPVOL_MSWINDOWS,  "MSWINDOWS"},   /* deal with ms-windows yuckiness. this is going away. */
49     {AFPVOL_NOHEX,      "NOHEX"},       /* don't do :hex translation */
50     {AFPVOL_USEDOTS,    "USEDOTS"},     /* use real dots */
51     {AFPVOL_LIMITSIZE,  "LIMITSIZE"},   /* limit size for older macs */
52     {AFPVOL_MAPASCII,   "MAPASCII"},    /* map the ascii range as well */
53     {AFPVOL_NOFILEID,   "NOFILEID"},    /* don't advertise createid resolveid and deleteid calls */
54     {AFPVOL_NOSTAT,     "NOSTAT"},      /* advertise the volume even if we can't stat() it
55                                          * maybe because it will be mounted later in preexec */
56     {AFPVOL_UNIX_PRIV,  "UNIXPRIV"},    /* support unix privileges */
57     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
58     {AFPVOL_CASEINSEN,  "CASEINSENSITIVE"}, /* volume is case insensitive */
59     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},  /* encode illegal sequence */
60     {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
61     {AFPVOL_INV_DOTS,   "INVISIBLEDOTS"}, 
62     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
63     {AFPVOL_TM,         "TM"},          /* Set "kSupportsTMLockSteal" is volume attributes */
64     {0, NULL}
65 };
66
67 static const vol_opt_name_t vol_opt_casefold[] = {
68     {AFPVOL_MTOUUPPER,  "MTOULOWER"},
69     {AFPVOL_MTOULOWER,  "MTOULOWER"},
70     {AFPVOL_UTOMUPPER,  "UTOMUPPER"},
71     {AFPVOL_UTOMLOWER,  "UTOMLOWER"},
72     {0, NULL}
73 };
74
75 typedef struct {
76     const char *name;
77     int type;
78 } info_option_t;
79
80 #define MAC_CHARSET 0
81 #define VOL_CHARSET 1
82 #define ADOUBLE_VER 2
83 #define CNIDBACKEND 3
84 #define CNIDDBDHOST 4
85 #define CNIDDBDPORT 5
86 #define CNID_DBPATH 6
87 #define VOLUME_OPTS 7
88 #define VOLCASEFOLD 8
89 #define EXTATTRTYPE 9
90
91 static const info_option_t info_options[] = {
92     {"MAC_CHARSET", MAC_CHARSET},
93     {"VOL_CHARSET", VOL_CHARSET},
94     {"ADOUBLE_VER", ADOUBLE_VER},
95     {"CNIDBACKEND", CNIDBACKEND},
96     {"CNIDDBDHOST", CNIDDBDHOST},
97     {"CNIDDBDPORT", CNIDDBDPORT},
98     {"CNID_DBPATH", CNID_DBPATH},
99     {"VOLUME_OPTS", VOLUME_OPTS},
100     {"VOLCASEFOLD", VOLCASEFOLD},
101     {"EXTATTRTYPE", EXTATTRTYPE},
102    {NULL, 0}
103 };
104
105 static char* find_in_path( char *path, char *subdir, size_t maxlen)
106 {
107     char        *pos;
108     struct stat st;
109
110     strlcat(path, subdir, maxlen);
111     pos = strrchr(path, '/');
112
113     while ( stat(path, &st) != 0) {
114         path[pos-path]=0;
115         if ((pos = strrchr(path, '/'))) {
116             path[pos-path]=0;
117             strlcat(path, subdir, maxlen);
118         }
119         else {
120             return NULL;
121         }
122     }
123
124     path[pos-path] = '/';
125     path[pos-path+1] = 0;
126
127     return path;
128 }
129
130 static char * make_path_absolute(char *path, size_t bufsize)
131 {
132     struct      stat st;
133     char        savecwd[MAXPATHLEN];
134     char        abspath[MAXPATHLEN];
135     char        *p;
136
137     strlcpy(abspath, path, sizeof(abspath));
138
139     /* we might be called from `ad cp ...` with non existing target */
140     if (stat(abspath, &st) != 0) {
141         if (errno != ENOENT)
142             return NULL;
143
144         if (NULL == (p = strrchr(abspath, '/')) )
145             /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
146             strcpy(abspath, ".");
147         else
148             /* try without the last path element */
149             *p = '\0';
150
151         if (stat(abspath, &st) != 0) {
152             return NULL;
153         }
154     }
155
156     if (S_ISREG(st.st_mode)) {
157         /* single file copy SOURCE */
158         if (NULL == (p = strrchr(abspath, '/')) )
159             /* no path, use "." instead */
160             strcpy(abspath, ".");
161         else
162             /* try without the last path element */
163             *p = '\0';
164     }
165
166     if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)        
167         return NULL;
168
169     if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
170         return NULL;
171     
172     if (strlen(abspath) > bufsize)
173         return NULL;
174
175     strlcpy(path, abspath, bufsize);
176     return path;
177 }
178
179 static char * find_volumeroot(char *path, size_t maxlen)
180 {
181     char *volume = make_path_absolute(path, maxlen);
182         
183     if (volume == NULL)
184        return NULL;
185
186     if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
187        return NULL;
188
189     return volume;
190 }
191
192 int vol_load_charsets( struct volinfo *vol)
193 {
194     if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
195         fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
196         return (-1);
197     }
198
199     if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
200         fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
201         return (-1);
202     }
203
204     return 0;
205 }
206
207 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
208 {
209     char *p, *q;
210     const vol_opt_name_t *op;
211
212     q = p = buf; 
213
214     while ( *p != '\0') {
215         if (*p == ' ') {
216             *p = '\0';
217             op = options;
218             for (;op->name; op++) {
219                 if ( !strcmp(op->name, q )) {
220                     *flags |= op->option;
221                     break;
222                 }
223             }
224             q = p+1;
225         }
226         p++;
227     }
228
229     return 0;
230
231             
232
233
234 static int parseline ( char *buf, struct volinfo *vol)
235 {
236     char *value;
237     size_t len;
238     int  option=-1;
239     const info_option_t  *p  = &info_options[0];
240
241     if (NULL == ( value = strchr(buf, ':')) )
242         return 1;
243
244     *value = '\0';
245     value++;
246
247     if ( 0 == (len = strlen(value)) )
248         return 0;
249
250     if (value[len-1] == '\n')
251         value[len-1] = '\0';
252
253     for (;p->name; p++) {
254         if ( !strcmp(p->name, buf )) {
255             option=p->type;
256             break;
257         }
258     }
259
260     switch (option) {
261       case MAC_CHARSET:
262         if ((vol->v_maccodepage = strdup(value)) == NULL) {
263             fprintf (stderr, "strdup: %s", strerror(errno));
264             return -1;
265         }
266         break;
267       case VOL_CHARSET:
268         if ((vol->v_volcodepage = strdup(value)) == NULL) {
269             fprintf (stderr, "strdup: %s", strerror(errno));
270             return -1;
271         }
272         break;
273       case CNIDBACKEND:
274         if ((vol->v_cnidscheme = strdup(value)) == NULL) {
275             fprintf (stderr, "strdup: %s", strerror(errno));
276             return -1;
277         }
278         break;
279       case CNIDDBDHOST:
280         if ((vol->v_dbd_host = strdup(value)) == NULL) {
281             fprintf (stderr, "strdup: %s", strerror(errno));
282             return -1;
283         }
284         break;
285       case CNIDDBDPORT:
286         if ((vol->v_dbd_port = strdup(value)) == NULL) {
287             fprintf (stderr, "strdup: %s", strerror(errno));
288             return -1;            
289         }
290         break;
291       case CNID_DBPATH:
292           if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
293               return -1;
294           strcpy(vol->v_dbpath, value);
295         break;
296       case ADOUBLE_VER:
297         if (strcasecmp(value, "v2") == 0) {
298             vol->ad_path = ad_path;
299             vol->v_adouble = AD_VERSION2;
300         } else if (strcasecmp(value, "ea") == 0) {
301             vol->ad_path = ad_path_ea;
302             vol->v_adouble = AD_VERSION_EA;
303         } else {
304
305             fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
306             return -1;
307         }
308         break;
309       case VOLUME_OPTS:
310         parse_options(value, &vol->v_flags, &vol_opt_names[0]);
311         break;
312       case VOLCASEFOLD:
313         parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
314         break;
315     case EXTATTRTYPE:
316         if (strcasecmp(value, "AFPVOL_EA_AD") == 0)    
317             vol->v_vfs_ea = AFPVOL_EA_AD;
318         else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
319             vol->v_vfs_ea = AFPVOL_EA_SYS;
320         break;
321       default:
322         fprintf (stderr, "unknown volume information: %s, %s", buf, value);
323         return (-1);
324         break;
325     }
326         
327     return 0;
328 }
329     
330
331 int loadvolinfo (char *path, struct volinfo *vol)
332 {
333
334     char   volinfofile[MAXPATHLEN];
335     char   buf[MAXPATHLEN];
336     struct flock lock;
337     int    fd, len;
338     FILE   *fp;
339
340     if ( !path || !vol)
341         return -1;
342
343     memset(vol, 0, sizeof(struct volinfo));
344     strlcpy(volinfofile, path, sizeof(volinfofile));
345
346     /* volinfo file is in .AppleDesktop */ 
347     if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
348         return -1;
349
350     if ((vol->v_path = strdup(volinfofile)) == NULL ) {
351         fprintf (stderr, "strdup: %s", strerror(errno));
352         return (-1);
353     }
354     /* Remove trailing slashes */
355     len = strlen(vol->v_path);
356     while (len && (vol->v_path[len-1] == '/')) {
357         vol->v_path[len-1] = 0;
358         len--;
359     }
360
361     strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
362     strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
363
364     /* open the file read only */
365     if ( NULL == (fp = fopen( volinfofile, "r")) )  {
366         fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
367         return (-1);
368     }
369     fd = fileno(fp); 
370
371     /* try to get a read lock */ 
372     lock.l_start  = 0;
373     lock.l_whence = SEEK_SET;
374     lock.l_len    = 0;
375     lock.l_type   = F_RDLCK;
376
377     /* wait for read lock */
378     if (fcntl(fd, F_SETLKW, &lock) < 0) {
379         fclose(fp);
380         return (-1);
381     }
382
383     /* read the file */
384     while (NULL != fgets(buf, sizeof(buf), fp)) {
385         parseline(buf, vol);
386     }
387
388     /* unlock file */
389     lock.l_type = F_UNLCK;
390     fcntl(fd, F_SETLK, &lock);
391
392     /* Translate vol options to ad options like afp/volume.c does it */
393     vol->v_ad_options = 0;
394     if ((vol->v_flags & AFPVOL_NODEV))
395         vol->v_ad_options |= ADVOL_NODEV;
396     if ((vol->v_flags & AFPVOL_CACHE))
397         vol->v_ad_options |= ADVOL_CACHE;
398     if ((vol->v_flags & AFPVOL_UNIX_PRIV))
399         vol->v_ad_options |= ADVOL_UNIXPRIV;
400     if ((vol->v_flags & AFPVOL_INV_DOTS))
401         vol->v_ad_options |= ADVOL_INVDOTS;
402
403     vol->retaincount = 1;
404
405     fclose(fp);
406     return 0;
407 }
408
409 /*!
410  * Allocate a struct volinfo object for refcounting usage with retain and close, and
411  * call loadvolinfo with it
412  */
413 struct volinfo *allocvolinfo(char *path)
414 {
415     struct volinfo *p = malloc(sizeof(struct volinfo));
416     if (p == NULL)
417         return NULL;
418
419     if (loadvolinfo(path, p) == -1)
420         return NULL;
421
422     p->malloced = 1;
423
424     return p;
425 }
426
427 void retainvolinfo(struct volinfo *vol)
428 {
429     vol->retaincount++;
430 }
431
432 /*!
433  * Decrement retain count, free resources when retaincount reaches 0
434  */
435 int closevolinfo(struct volinfo *volinfo)
436 {
437     if (volinfo->retaincount <= 0)
438         abort();
439
440     volinfo->retaincount--;
441
442     if (volinfo->retaincount == 0) {
443         free(volinfo->v_name); volinfo->v_name = NULL;
444         free(volinfo->v_path); volinfo->v_path = NULL;
445         free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
446         free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
447         free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
448         free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
449         free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
450         free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
451         if (volinfo->malloced) {
452             volinfo->malloced = 0;
453             free(volinfo);
454         }
455     }
456
457     return 0;
458 }
459
460 /*
461  * Save the volume options to a file, used by shell utilities. Writing the file
462  * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
463  */
464 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
465 {
466     char buf[16348];
467     char item[MAXPATHLEN];
468     int fd;
469     int ret = 0;
470     struct flock lock;
471     const vol_opt_name_t *op = &vol_opt_names[0];
472     const vol_opt_name_t *cf = &vol_opt_casefold[0];
473
474     strlcpy (item, vol->v_path, sizeof(item));
475     strlcat (item, "/.AppleDesktop/", sizeof(item));
476     strlcat (item, VOLINFOFILE, sizeof(item));
477
478     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
479         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
480         return (-1);
481     }
482
483     /* try to get a lock */
484     lock.l_start  = 0;
485     lock.l_whence = SEEK_SET;
486     lock.l_len    = 0;
487     lock.l_type   = F_WRLCK;
488
489     if (fcntl(fd, F_SETLK, &lock) < 0) {
490         if (errno == EACCES || errno == EAGAIN) {
491             /* ignore, other process already writing the file */
492             return 0;
493         } else {
494             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
495             return (-1);
496         }
497     }
498
499     /* write volume options */
500     snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
501     snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
502     strlcat(buf, item, sizeof(buf));
503
504     switch (vol->v_adouble) {
505         case AD_VERSION2:
506             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
507             break;
508         case AD_VERSION_EA:
509             strlcat(buf, "ADOUBLE_VER:ea\n", sizeof(buf));
510             break;
511     }
512
513     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
514     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
515     strlcat(buf, "\n", sizeof(buf));
516
517     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
518     strlcat(buf, Cnid_srv, sizeof(buf));
519     strlcat(buf, "\n", sizeof(buf));
520
521     strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
522     strlcat(buf, Cnid_port, sizeof(buf));
523     strlcat(buf, "\n", sizeof(buf));
524
525     strcpy(item, "CNID_DBPATH:");
526     if (vol->v_dbpath)
527         strlcat(item, vol->v_dbpath, sizeof(item));
528     else
529         strlcat(item, vol->v_path, sizeof(item));
530     strlcat(item, "\n", sizeof(item));
531     strlcat(buf, item, sizeof(buf));
532
533     /* volume flags */
534     strcpy(item, "VOLUME_OPTS:");
535     for (;op->name; op++) {
536         if ( (vol->v_flags & op->option) ) {
537             strlcat(item, op->name, sizeof(item));
538             strlcat(item, " ", sizeof(item));
539         }
540     }
541     strlcat(item, "\n", sizeof(item));
542     strlcat(buf, item, sizeof(buf));
543
544     /* casefold flags */
545     strcpy(item, "VOLCASEFOLD:");
546     for (;cf->name; cf++) {
547         if ( (vol->v_casefold & cf->option) ) {
548             strlcat(item, cf->name, sizeof(item));
549             strlcat(item, " ", sizeof(item));
550         }
551     }
552     strlcat(item, "\n", sizeof(item));
553     strlcat(buf, item, sizeof(buf));
554
555     /* ExtendedAttributes */
556     strcpy(item, "EXTATTRTYPE:");
557     switch (vol->v_vfs_ea) {
558     case AFPVOL_EA_SYS:
559         strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
560         break;
561     case AFPVOL_EA_AD:
562         strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
563         break;
564     case AFPVOL_EA_NONE:
565         strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
566         break;
567     default:
568         strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
569     }
570
571     strlcat(buf, item, sizeof(buf));
572
573     if (strlen(buf) >= sizeof(buf)-1)
574         LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
575    if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
576        LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
577    }
578
579    lock.l_type = F_UNLCK;
580    fcntl(fd, F_SETLK, &lock);
581    close (fd);
582    return ret;
583 }