]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/appl.c
7eb6880dc724e0b51ec5dcafe1b77266fa61f65c
[netatalk.git] / etc / afpd / appl.c
1 /*
2  * $Id: appl.c,v 1.14 2003-06-09 15:09:19 srittau Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #ifdef HAVE_FCNTL_H
17 #include <fcntl.h>
18 #endif /* HAVE_FCNTL_H */
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif /* HAVE_UNISTD_H */
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/param.h>
25 #include <atalk/logger.h>
26 #include <errno.h>
27
28 #include <netatalk/endian.h>
29 #include <atalk/adouble.h>
30 #include <atalk/afp.h>
31
32 #include "volume.h"
33 #include "globals.h"
34 #include "directory.h"
35 #include "file.h"
36 #include "desktop.h"
37
38 static struct savedt    sa = { { 0, 0, 0, 0 }, -1, 0 };
39
40 static __inline__ int pathcmp( p, plen, q, qlen )
41 char    *p;
42 int     plen;
43 char    *q;
44 int     qlen;
45 {
46     return (( plen == qlen && memcmp( p, q, plen ) == 0 ) ? 0 : 1 );
47 }
48
49 static int applopen( vol, creator, flags, mode )
50 struct vol      *vol;
51 u_char  creator[ 4 ];
52 {
53     char        *dtf, *adt, *adts;
54
55     if ( sa.sdt_fd != -1 ) {
56         if ( !(flags & ( O_RDWR | O_WRONLY )) &&
57                 memcmp( sa.sdt_creator, creator, sizeof( CreatorType )) == 0 &&
58                 sa.sdt_vid == vol->v_vid ) {
59             return( AFP_OK );
60         }
61         close( sa.sdt_fd );
62         sa.sdt_fd = -1;
63     }
64
65     dtf = dtfile( vol, creator, ".appl" );
66
67     if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
68         if ( errno == ENOENT && ( flags & O_CREAT )) {
69             if (( adts = strrchr( dtf, '/' )) == NULL ) {
70                 return( AFPERR_PARAM );
71             }
72             *adts = '\0';
73             if (( adt = strrchr( dtf, '/' )) == NULL ) {
74                 return( AFPERR_PARAM );
75             }
76             *adt = '\0';
77             (void) ad_mkdir( dtf, DIRBITS | 0777 );
78             *adt = '/';
79             (void) ad_mkdir( dtf, DIRBITS | 0777 );
80             *adts = '/';
81
82             if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
83                 return( AFPERR_PARAM );
84             }
85         } else {
86             return( AFPERR_PARAM );
87         }
88     }
89     memcpy( sa.sdt_creator, creator, sizeof( CreatorType ));
90     sa.sdt_vid = vol->v_vid;
91     sa.sdt_index = 0;
92     return( AFP_OK );
93 }
94
95 /*
96  * copy appls to new file, deleting any matching (old) appl entries
97  */
98 static int copyapplfile( sfd, dfd, mpath, mplen )
99 int             sfd;
100 int             dfd;
101 char    *mpath;
102 u_short mplen;
103 {
104     int         cc;
105     char        *p;
106     u_int16_t   len;
107     u_char      appltag[ 4 ];
108     char        buf[ MAXPATHLEN ];
109
110     while (( cc = read( sfd, buf, sizeof(appltag) + sizeof( u_short ))) > 0 ) {
111         p = buf + sizeof(appltag);
112         memcpy( &len, p, sizeof(len));
113         len = ntohs( len );
114         p += sizeof( len );
115         if (( cc = read( sa.sdt_fd, p, len )) < len ) {
116             break;
117         }
118         if ( pathcmp( mpath, mplen, p, len ) != 0 ) {
119             p += len;
120             if ( write( dfd, buf, p - buf ) != p - buf ) {
121                 cc = -1;
122                 break;
123             }
124         }
125     }
126     return( cc );
127 }
128
129 /*
130  * build mac. path (backwards) by traversing the directory tree
131  *
132  * The old way: dir and path refer to an app, path is a mac format
133  * pathname.  makemacpath() builds something that looks like a cname,
134  * but uses upaths instead of mac format paths.
135  *
136  * The new way: dir and path refer to an app, path is a mac format
137  * pathname.  makemacpath() builds a cname.
138  *
139  * See afp_getappl() for the backward compatiblity code.
140  */
141 static char *
142 makemacpath( mpath, mpathlen, dir, path )
143 char    *mpath;
144 int             mpathlen;
145 struct dir      *dir;
146 char    *path;
147 {
148     char        *p;
149
150     p = mpath + mpathlen;
151     p -= strlen( path );
152     strncpy( p, path, strlen( path ));
153
154     while ( dir->d_parent != NULL ) {
155         p -= strlen( dir->d_m_name ) + 1;
156         strcpy( p, dir->d_m_name );
157         dir = dir->d_parent;
158     }
159     return( p );
160 }
161
162
163 int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen )
164 AFPObj      *obj;
165 char    *ibuf, *rbuf;
166 int             ibuflen, *rbuflen;
167 {
168     struct vol          *vol;
169     struct dir          *dir;
170     int                 tfd, cc;
171     u_int32_t           did;
172     u_int16_t           vid, mplen;
173     struct path         *path;
174     char                *dtf, *p, *mp;
175     u_char              creator[ 4 ];
176     u_char              appltag[ 4 ];
177     char                *mpath, *tempfile;
178
179     *rbuflen = 0;
180     ibuf += 2;
181
182     memcpy( &vid, ibuf, sizeof( vid ));
183     ibuf += sizeof( vid );
184     if (NULL == ( vol = getvolbyvid( vid ))) {
185         return( AFPERR_PARAM );
186     }
187
188     memcpy( &did, ibuf, sizeof( did ));
189     ibuf += sizeof( did );
190     if (NULL == ( dir = dirlookup( vol, did )) ) {
191         return afp_errno;
192     }
193
194     memcpy( creator, ibuf, sizeof( creator ));
195     ibuf += sizeof( creator );
196
197     memcpy( appltag, ibuf, sizeof( appltag ));
198     ibuf += sizeof( appltag );
199
200     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
201         return get_afp_errno(AFPERR_PARAM);
202     }
203     if ( path_isadir(path) ) {
204         return( AFPERR_BADTYPE );
205     }
206
207     if ( applopen( vol, creator, O_RDWR|O_CREAT, 0666 ) != AFP_OK ) {
208         return( AFPERR_PARAM );
209     }
210     if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
211         return( AFPERR_PARAM );
212     }
213     dtf = dtfile( vol, creator, ".appl.temp" );
214     tempfile = obj->oldtmp;
215     strcpy( tempfile, dtf );
216     if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
217         return( AFPERR_PARAM );
218     }
219     mpath = obj->newtmp;
220     mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path->m_name );
221     mplen =  mpath + AFPOBJ_TMPSIZ - mp;
222
223     /* write the new appl entry at start of temporary file */
224     p = mp - sizeof( u_short );
225     mplen = htons( mplen );
226     memcpy( p, &mplen, sizeof( mplen ));
227     mplen = ntohs( mplen );
228     p -= sizeof( appltag );
229     memcpy(p, appltag, sizeof( appltag ));
230     cc = mpath + AFPOBJ_TMPSIZ - p;
231     if ( write( tfd, p, cc ) != cc ) {
232         unlink( tempfile );
233         return( AFPERR_PARAM );
234     }
235     cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
236     close( tfd );
237     close( sa.sdt_fd );
238     sa.sdt_fd = -1;
239
240     if ( cc < 0 ) {
241         unlink( tempfile );
242         return( AFPERR_PARAM );
243     }
244     if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
245         return( AFPERR_PARAM );
246     }
247     return( AFP_OK );
248 }
249
250 int afp_rmvappl(obj, ibuf, ibuflen, rbuf, rbuflen )
251 AFPObj      *obj;
252 char    *ibuf, *rbuf;
253 int             ibuflen, *rbuflen;
254 {
255     struct vol          *vol;
256     struct dir          *dir;
257     int                 tfd, cc;
258     u_int32_t           did;
259     u_int16_t           vid, mplen;
260     struct path         *path;
261     char                *dtf, *mp;
262     u_char              creator[ 4 ];
263     char                *tempfile, *mpath;
264
265     *rbuflen = 0;
266     ibuf += 2;
267
268     memcpy( &vid, ibuf, sizeof( vid ));
269     ibuf += sizeof( vid );
270     if (NULL == ( vol = getvolbyvid( vid ))) {
271         return( AFPERR_PARAM );
272     }
273
274     memcpy( &did, ibuf, sizeof( did ));
275     ibuf += sizeof( did );
276     if (NULL == ( dir = dirlookup( vol, did )) ) {
277         return afp_errno;
278     }
279
280     memcpy( creator, ibuf, sizeof( creator ));
281     ibuf += sizeof( creator );
282
283     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
284         return get_afp_errno(AFPERR_PARAM);
285     }
286     if ( path_isadir(path) ) {
287         return( AFPERR_BADTYPE );
288     }
289
290     if ( applopen( vol, creator, O_RDWR, 0666 ) != AFP_OK ) {
291         return( AFPERR_NOOBJ );
292     }
293     if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
294         return( AFPERR_PARAM );
295     }
296     dtf = dtfile( vol, creator, ".appl.temp" );
297     tempfile = obj->oldtmp;
298     strcpy( tempfile, dtf );
299     if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
300         return( AFPERR_PARAM );
301     }
302     mpath = obj->newtmp;
303     mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path->m_name );
304     mplen =  mpath + AFPOBJ_TMPSIZ - mp;
305     cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
306     close( tfd );
307     close( sa.sdt_fd );
308     sa.sdt_fd = -1;
309
310     if ( cc < 0 ) {
311         unlink( tempfile );
312         return( AFPERR_PARAM );
313     }
314     if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
315         return( AFPERR_PARAM );
316     }
317     return( AFP_OK );
318 }
319
320 int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen )
321 AFPObj      *obj;
322 char    *ibuf, *rbuf;
323 int             ibuflen, *rbuflen;
324 {
325     struct vol          *vol;
326     char                *p, *q;
327     int                 cc, buflen;
328     u_int16_t           vid, aindex, bitmap, len;
329     u_char              creator[ 4 ];
330     u_char              appltag[ 4 ];
331     char                *buf, *cbuf;
332     struct path         *path;
333
334     ibuf += 2;
335
336     memcpy( &vid, ibuf, sizeof( vid ));
337     ibuf += sizeof( vid );
338     if (NULL == ( vol = getvolbyvid( vid )) ) {
339         *rbuflen = 0;
340         return( AFPERR_PARAM );
341     }
342
343     memcpy( creator, ibuf, sizeof( creator ));
344     ibuf += sizeof( creator );
345
346     memcpy( &aindex, ibuf, sizeof( aindex ));
347     ibuf += sizeof( aindex );
348     aindex = ntohs( aindex );
349     if ( aindex ) { /* index 0 == index 1 */
350         --aindex;
351     }
352
353     memcpy( &bitmap, ibuf, sizeof( bitmap ));
354     bitmap = ntohs( bitmap );
355     ibuf += sizeof( bitmap );
356
357     if ( applopen( vol, creator, O_RDONLY, 0666 ) != AFP_OK ) {
358         *rbuflen = 0;
359         return( AFPERR_NOITEM );
360     }
361     if ( aindex < sa.sdt_index ) {
362         if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
363             *rbuflen = 0;
364             return( AFPERR_PARAM );
365         }
366         sa.sdt_index = 0;
367     }
368
369     /* position to correct spot within appl file */
370     buf = obj->oldtmp;
371     while (( cc = read( sa.sdt_fd, buf, sizeof( appltag )
372                         + sizeof( u_short ))) > 0 ) {
373         p = buf + sizeof( appltag );
374         memcpy( &len, p, sizeof( len ));
375         len = ntohs( len );
376         p += sizeof( u_short );
377         if (( cc = read( sa.sdt_fd, p, len )) < len ) {
378             break;
379         }
380         if ( sa.sdt_index == aindex ) {
381             break;
382         }
383         sa.sdt_index++;
384     }
385     if ( cc <= 0 || sa.sdt_index != aindex ) {
386         *rbuflen = 0;
387         return( AFPERR_NOITEM );
388     }
389     sa.sdt_index++;
390
391 #ifdef APPLCNAME
392     /*
393      * Check to see if this APPL mapping has an mpath or a upath.  If
394      * there are any ':'s in the name, it is a upath and must be converted
395      * to an mpath.  Hopefully, this code will go away.
396      */
397     {
398 #define hextoint( c )   ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
399 #define islxdigit(x)    (!isupper(x)&&isxdigit(x))
400
401         char    utomname[ MAXPATHLEN + 1];
402         char            *u, *m;
403         int             i, h;
404
405         u = p;
406         m = utomname;
407         i = len;
408         while ( i ) {
409             if ( *u == ':' && *(u+1) != '\0' && islxdigit( *(u+1)) &&
410                     *(u+2) != '\0' && islxdigit( *(u+2))) {
411                 ++u, --i;
412                 h = hextoint( *u ) << 4;
413                 ++u, --i;
414                 h |= hextoint( *u );
415                 *m++ = h;
416             } else {
417                 *m++ = *u;
418             }
419             ++u, --i;
420         }
421
422         len = m - utomname;
423         p = utomname;
424
425         if ( p[ len - 1 ] == '\0' ) {
426             len--;
427         }
428     }
429 #endif /* APPLCNAME */
430
431     /* fake up a cname */
432     cbuf = obj->newtmp;
433     q = cbuf;
434     *q++ = 2;   /* long path type */
435     *q++ = (unsigned char)len;
436     memcpy( q, p, len );
437     q = cbuf;
438
439     if (( path = cname( vol, vol->v_dir, &q )) == NULL ) {
440         *rbuflen = 0;
441         return( AFPERR_NOITEM );
442     }
443
444     if ( path_isadir(path) || path->st_errno ) {
445         *rbuflen = 0;
446         return( AFPERR_NOITEM );
447     }
448     buflen = *rbuflen - sizeof( bitmap ) - sizeof( appltag );
449     if ( getfilparams(vol, bitmap, path, curdir, rbuf + sizeof( bitmap ) +
450                       sizeof( appltag ), &buflen ) != AFP_OK ) {
451         *rbuflen = 0;
452         return( AFPERR_BITMAP );
453     }
454
455     *rbuflen = buflen + sizeof( bitmap ) + sizeof( appltag );
456     bitmap = htons( bitmap );
457     memcpy( rbuf, &bitmap, sizeof( bitmap ));
458     rbuf += sizeof( bitmap );
459     memcpy( rbuf, appltag, sizeof( appltag ));
460     rbuf += sizeof( appltag );
461     return( AFP_OK );
462 }
463