pull-prototypes-to-head-20020821
[openafs.git] / src / sys / rmtsysnet.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <errno.h>
16 #include <sys/param.h>
17 #include <sys/types.h>
18 #include <afs/vice.h>
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #else
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <sys/file.h>
25 #endif
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #else
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 #endif
35 #include <afs/afsint.h>
36 #include <afs/venus.h>
37 #include <rx/xdr.h>
38 #include "rmtsys.h"
39
40
41 /*
42  * We deal with converting pioctl parameters between host and network order.
43  * Painful but somebody has to do this and pioctl is the best place rather than
44  * leaving it to the calling application programs!
45  */
46
47 #define MAXNAME         100
48 #define MAXSIZE         2048
49 #define MAXHOSTS        8       /* XXX HARD Limit limitation XXX */
50 #define MAXGCSIZE       16
51
52 struct Acl {
53     int     nplus;
54     int     nminus;
55     struct  AclEntry *pluslist;
56     struct  AclEntry *minuslist;
57 };
58
59 struct AclEntry {
60     struct  AclEntry    *next;
61     char                name[MAXNAME];
62     afs_int32           rights;
63 };
64
65 struct ClearToken {
66         afs_int32 AuthHandle;
67         char HandShakeKey[8];
68         afs_int32 ViceId;
69         afs_int32 BeginTimestamp;
70         afs_int32 EndTimestamp;
71 };
72
73 char *RSkipLine (astr)
74     register char *astr; {
75     while (*astr !='\n') astr++;
76     astr++;
77     return astr;
78 }
79
80
81 struct Acl *RParseAcl(astr)
82     char *astr; {
83     int nplus, nminus, i, trights;
84     char tname[MAXNAME];
85     struct AclEntry *first, *last, *tl;
86     struct Acl *ta;
87     sscanf(astr, "%d", &nplus);
88     astr = RSkipLine(astr);
89     sscanf(astr, "%d", &nminus);
90     astr = RSkipLine(astr);
91
92     ta = (struct Acl *) malloc (sizeof (struct Acl));
93     ta->nplus = nplus;
94     ta->nminus = nminus;
95
96     last = 0;
97     first = 0;
98     for(i=0;i<nplus;i++) {
99         sscanf(astr, "%100s %d", tname, &trights);
100         astr = RSkipLine(astr);
101         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
102         if (!first) first = tl;
103         strcpy(tl->name, tname);
104         tl->rights = trights;
105         tl->next = 0;
106         if (last) last->next = tl;
107         last = tl;
108     }
109     ta->pluslist = first;
110
111     last = 0;
112     first = 0;
113     for(i=0;i<nminus;i++) {
114         sscanf(astr, "%100s %d", tname, &trights);
115         astr = RSkipLine(astr);
116         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
117         if (!first) first = tl;
118         strcpy(tl->name, tname);
119         tl->rights = trights;
120         tl->next = 0;
121         if (last) last->next = tl;
122         last = tl;
123     }
124     ta->minuslist = first;
125
126     return ta;
127 }
128
129
130 int RAclToString(struct Acl *acl, char *mydata, int ntoh_conv)
131 {
132     char tstring[MAXSIZE];
133     struct AclEntry *tp;
134
135 /* No conversion needed since they're in network order in the first place */
136     sprintf(mydata, "%d\n%d\n", acl->nplus, acl->nminus);
137     for(tp = acl->pluslist;tp;tp=tp->next) {
138         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
139         strcat(mydata, tstring);
140     }
141     for(tp = acl->minuslist;tp;tp=tp->next) {
142         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
143         strcat(mydata, tstring);
144     }
145 }
146
147
148 /* Free all malloced stuff */
149 int RCleanAcl(struct Acl *aa)
150 {
151     register struct AclEntry *te, *ne;
152
153     for(te = aa->pluslist; te; te=ne) {
154         ne = te->next;
155         free(te);
156     }
157     for(te = aa->minuslist; te; te=ne) {
158         ne = te->next;
159         free(te);
160     }
161     free(aa);
162 }
163
164
165 int RFetchVolumeStatus_conversion(char *data, int ntoh_conv)
166 {
167     struct AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *)data;
168
169     if (ntoh_conv) { /* Network -> Host */
170         status->Vid = ntohl(status->Vid);
171         status->ParentId = ntohl(status->ParentId);
172 #ifdef  notdef
173 /* save cycles */
174         status->OnLine = status->OnLine;
175         status->InService = status->InService;
176         status->Blessed = status->Blessed;
177         status->NeedsSalvage = status->NeedsSalvage;
178 #endif
179         status->Type = ntohl(status->Type);
180         status->MinQuota = ntohl(status->MinQuota);
181         status->MaxQuota = ntohl(status->MaxQuota);
182         status->BlocksInUse = ntohl(status->BlocksInUse);
183         status->PartBlocksAvail = ntohl(status->PartBlocksAvail);
184         status->PartMaxBlocks = ntohl(status->PartMaxBlocks);
185     } else {    /* Host -> Network */
186         status->Vid = htonl(status->Vid);
187         status->ParentId = htonl(status->ParentId);
188 #ifdef  notdef
189 /* save cycles */
190         status->OnLine = status->OnLine;
191         status->InService = status->InService;
192         status->Blessed = status->Blessed;
193         status->NeedsSalvage = status->NeedsSalvage;
194 #endif
195         status->Type = htonl(status->Type);
196         status->MinQuota = htonl(status->MinQuota);
197         status->MaxQuota = htonl(status->MaxQuota);
198         status->BlocksInUse = htonl(status->BlocksInUse);
199         status->PartBlocksAvail = htonl(status->PartBlocksAvail);
200         status->PartMaxBlocks = htonl(status->PartMaxBlocks);
201     }
202 }
203
204 int RClearToken_convert(char *ptr, int ntoh_conv)
205 {
206     struct ClearToken *ticket = (struct ClearToken *)ptr;
207
208     if (ntoh_conv) {    /* Network -> host */
209         ticket->AuthHandle = ntohl(ticket->AuthHandle);
210         ticket->ViceId = ntohl(ticket->ViceId);
211         ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
212         ticket->EndTimestamp = ntohl(ticket->EndTimestamp);
213     } else {    /* Host -> Network */
214         ticket->AuthHandle = htonl(ticket->AuthHandle);
215         ticket->ViceId = htonl(ticket->ViceId);
216         ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
217         ticket->EndTimestamp = htonl(ticket->EndTimestamp);
218     }   
219 }
220
221 int inparam_conversion(afs_int32 cmd, char *buffer, afs_int32 ntoh_conv)
222 {
223     struct Acl *acl;
224     afs_int32 *lptr, i;
225     char *ptr;
226
227     switch (cmd & 0xffff) {
228         case VIOCSETAL & 0xffff:
229             acl = RParseAcl(buffer);
230             RAclToString(acl, buffer, ntoh_conv);
231             RCleanAcl(acl);
232             break;
233         case VIOCSETTOK & 0xffff:
234             lptr = (afs_int32 *)buffer;
235             /* i is sizeof of the secret ticket */
236             if (ntoh_conv) {
237                 i = ntohl(*lptr);
238                 *lptr = i;
239             } else {
240                 i = *lptr;
241                 *lptr = htonl(i);
242             }
243             lptr++;
244             ptr = (char *)lptr;
245             ptr += i;   /* skip over the ticket */
246             lptr = (afs_int32 *)ptr;
247             /* i is now size of the clear token */
248             if (ntoh_conv) {
249                 i = ntohl(*lptr);
250                 *lptr = i;
251             } else {
252                 i = *lptr;
253                 *lptr = htonl(i);
254             }
255             lptr++;
256             ptr = (char *)lptr;
257             RClearToken_convert(ptr, ntoh_conv);
258             ptr += i;   /* sizeof(struct ClearToken) */
259             lptr = (afs_int32 *)ptr;
260             if (ntoh_conv)
261                 *lptr = ntohl(*lptr);
262             else
263                 *lptr = htonl(*lptr);
264             lptr++;   /* primary flag */            
265             break;
266         case VIOCSETVOLSTAT & 0xffff:
267             RFetchVolumeStatus_conversion(buffer, ntoh_conv);
268             break;
269         case VIOCGETTOK & 0xffff:
270             lptr = (afs_int32 *)buffer;
271             if (ntoh_conv)
272                 *lptr = ntohl(*lptr);
273             else
274                 *lptr = htonl(*lptr);
275             break;
276         case VIOCCKSERV & 0xffff:
277             lptr = (afs_int32 *)buffer;
278             if (ntoh_conv)
279                 *lptr = ntohl(*lptr);
280             else
281                 *lptr = htonl(*lptr);
282             break;
283         case VIOCACCESS & 0xffff:
284             lptr = (afs_int32 *)buffer;
285             if (ntoh_conv)
286                 *lptr = ntohl(*lptr);
287             else
288                 *lptr = htonl(*lptr);
289             break;
290         case VIOCSETCACHESIZE & 0xffff:
291             lptr = (afs_int32 *)buffer;
292             if (ntoh_conv)
293                 *lptr = ntohl(*lptr);
294             else
295                 *lptr = htonl(*lptr);
296             break;
297         case VIOCGETCELL & 0xffff:
298             lptr = (afs_int32 *)buffer;
299             if (ntoh_conv)
300                 *lptr = ntohl(*lptr);
301             else
302                 *lptr = htonl(*lptr);
303             break;
304         case VIOC_AFS_MARINER_HOST & 0xffff:
305             lptr = (afs_int32 *)buffer;
306             if (ntoh_conv)
307                 *lptr = ntohl(*lptr);
308             else
309                 *lptr = htonl(*lptr);
310             break;
311         case VIOC_VENUSLOG & 0xffff:
312             lptr = (afs_int32 *)buffer;
313             if (ntoh_conv)
314                 *lptr = ntohl(*lptr);
315             else
316                 *lptr = htonl(*lptr);
317             break;
318         case VIOC_SETCELLSTATUS & 0xffff:
319             lptr = (afs_int32 *)buffer;
320             if (ntoh_conv)
321                 *lptr = ntohl(*lptr);
322             else
323                 *lptr = htonl(*lptr);
324             lptr++;
325             if (ntoh_conv)
326                 *lptr = ntohl(*lptr);
327             else
328                 *lptr = htonl(*lptr);
329             break;
330         case VIOC_AFS_SYSNAME & 0xffff:
331             lptr = (afs_int32 *)buffer;
332             if (ntoh_conv)
333                 *lptr = ntohl(*lptr);
334             else
335                 *lptr = htonl(*lptr);
336             break;
337         case VIOC_EXPORTAFS & 0xffff:
338             lptr = (afs_int32 *)buffer;
339             if (ntoh_conv)
340                 *lptr = ntohl(*lptr);
341             else
342                 *lptr = htonl(*lptr);
343             break;
344         case VIOCGETAL & 0xffff:
345         case VIOCGETVOLSTAT & 0xffff:
346         case VIOCGETCACHEPARMS & 0xffff:
347         case VIOCFLUSH & 0xffff:
348         case VIOCSTAT & 0xffff:
349         case VIOCUNLOG & 0xffff:
350         case VIOCCKBACK & 0xffff:
351         case VIOCCKCONN & 0xffff:
352         case VIOCGETTIME & 0xffff:      /* Obsolete */
353         case VIOCWHEREIS & 0xffff:
354         case VIOCPREFETCH & 0xffff:
355         case VIOCNOP & 0xffff:          /* Obsolete */
356         case VIOCENGROUP & 0xffff:      /* Obsolete */
357         case VIOCDISGROUP & 0xffff:     /* Obsolete */
358         case VIOCLISTGROUPS & 0xffff:   /* Obsolete */
359         case VIOCUNPAG & 0xffff:
360         case VIOCWAITFOREVER & 0xffff:  /* Obsolete */
361         case VIOCFLUSHCB & 0xffff:
362         case VIOCNEWCELL & 0xffff:
363         case VIOC_AFS_DELETE_MT_PT & 0xffff:
364         case VIOC_AFS_STAT_MT_PT & 0xffff:
365         case VIOC_FILE_CELL_NAME & 0xffff:
366         case VIOC_GET_WS_CELL & 0xffff:
367         case VIOC_GET_PRIMARY_CELL & 0xffff:
368         case VIOC_GETCELLSTATUS & 0xffff:
369         case VIOC_FLUSHVOLUME & 0xffff:
370         case VIOCGETFID & 0xffff:       /* nothing yet */
371             break;
372         default:
373             /* Note that new pioctls are supposed to be in network order! */
374             break;          
375     }
376 }
377     
378
379 int outparam_conversion(afs_int32 cmd, char *buffer, afs_int32 ntoh_conv)
380 {
381     struct Acl *acl;
382     afs_int32 *lptr, i;
383     char *ptr;
384
385     switch (cmd & 0xffff) {
386         case VIOCGETAL & 0xffff:
387             acl = RParseAcl(buffer);
388             RAclToString(acl, buffer, ntoh_conv);
389             RCleanAcl(acl);
390             break;
391         case VIOCGETVOLSTAT & 0xffff:
392             RFetchVolumeStatus_conversion(buffer, ntoh_conv);
393             break;
394         case VIOCSETVOLSTAT & 0xffff:
395             RFetchVolumeStatus_conversion(buffer, ntoh_conv);
396             break;
397         case VIOCGETTOK & 0xffff:
398             lptr = (afs_int32 *)buffer;                 
399             /* i is set to sizeof secret ticket */
400             if (ntoh_conv) {
401                 i = ntohl(*lptr);
402                 *lptr = i;
403             } else {
404                 i = *lptr;
405                 *lptr = htonl(i);
406             }
407             lptr++;
408             ptr = (char *)lptr;
409             ptr += i;   /* skip over the ticket */
410             lptr = (afs_int32 *)ptr;
411             /* i is set to sizeof clear ticket */
412             if (ntoh_conv) {
413                 i = ntohl(*lptr);
414                 *lptr = i;
415             } else {
416                 i = *lptr;
417                 *lptr = htonl(i);
418             }
419             lptr++;
420             ptr = (char *)lptr;
421             RClearToken_convert(ptr, ntoh_conv);
422             ptr += i;   /* sizeof(struct ClearToken) */
423             lptr = (afs_int32 *)ptr;
424             if (ntoh_conv)
425                 *lptr = ntohl(*lptr);
426             else
427                 *lptr = htonl(*lptr);
428             lptr++;   /* primary flag */         
429             break;
430         case VIOCCKCONN & 0xffff:
431             if (ntoh_conv)
432                 *lptr = ntohl(*lptr);
433             else
434                 *lptr = htonl(*lptr);
435             break;
436         case VIOC_AFS_MARINER_HOST & 0xffff:
437             lptr = (afs_int32 *)buffer;
438             if (ntoh_conv)
439                 *lptr = ntohl(*lptr);
440             else
441                 *lptr = htonl(*lptr);
442             break;
443         case VIOC_VENUSLOG & 0xffff:
444             lptr = (afs_int32 *)buffer;
445             if (ntoh_conv)
446                 *lptr = ntohl(*lptr);
447             else
448                 *lptr = htonl(*lptr);
449             break;
450         case VIOC_GETCELLSTATUS & 0xffff:
451             lptr = (afs_int32 *)buffer;
452             if (ntoh_conv)
453                 *lptr = ntohl(*lptr);
454             else
455                 *lptr = htonl(*lptr);
456             break;
457         case VIOC_AFS_SYSNAME & 0xffff:
458             lptr = (afs_int32 *)buffer;
459             if (ntoh_conv)
460                 *lptr = ntohl(*lptr);
461             else
462                 *lptr = htonl(*lptr);
463             break;
464         case VIOC_EXPORTAFS & 0xffff:
465             lptr = (afs_int32 *)buffer;
466             if (ntoh_conv)
467                 *lptr = ntohl(*lptr);
468             else
469                 *lptr = htonl(*lptr);
470             break;
471         case VIOCGETCACHEPARMS & 0xffff: 
472             lptr = (afs_int32 *)buffer;
473             for (i=0; i < MAXGCSIZE; i++, lptr++) {
474                 if (ntoh_conv)
475                     *lptr = ntohl(*lptr);
476                 else
477                     *lptr = htonl(*lptr);
478             }
479             break;
480         case VIOCUNLOG & 0xffff:
481         case VIOCCKSERV & 0xffff:       /* Already in network order */
482         case VIOCCKBACK & 0xffff:
483         case VIOCGETTIME & 0xffff:      /* Obsolete */
484         case VIOCWHEREIS & 0xffff:      /* Already in network order */
485         case VIOCPREFETCH & 0xffff:
486         case VIOCNOP & 0xffff:          /* Obsolete */
487         case VIOCENGROUP & 0xffff:      /* Obsolete */
488         case VIOCDISGROUP & 0xffff:     /* Obsolete */
489         case VIOCLISTGROUPS & 0xffff:   /* Obsolete */
490         case VIOCACCESS & 0xffff:
491         case VIOCUNPAG & 0xffff:
492         case VIOCWAITFOREVER & 0xffff:  /* Obsolete */
493         case VIOCSETCACHESIZE & 0xffff:
494         case VIOCFLUSHCB & 0xffff:
495         case VIOCNEWCELL & 0xffff:
496         case VIOCGETCELL & 0xffff:      /* Already in network order */
497         case VIOC_AFS_DELETE_MT_PT & 0xffff:
498         case VIOC_AFS_STAT_MT_PT & 0xffff:
499         case VIOC_FILE_CELL_NAME & 0xffff:      /* no need to convert */
500         case VIOC_GET_WS_CELL & 0xffff: /* no need to convert */
501         case VIOCGETFID & 0xffff:       /* nothing yet */
502         case VIOCSETAL & 0xffff:
503         case VIOCFLUSH & 0xffff:
504         case VIOCSTAT & 0xffff:
505         case VIOCSETTOK & 0xffff:
506         case VIOC_GET_PRIMARY_CELL & 0xffff:    /* The cell is returned here */
507         case VIOC_SETCELLSTATUS & 0xffff:
508         case VIOC_FLUSHVOLUME & 0xffff:
509             break;
510         default:
511             /* Note that new pioctls are supposed to be in network order! */
512             break;
513     }
514 }
515
516
517
518
519
520
521