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