]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/volinfo.c
Fix ad_open, ad_flush, ad_close, VFS stuff and volinfo for adouble:ea, also deactivat...
[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     strlcpy(abspath, path, sizeof(abspath));
143
144     /* we might be called from `ad cp ...` with non existing target */
145     if (stat(abspath, &st) != 0) {
146         if (errno != ENOENT)
147             return NULL;
148
149         if (NULL == (p = strrchr(abspath, '/')) )
150             /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
151             strcpy(abspath, ".");
152         else
153             /* try without the last path element */
154             *p = '\0';
155
156         if (stat(abspath, &st) != 0) {
157             return NULL;
158         }
159     }
160
161     if (S_ISREG(st.st_mode)) {
162         /* single file copy SOURCE */
163         if (NULL == (p = strrchr(abspath, '/')) )
164             /* no path, use "." instead */
165             strcpy(abspath, ".");
166         else
167             /* try without the last path element */
168             *p = '\0';
169     }
170
171     if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)        
172         return NULL;
173
174     if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
175         return NULL;
176     
177     if (strlen(abspath) > bufsize)
178         return NULL;
179
180     strlcpy(path, abspath, bufsize);
181     return path;
182 }
183
184 static char * find_volumeroot(char *path, size_t maxlen)
185 {
186     char *volume = make_path_absolute(path, maxlen);
187         
188     if (volume == NULL)
189        return NULL;
190
191     if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
192        return NULL;
193
194     return volume;
195 }
196
197 int vol_load_charsets( struct volinfo *vol)
198 {
199     if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
200         fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
201         return (-1);
202     }
203
204     if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
205         fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
206         return (-1);
207     }
208
209     return 0;
210 }
211
212 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
213 {
214     char *p, *q;
215     const vol_opt_name_t *op;
216
217     q = p = buf; 
218
219     while ( *p != '\0') {
220         if (*p == ' ') {
221             *p = '\0';
222             op = options;
223             for (;op->name; op++) {
224                 if ( !strcmp(op->name, q )) {
225                     *flags |= op->option;
226                     break;
227                 }
228             }
229             q = p+1;
230         }
231         p++;
232     }
233
234     return 0;
235
236             
237
238
239 static int parseline ( char *buf, struct volinfo *vol)
240 {
241     char *value;
242     size_t len;
243     int  option=-1;
244     const info_option_t  *p  = &info_options[0];
245
246     if (NULL == ( value = strchr(buf, ':')) )
247         return 1;
248
249     *value = '\0';
250     value++;
251
252     if ( 0 == (len = strlen(value)) )
253         return 0;
254
255     if (value[len-1] == '\n')
256         value[len-1] = '\0';
257
258     for (;p->name; p++) {
259         if ( !strcmp(p->name, buf )) {
260             option=p->type;
261             break;
262         }
263     }
264
265     switch (option) {
266       case MAC_CHARSET:
267         if ((vol->v_maccodepage = strdup(value)) == NULL) {
268             fprintf (stderr, "strdup: %s", strerror(errno));
269             return -1;
270         }
271         break;
272       case VOL_CHARSET:
273         if ((vol->v_volcodepage = strdup(value)) == NULL) {
274             fprintf (stderr, "strdup: %s", strerror(errno));
275             return -1;
276         }
277         break;
278       case CNIDBACKEND:
279         if ((vol->v_cnidscheme = strdup(value)) == NULL) {
280             fprintf (stderr, "strdup: %s", strerror(errno));
281             return -1;
282         }
283         break;
284       case CNIDDBDHOST:
285         if ((vol->v_dbd_host = strdup(value)) == NULL) {
286             fprintf (stderr, "strdup: %s", strerror(errno));
287             return -1;
288         }
289         break;
290       case CNIDDBDPORT:
291         if ((vol->v_dbd_port = strdup(value)) == NULL) {
292             fprintf (stderr, "strdup: %s", strerror(errno));
293             return -1;            
294         }
295         break;
296       case CNID_DBPATH:
297         if ((vol->v_dbpath = strdup(value)) == NULL) {
298             fprintf (stderr, "strdup: %s", strerror(errno));
299             return -1;
300         }
301         break;
302       case ADOUBLE_VER:
303         if (strcasecmp(value, "v2") == 0) {
304             vol->ad_path = ad_path;
305             vol->v_adouble = AD_VERSION2;
306         } else if (strcasecmp(value, "ea") == 0) {
307             vol->ad_path = ad_path_ea;
308             vol->v_adouble = AD_VERSION_EA;
309         } else {
310
311             fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
312             return -1;
313         }
314         break;
315       case VOLUME_OPTS:
316         parse_options(value, &vol->v_flags, &vol_opt_names[0]);
317         break;
318       case VOLCASEFOLD:
319         parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
320         break;
321     case EXTATTRTYPE:
322         if (strcasecmp(value, "AFPVOL_EA_AD") == 0)    
323             vol->v_vfs_ea = AFPVOL_EA_AD;
324         else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
325             vol->v_vfs_ea = AFPVOL_EA_SYS;
326         break;
327       default:
328         fprintf (stderr, "unknown volume information: %s, %s", buf, value);
329         return (-1);
330         break;
331     }
332         
333     return 0;
334 }
335     
336
337 int loadvolinfo (char *path, struct volinfo *vol)
338 {
339
340     char   volinfofile[MAXPATHLEN];
341     char   buf[MAXPATHLEN];
342     struct flock lock;
343     int    fd;
344     FILE   *fp;
345
346     if ( !path || !vol)
347         return -1;
348
349     memset(vol, 0, sizeof(struct volinfo));
350     strlcpy(volinfofile, path, sizeof(volinfofile));
351
352     /* volinfo file is in .AppleDesktop */ 
353     if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
354         return -1;
355
356     if ((vol->v_path = strdup(volinfofile)) == NULL ) {
357         fprintf (stderr, "strdup: %s", strerror(errno));
358         return (-1);
359     }
360     strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
361     strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
362
363     /* open the file read only */
364     if ( NULL == (fp = fopen( volinfofile, "r")) )  {
365         fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
366         return (-1);
367     }
368     fd = fileno(fp); 
369
370     /* try to get a read lock */ 
371     lock.l_start  = 0;
372     lock.l_whence = SEEK_SET;
373     lock.l_len    = 0;
374     lock.l_type   = F_RDLCK;
375
376     /* wait for read lock */
377     if (fcntl(fd, F_SETLKW, &lock) < 0) {
378         fclose(fp);
379         return (-1);
380     }
381
382     /* read the file */
383     while (NULL != fgets(buf, sizeof(buf), fp)) {
384         parseline(buf, vol);
385     }
386
387     /* unlock file */
388     lock.l_type = F_UNLCK;
389     fcntl(fd, F_SETLK, &lock);
390
391     /* Translate vol options to ad options like afp/volume.c does it */
392     vol->v_ad_options = 0;
393     if ((vol->v_flags & AFPVOL_NODEV))
394         vol->v_ad_options |= ADVOL_NODEV;
395     if ((vol->v_flags & AFPVOL_CACHE))
396         vol->v_ad_options |= ADVOL_CACHE;
397     if ((vol->v_flags & AFPVOL_UNIX_PRIV))
398         vol->v_ad_options |= ADVOL_UNIXPRIV;
399     if ((vol->v_flags & AFPVOL_INV_DOTS))
400         vol->v_ad_options |= ADVOL_INVDOTS;
401
402     fclose(fp);
403     return 0;
404 }
405
406 /*
407  * Save the volume options to a file, used by shell utilities. Writing the file
408  * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
409  */
410 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
411 {
412     char buf[16348];
413     char item[MAXPATHLEN];
414     int fd;
415     int ret = 0;
416     struct flock lock;
417     const vol_opt_name_t *op = &vol_opt_names[0];
418     const vol_opt_name_t *cf = &vol_opt_casefold[0];
419
420     strlcpy (item, vol->v_path, sizeof(item));
421     strlcat (item, "/.AppleDesktop/", sizeof(item));
422     strlcat (item, VOLINFOFILE, sizeof(item));
423
424     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
425         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
426         return (-1);
427     }
428
429     /* try to get a lock */
430     lock.l_start  = 0;
431     lock.l_whence = SEEK_SET;
432     lock.l_len    = 0;
433     lock.l_type   = F_WRLCK;
434
435     if (fcntl(fd, F_SETLK, &lock) < 0) {
436         if (errno == EACCES || errno == EAGAIN) {
437             /* ignore, other process already writing the file */
438             return 0;
439         } else {
440             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
441             return (-1);
442         }
443     }
444
445     /* write volume options */
446     snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
447     snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
448     strlcat(buf, item, sizeof(buf));
449
450     switch (vol->v_adouble) {
451         case AD_VERSION2:
452             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
453             break;
454         case AD_VERSION_EA:
455             strlcat(buf, "ADOUBLE_VER:ea\n", sizeof(buf));
456             break;
457     }
458
459     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
460     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
461     strlcat(buf, "\n", sizeof(buf));
462
463     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
464     strlcat(buf, Cnid_srv, sizeof(buf));
465     strlcat(buf, "\n", sizeof(buf));
466
467     strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
468     strlcat(buf, Cnid_port, sizeof(buf));
469     strlcat(buf, "\n", sizeof(buf));
470
471     strcpy(item, "CNID_DBPATH:");
472     if (vol->v_dbpath)
473         strlcat(item, vol->v_dbpath, sizeof(item));
474     else
475         strlcat(item, vol->v_path, sizeof(item));
476     strlcat(item, "\n", sizeof(item));
477     strlcat(buf, item, sizeof(buf));
478
479     /* volume flags */
480     strcpy(item, "VOLUME_OPTS:");
481     for (;op->name; op++) {
482         if ( (vol->v_flags & op->option) ) {
483             strlcat(item, op->name, sizeof(item));
484             strlcat(item, " ", sizeof(item));
485         }
486     }
487     strlcat(item, "\n", sizeof(item));
488     strlcat(buf, item, sizeof(buf));
489
490     /* casefold flags */
491     strcpy(item, "VOLCASEFOLD:");
492     for (;cf->name; cf++) {
493         if ( (vol->v_casefold & cf->option) ) {
494             strlcat(item, cf->name, sizeof(item));
495             strlcat(item, " ", sizeof(item));
496         }
497     }
498     strlcat(item, "\n", sizeof(item));
499     strlcat(buf, item, sizeof(buf));
500
501     /* ExtendedAttributes */
502     strcpy(item, "EXTATTRTYPE:");
503     switch (vol->v_vfs_ea) {
504     case AFPVOL_EA_SYS:
505         strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
506         break;
507     case AFPVOL_EA_AD:
508         strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
509         break;
510     case AFPVOL_EA_NONE:
511         strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
512         break;
513     default:
514         strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
515     }
516
517     strlcat(buf, item, sizeof(buf));
518
519     if (strlen(buf) >= sizeof(buf)-1)
520         LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
521    if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
522        LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
523    }
524
525    lock.l_type = F_UNLCK;
526    fcntl(fd, F_SETLK, &lock);
527    close (fd);
528    return ret;
529 }