]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/volinfo.c
Revert change from 3194183
[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 static const vol_opt_name_t vol_opt_names[] = {
48     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
49     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
50     {AFPVOL_NOADOUBLE,  "NOADOUBLE"},   /* don't create .AppleDouble by default */
51     {AFPVOL_RO,         "READONLY"},    /* read-only volume */
52     {AFPVOL_MSWINDOWS,  "MSWINDOWS"},   /* deal with ms-windows yuckiness. this is going away. */
53     {AFPVOL_NOHEX,      "NOHEX"},       /* don't do :hex translation */
54     {AFPVOL_USEDOTS,    "USEDOTS"},     /* use real dots */
55     {AFPVOL_LIMITSIZE,  "LIMITSIZE"},   /* limit size for older macs */
56     {AFPVOL_MAPASCII,   "MAPASCII"},    /* map the ascii range as well */
57     {AFPVOL_DROPBOX,    "DROPBOX"},     /* dropkludge dropbox support */
58     {AFPVOL_NOFILEID,   "NOFILEID"},    /* don't advertise createid resolveid and deleteid calls */
59     {AFPVOL_NOSTAT,     "NOSTAT"},      /* advertise the volume even if we can't stat() it
60                                          * maybe because it will be mounted later in preexec */
61     {AFPVOL_UNIX_PRIV,  "UNIXPRIV"},    /* support unix privileges */
62     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
63     {AFPVOL_CASEINSEN,  "CASEINSENSITIVE"}, /* volume is case insensitive */
64     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},  /* encode illegal sequence */
65     {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
66     {AFPVOL_INV_DOTS,   "INVISIBLEDOTS"}, 
67     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
68     {AFPVOL_TM,         "TM"},          /* Set "kSupportsTMLockSteal" is volume attributes */
69     {0, NULL}
70 };
71
72 static const vol_opt_name_t vol_opt_casefold[] = {
73     {AFPVOL_MTOUUPPER,  "MTOULOWER"},
74     {AFPVOL_MTOULOWER,  "MTOULOWER"},
75     {AFPVOL_UTOMUPPER,  "UTOMUPPER"},
76     {AFPVOL_UTOMLOWER,  "UTOMLOWER"},
77     {0, NULL}
78 };
79
80 typedef struct {
81     const char *name;
82     int type;
83 } info_option_t;
84
85 #define MAC_CHARSET 0
86 #define VOL_CHARSET 1
87 #define ADOUBLE_VER 2
88 #define CNIDBACKEND 3
89 #define CNIDDBDHOST 4
90 #define CNIDDBDPORT 5
91 #define CNID_DBPATH 6
92 #define VOLUME_OPTS 7
93 #define VOLCASEFOLD 8
94 #define EXTATTRTYPE 9
95
96 static const info_option_t info_options[] = {
97     {"MAC_CHARSET", MAC_CHARSET},
98     {"VOL_CHARSET", VOL_CHARSET},
99     {"ADOUBLE_VER", ADOUBLE_VER},
100     {"CNIDBACKEND", CNIDBACKEND},
101     {"CNIDDBDHOST", CNIDDBDHOST},
102     {"CNIDDBDPORT", CNIDDBDPORT},
103     {"CNID_DBPATH", CNID_DBPATH},
104     {"VOLUME_OPTS", VOLUME_OPTS},
105     {"VOLCASEFOLD", VOLCASEFOLD},
106     {"EXTATTRTYPE", EXTATTRTYPE},
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     case EXTATTRTYPE:
310         if (strcasecmp(value, "AFPVOL_EA_AD") == 0)    
311             vol->v_vfs_ea = AFPVOL_EA_AD;
312         else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
313             vol->v_vfs_ea = AFPVOL_EA_SYS;
314         break;
315       default:
316         fprintf (stderr, "unknown volume information: %s, %s", buf, value);
317         return (-1);
318         break;
319     }
320         
321     return 0;
322 }
323     
324
325 int loadvolinfo (char *path, struct volinfo *vol)
326 {
327
328     char   volinfofile[MAXPATHLEN];
329     char   buf[MAXPATHLEN];
330     struct flock lock;
331     int    fd;
332     FILE   *fp;
333
334     if ( !path || !vol)
335         return -1;
336
337     memset(vol, 0, sizeof(struct volinfo));
338     strlcpy(volinfofile, path, sizeof(volinfofile));
339
340     /* volinfo file is in .AppleDesktop */ 
341     if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
342         return -1;
343
344     if ((vol->v_path = strdup(volinfofile)) == NULL ) {
345         fprintf (stderr, "strdup: %s", strerror(errno));
346         return (-1);
347     }
348     strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
349     strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
350
351     /* open the file read only */
352     if ( NULL == (fp = fopen( volinfofile, "r")) )  {
353         fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
354         return (-1);
355     }
356     fd = fileno(fp); 
357
358     /* try to get a read lock */ 
359     lock.l_start  = 0;
360     lock.l_whence = SEEK_SET;
361     lock.l_len    = 0;
362     lock.l_type   = F_RDLCK;
363
364     /* wait for read lock */
365     if (fcntl(fd, F_SETLKW, &lock) < 0) {
366         fclose(fp);
367         return (-1);
368     }
369
370     /* read the file */
371     while (NULL != fgets(buf, sizeof(buf), fp)) {
372         parseline(buf, vol);
373     }
374
375     /* unlock file */
376     lock.l_type = F_UNLCK;
377     fcntl(fd, F_SETLK, &lock);
378
379     /* Translate vol options to ad options like afp/volume.c does it */
380     vol->v_ad_options = 0;
381     if ((vol->v_flags & AFPVOL_NODEV))
382         vol->v_ad_options |= ADVOL_NODEV;
383     if ((vol->v_flags & AFPVOL_CACHE))
384         vol->v_ad_options |= ADVOL_CACHE;
385     if ((vol->v_flags & AFPVOL_UNIX_PRIV))
386         vol->v_ad_options |= ADVOL_UNIXPRIV;
387     if ((vol->v_flags & AFPVOL_INV_DOTS))
388         vol->v_ad_options |= ADVOL_INVDOTS;
389
390     fclose(fp);
391     return 0;
392 }
393
394 /*
395  * Save the volume options to a file, used by shell utilities. Writing the file
396  * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
397  */
398 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
399 {
400     char buf[16348];
401     char item[MAXPATHLEN];
402     int fd;
403     int ret = 0;
404     struct flock lock;
405     const vol_opt_name_t *op = &vol_opt_names[0];
406     const vol_opt_name_t *cf = &vol_opt_casefold[0];
407
408     strlcpy (item, vol->v_path, sizeof(item));
409     strlcat (item, "/.AppleDesktop/", sizeof(item));
410     strlcat (item, VOLINFOFILE, sizeof(item));
411
412     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
413         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
414         return (-1);
415     }
416
417     /* try to get a lock */
418     lock.l_start  = 0;
419     lock.l_whence = SEEK_SET;
420     lock.l_len    = 0;
421     lock.l_type   = F_WRLCK;
422
423     if (fcntl(fd, F_SETLK, &lock) < 0) {
424         if (errno == EACCES || errno == EAGAIN) {
425             /* ignore, other process already writing the file */
426             return 0;
427         } else {
428             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
429             return (-1);
430         }
431     }
432
433     /* write volume options */
434     snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
435     snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
436     strlcat(buf, item, sizeof(buf));
437
438     switch (vol->v_adouble) {
439         case AD_VERSION1:
440             strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
441             break;
442         case AD_VERSION2:
443             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
444             break;
445         case AD_VERSION2_OSX:
446             strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
447             break;
448         case AD_VERSION1_SFM:
449             strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
450             break;
451     }
452
453     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
454     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
455     strlcat(buf, "\n", sizeof(buf));
456
457     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
458     strlcat(buf, Cnid_srv, sizeof(buf));
459     strlcat(buf, "\n", sizeof(buf));
460
461     strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
462     strlcat(buf, Cnid_port, sizeof(buf));
463     strlcat(buf, "\n", sizeof(buf));
464
465     strcpy(item, "CNID_DBPATH:");
466     if (vol->v_dbpath)
467         strlcat(item, vol->v_dbpath, sizeof(item));
468     else
469         strlcat(item, vol->v_path, sizeof(item));
470     strlcat(item, "\n", sizeof(item));
471     strlcat(buf, item, sizeof(buf));
472
473     /* volume flags */
474     strcpy(item, "VOLUME_OPTS:");
475     for (;op->name; op++) {
476         if ( (vol->v_flags & op->option) ) {
477             strlcat(item, op->name, sizeof(item));
478             strlcat(item, " ", sizeof(item));
479         }
480     }
481     strlcat(item, "\n", sizeof(item));
482     strlcat(buf, item, sizeof(buf));
483
484     /* casefold flags */
485     strcpy(item, "VOLCASEFOLD:");
486     for (;cf->name; cf++) {
487         if ( (vol->v_casefold & cf->option) ) {
488             strlcat(item, cf->name, sizeof(item));
489             strlcat(item, " ", sizeof(item));
490         }
491     }
492     strlcat(item, "\n", sizeof(item));
493     strlcat(buf, item, sizeof(buf));
494
495     /* ExtendedAttributes */
496     strcpy(item, "EXTATTRTYPE:");
497     switch (vol->v_vfs_ea) {
498     case AFPVOL_EA_SYS:
499         strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
500         break;
501     case AFPVOL_EA_AD:
502         strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
503         break;
504     case AFPVOL_EA_NONE:
505         strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
506         break;
507     default:
508         strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
509     }
510
511     strlcat(buf, item, sizeof(buf));
512
513     if (strlen(buf) >= sizeof(buf)-1)
514         LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
515    if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
516        LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
517    }
518
519    lock.l_type = F_UNLCK;
520    fcntl(fd, F_SETLK, &lock);
521    close (fd);
522    return ret;
523 }