]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afs.c
try to make afpd directory access right works the way Macs expect
[netatalk.git] / etc / afpd / afs.c
1 /*
2  * $Id: afs.c,v 1.11 2002-08-29 18:57:26 didg Exp $
3  * Copyright (c) 1990,1993 Regents of The University of Michigan.
4  * All Rights Reserved.  See COPYRIGHT.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif /* HAVE_CONFIG_H */
10
11 #ifdef AFS
12
13 #include <string.h>
14 #include <sys/types.h>
15 #include <atalk/logger.h>
16 #include <netatalk/endian.h>
17 #include <netinet/in.h>
18 #include <afs/venus.h>
19 #include <afs/afsint.h>
20 #include <atalk/afp.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif /* HAVE_UNISTD_H */
24
25 #include "globals.h"
26 #include "directory.h"
27 #include "volume.h"
28 #include "misc.h"
29
30 afs_getvolspace( vol, bfree, btotal, bsize )
31 struct vol      *vol;
32 VolSpace        *bfree, *btotal;
33 u_int32_t   *bsize;
34 {
35     struct ViceIoctl    vi;
36     struct VolumeStatus *vs;
37     char                venuspace[ sizeof( struct VolumeStatus ) + 3 ];
38     int                 total, free;
39
40     vi.in_size = 0;
41     vi.out_size = sizeof( venuspace );
42     vi.out = venuspace;
43     if ( pioctl( vol->v_path, VIOCGETVOLSTAT, &vi, 1 ) < 0 ) {
44         return( AFPERR_PARAM );
45     }
46
47     vs = (struct VolumeStatus *)venuspace;
48
49     if ( vs->PartBlocksAvail > 0 ) {
50         if ( vs->MaxQuota != 0 ) {
51 #define min(x,y)        (((x)<(y))?(x):(y))
52             free = min( vs->MaxQuota - vs->BlocksInUse, vs->PartBlocksAvail );
53         } else {
54             free = vs->PartBlocksAvail;
55         }
56     } else {
57         free = 0;
58     }
59
60     if ( vs->MaxQuota != 0 ) {
61         total = free + vs->BlocksInUse;
62     } else {
63         total = vs->PartMaxBlocks;
64     }
65
66     *bsize = 1024;
67     *bfree = (VolSpace) free * 1024;
68     *btotal = (VolSpace) total * 1024;
69
70     return( AFP_OK );
71 }
72
73 afp_getdiracl(obj, ibuf, ibuflen, rbuf, rbuflen )
74 AFPObj      *obj;
75 char    *ibuf, *rbuf;
76 int             ibuflen, *rbuflen;
77 {
78     struct ViceIoctl    vi;
79     struct vol          *vol;
80     struct dir          *dir;
81     char                *path;
82     u_int32_t           did;
83     u_int16_t           vid;
84
85     ibuf += 2;
86     memcpy( &vid, ibuf, sizeof( vid ));
87     ibuf += sizeof( short );
88     if (( vol = getvolbyvid( vid )) == NULL ) {
89         *rbuflen = 0;
90         return( AFPERR_PARAM );
91     }
92
93     memcpy( &did, ibuf, sizeof( did ));
94     ibuf += sizeof( int );
95     if (( dir = dirsearch( vol, did )) == NULL ) {
96         *rbuflen = 0;
97         return( AFPERR_NOOBJ );
98     }
99
100     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
101         *rbuflen = 0;
102         return( AFPERR_NOOBJ );
103     }
104     if ( *path != '\0' ) {
105         *rbuflen = 0;
106         return( AFPERR_BITMAP );
107     }
108
109     vi.in_size = 0;
110     vi.out_size = *rbuflen;
111     vi.out = rbuf;
112     if ( pioctl( ".", VIOCGETAL, &vi, 1 ) < 0 ) {
113         *rbuflen = 0;
114         return( AFPERR_PARAM );
115     }
116     *rbuflen = strlen( vi.out ) + 1;
117     return( AFP_OK );
118 }
119
120 /*
121  * Calculate the mode for a directory in AFS.  First, make sure the
122  * directory is in AFS.  Could probably use something less heavy than
123  * VIOCGETAL.  If the directory is on AFS, use access() calls to
124  * estimate permission, a la mdw.
125  */
126 #ifdef accessmode
127     #undef accessmode
128 #endif
129
130 afsmode( path, ma, dir, st )
131 char            *path;
132 struct maccess  *ma;
133 struct dir      *dir;
134 struct stat     *st;
135 {
136     struct ViceIoctl    vi;
137     char                buf[ 1024 ];
138
139     if (( dir->d_flags & DIRF_FSMASK ) == DIRF_NOFS ) {
140         vi.in_size = 0;
141         vi.out_size = sizeof( buf );
142         vi.out = buf;
143         if ( pioctl( path, VIOCGETAL, &vi, 1 ) < 0 ) {
144             dir->d_flags |= DIRF_UFS;
145         } else {
146             dir->d_flags |= DIRF_AFS;
147         }
148     }
149
150     if (( dir->d_flags & DIRF_FSMASK ) != DIRF_AFS ) {
151         return;
152     }
153
154     accessmode( path, &ma, dir, st );
155
156     return;
157 }
158
159 extern struct dir       *curdir;
160 /*
161  * cmd | 0 | vid | did | pathtype | pathname | 0 | acl
162  */
163 afp_setdiracl(obj, ibuf, ibuflen, rbuf, rbuflen )
164 AFPObj      *obj;
165 char    *ibuf, *rbuf;
166 int             ibuflen, *rbuflen;
167 {
168     struct ViceIoctl    vi;
169     struct vol          *vol;
170     struct dir          *dir;
171     char                *path, *iend;
172     u_int32_t           did;
173     u_int16_t           vid;
174
175     *rbuflen = 0;
176     iend = ibuf + ibuflen;
177     ibuf += 2;
178     memcpy( &vid, ibuf, sizeof( vid ));
179     ibuf += sizeof( short );
180     if (( vol = getvolbyvid( vid )) == NULL ) {
181         *rbuflen = 0;
182         return( AFPERR_PARAM );
183     }
184
185     memcpy( &did, ibuf, sizeof( did ));
186     ibuf += sizeof( int );
187     if (( dir = dirsearch( vol, did )) == NULL ) {
188         *rbuflen = 0;
189         return( AFPERR_NOOBJ );
190     }
191
192     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
193         *rbuflen = 0;
194         return( AFPERR_NOOBJ );
195     }
196     if ( *path != '\0' ) {
197         *rbuflen = 0;
198         return( AFPERR_BITMAP );
199     }
200
201     if ((int)ibuf & 1 ) {
202         ibuf++;
203     }
204
205     vi.in_size = iend - ibuf;
206     vi.in = ibuf;
207     vi.out_size = 0;
208
209     if ( pioctl( ".", VIOCSETAL, &vi, 1 ) < 0 ) {
210         *rbuflen = 0;
211         return( AFPERR_PARAM );
212     }
213     pioctl( ".AppleDouble", VIOCSETAL, &vi, 1 );
214     if ( curdir->d_did  == DIRDID_ROOT ) {
215         pioctl( ".AppleDesktop", VIOCSETAL, &vi, 1 );
216     }
217
218     return( AFP_OK );
219 }
220
221
222 #ifdef UAM_AFSKRB
223
224 #include <krb.h>
225 #include <des.h>
226 #include <afs/kauth.h>
227 #include <afs/kautils.h>
228
229 extern C_Block          seskey;
230 extern Key_schedule     seskeysched;
231
232 afp_afschangepw(obj, ibuf, ibuflen, rbuf, rbuflen )
233 AFPObj      *obj;
234 char    *ibuf, *rbuf;
235 int             ibuflen, *rbuflen;
236 {
237     char        name[ MAXKTCNAMELEN ], instance[ MAXKTCNAMELEN ];
238     char        realm[ MAXKTCREALMLEN ];
239     char        oldpw[ 9 ], newpw[ 9 ];
240     int         len, rc;
241     u_int16_t   clen;
242     struct ktc_encryptionKey    oldkey, newkey;
243     struct ktc_token            adtok;
244     struct ubik_client          *conn;
245
246     *rbuflen = 0;
247     ++ibuf;
248     len = (unsigned char) *ibuf++;
249     ibuf[ len ] = '\0';
250     *name = *instance = *realm = '\0';
251     ka_ParseLoginName( ibuf, name, instance, realm );
252     ucase( realm );
253     if ( *realm == '\0' ) {
254         if ( krb_get_lrealm( realm, 1 ) != KSUCCESS ) {
255             LOG(log_error, logtype_afpd, "krb_get_lrealm failed" );
256             return( AFPERR_BADUAM );
257         }
258     }
259
260     if ( strlen( name ) < 2 || strlen( name ) > 18 ) {
261         return( AFPERR_PARAM );
262     }
263     ibuf += len;
264
265     memcpy( &clen, ibuf, sizeof( clen ));
266     clen = ntohs( clen );
267     if ( clen % 8 != 0 ) {
268         return( AFPERR_PARAM );
269     }
270
271     ibuf += sizeof( short );
272     pcbc_encrypt((C_Block *)ibuf, (C_Block *)ibuf,
273                  clen, seskeysched, seskey, DES_DECRYPT );
274
275     len = (unsigned char) *ibuf++;
276     if ( len > 8 ) {
277         return( AFPERR_PARAM );
278     }
279     memset( oldpw, 0, sizeof( oldpw ));
280     memcpy( oldpw, ibuf, len );
281     ibuf += len;
282     oldpw[ len ] = '\0';
283
284     len = (unsigned char) *ibuf++;
285     if ( len > 8 ) {
286         return( AFPERR_PARAM );
287     }
288     memset( newpw, 0, sizeof( newpw ));
289     memcpy( newpw, ibuf, len );
290     ibuf += len;
291     newpw[ len ] = '\0';
292
293     LOG(log_info, logtype_afpd,
294         "changing password for <%s>.<%s>@<%s>", name, instance, realm );
295
296     ka_StringToKey( oldpw, realm, &oldkey );
297     memset( oldpw, 0, sizeof( oldpw ));
298     ka_StringToKey( newpw, realm, &newkey );
299     memset( newpw, 0, sizeof( newpw ));
300
301     rc = ka_GetAdminToken( name, instance, realm, &oldkey, 60, &adtok, 0 );
302     memset( &oldkey, 0, sizeof( oldkey ));
303     switch ( rc ) {
304     case 0:
305         break;
306     case KABADREQUEST:
307         memset( &newkey, 0, sizeof( newkey ));
308         return( AFPERR_NOTAUTH );
309     default:
310         memset( &newkey, 0, sizeof( newkey ));
311         return( AFPERR_BADUAM );
312     }
313     if ( ka_AuthServerConn( realm, KA_MAINTENANCE_SERVICE, &adtok, &conn )
314             != 0 ) {
315         memset( &newkey, 0, sizeof( newkey ));
316         return( AFPERR_BADUAM );
317     }
318
319     rc = ka_ChangePassword( name, instance, conn, 0, &newkey );
320     memset( &newkey, 0, sizeof( newkey ));
321     if ( rc != 0 ) {
322         return( AFPERR_BADUAM );
323     }
324
325     LOG(log_debug, logtype_afpd, "password changed succeeded" );
326     return( AFP_OK );
327 }
328
329 #endif /* UAM_AFSKRB */
330 #endif /* AFS */