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