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