death to register
[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 #include <afs/afs_consts.h>
13
14 #include <errno.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <afs/vice.h>
18 #ifdef AFS_NT40_ENV
19 #include <winsock2.h>
20 #else
21 #include <netdb.h>
22 #include <netinet/in.h>
23 #include <sys/file.h>
24 #endif
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <string.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 MAXGCSIZE       16
42
43 struct Acl {
44     int nplus;
45     int nminus;
46     struct AclEntry *pluslist;
47     struct AclEntry *minuslist;
48 };
49
50 struct AclEntry {
51     struct AclEntry *next;
52     char name[MAXNAME];
53     afs_int32 rights;
54 };
55
56 struct ClearToken {
57     afs_int32 AuthHandle;
58     char HandShakeKey[8];
59     afs_int32 ViceId;
60     afs_int32 BeginTimestamp;
61     afs_int32 EndTimestamp;
62 };
63
64 char *
65 RSkipLine(char *astr)
66 {
67     while (*astr != '\n')
68         astr++;
69     astr++;
70     return astr;
71 }
72
73
74 struct Acl *
75 RParseAcl(char *astr)
76 {
77     int nplus, nminus, i, trights;
78     char tname[MAXNAME];
79     struct AclEntry *first, *last, *tl;
80     struct Acl *ta;
81     sscanf(astr, "%d", &nplus);
82     astr = RSkipLine(astr);
83     sscanf(astr, "%d", &nminus);
84     astr = RSkipLine(astr);
85
86     ta = (struct Acl *)malloc(sizeof(struct Acl));
87     ta->nplus = nplus;
88     ta->nminus = nminus;
89
90     last = 0;
91     first = 0;
92     for (i = 0; i < nplus; i++) {
93         sscanf(astr, "%100s %d", tname, &trights);
94         astr = RSkipLine(astr);
95         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
96         if (!first)
97             first = tl;
98         strcpy(tl->name, tname);
99         tl->rights = trights;
100         tl->next = 0;
101         if (last)
102             last->next = tl;
103         last = tl;
104     }
105     ta->pluslist = first;
106
107     last = 0;
108     first = 0;
109     for (i = 0; i < nminus; i++) {
110         sscanf(astr, "%100s %d", tname, &trights);
111         astr = RSkipLine(astr);
112         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
113         if (!first)
114             first = tl;
115         strcpy(tl->name, tname);
116         tl->rights = trights;
117         tl->next = 0;
118         if (last)
119             last->next = tl;
120         last = tl;
121     }
122     ta->minuslist = first;
123
124     return ta;
125 }
126
127
128 void
129 RAclToString(struct Acl *acl, char *mydata, int ntoh_conv)
130 {
131     char tstring[AFS_PIOCTL_MAXSIZE];
132     struct AclEntry *tp;
133
134 /* No conversion needed since they're in network order in the first place */
135     sprintf(mydata, "%d\n%d\n", acl->nplus, acl->nminus);
136     for (tp = acl->pluslist; tp; tp = tp->next) {
137         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
138         strcat(mydata, tstring);
139     }
140     for (tp = acl->minuslist; tp; tp = tp->next) {
141         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
142         strcat(mydata, tstring);
143     }
144 }
145
146
147 /* Free all malloced stuff */
148 void
149 RCleanAcl(struct Acl *aa)
150 {
151     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 void
166 RFetchVolumeStatus_conversion(char *data, int ntoh_conv)
167 {
168     struct AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *) data;
169
170     if (ntoh_conv) {            /* Network -> Host */
171         status->Vid = ntohl(status->Vid);
172         status->ParentId = ntohl(status->ParentId);
173 #ifdef  notdef
174 /* save cycles */
175         status->OnLine = status->OnLine;
176         status->InService = status->InService;
177         status->Blessed = status->Blessed;
178         status->NeedsSalvage = status->NeedsSalvage;
179 #endif
180         status->Type = ntohl(status->Type);
181         status->MinQuota = ntohl(status->MinQuota);
182         status->MaxQuota = ntohl(status->MaxQuota);
183         status->BlocksInUse = ntohl(status->BlocksInUse);
184         status->PartBlocksAvail = ntohl(status->PartBlocksAvail);
185         status->PartMaxBlocks = ntohl(status->PartMaxBlocks);
186     } else {                    /* Host -> Network */
187         status->Vid = htonl(status->Vid);
188         status->ParentId = htonl(status->ParentId);
189 #ifdef  notdef
190 /* save cycles */
191         status->OnLine = status->OnLine;
192         status->InService = status->InService;
193         status->Blessed = status->Blessed;
194         status->NeedsSalvage = status->NeedsSalvage;
195 #endif
196         status->Type = htonl(status->Type);
197         status->MinQuota = htonl(status->MinQuota);
198         status->MaxQuota = htonl(status->MaxQuota);
199         status->BlocksInUse = htonl(status->BlocksInUse);
200         status->PartBlocksAvail = htonl(status->PartBlocksAvail);
201         status->PartMaxBlocks = htonl(status->PartMaxBlocks);
202     }
203 }
204
205 void
206 RClearToken_convert(char *ptr, int ntoh_conv)
207 {
208     struct ClearToken *ticket = (struct ClearToken *)ptr;
209
210     if (ntoh_conv) {            /* Network -> host */
211         ticket->AuthHandle = ntohl(ticket->AuthHandle);
212         ticket->ViceId = ntohl(ticket->ViceId);
213         ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
214         ticket->EndTimestamp = ntohl(ticket->EndTimestamp);
215     } else {                    /* Host -> Network */
216         ticket->AuthHandle = htonl(ticket->AuthHandle);
217         ticket->ViceId = htonl(ticket->ViceId);
218         ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
219         ticket->EndTimestamp = htonl(ticket->EndTimestamp);
220     }
221 }
222
223 void
224 inparam_conversion(afs_int32 cmd, char *buffer, afs_int32 ntoh_conv)
225 {
226     struct Acl *acl;
227     afs_int32 *lptr, i;
228     char *ptr;
229
230     switch (cmd & 0xffff) {
231     case VIOCSETAL & 0xffff:
232         acl = RParseAcl(buffer);
233         RAclToString(acl, buffer, ntoh_conv);
234         RCleanAcl(acl);
235         break;
236     case VIOCSETTOK & 0xffff:
237         lptr = (afs_int32 *) buffer;
238         /* i is sizeof of the secret ticket */
239         if (ntoh_conv) {
240             i = ntohl(*lptr);
241             *lptr = i;
242         } else {
243             i = *lptr;
244             *lptr = htonl(i);
245         }
246         lptr++;
247         ptr = (char *)lptr;
248         ptr += i;               /* skip over the ticket */
249         lptr = (afs_int32 *) ptr;
250         /* i is now size of the clear token */
251         if (ntoh_conv) {
252             i = ntohl(*lptr);
253             *lptr = i;
254         } else {
255             i = *lptr;
256             *lptr = htonl(i);
257         }
258         lptr++;
259         ptr = (char *)lptr;
260         RClearToken_convert(ptr, ntoh_conv);
261         ptr += i;               /* sizeof(struct ClearToken) */
262         lptr = (afs_int32 *) ptr;
263         if (ntoh_conv)
264             *lptr = ntohl(*lptr);
265         else
266             *lptr = htonl(*lptr);
267         lptr++;                 /* primary flag */
268         break;
269     case VIOCSETVOLSTAT & 0xffff:
270         RFetchVolumeStatus_conversion(buffer, ntoh_conv);
271         break;
272     case VIOCGETTOK & 0xffff:
273     case VIOCCKSERV & 0xffff:
274     case VIOCACCESS & 0xffff:
275     case VIOCSETCACHESIZE & 0xffff:
276     case VIOCGETCELL & 0xffff:
277     case VIOC_AFS_MARINER_HOST & 0xffff:
278     case VIOC_VENUSLOG & 0xffff:
279     case VIOC_AFS_SYSNAME & 0xffff:
280     case VIOC_EXPORTAFS & 0xffff:
281         lptr = (afs_int32 *) buffer;
282         if (ntoh_conv)
283             *lptr = ntohl(*lptr);
284         else
285             *lptr = htonl(*lptr);
286         break;
287     case VIOC_SETCELLSTATUS & 0xffff:
288         lptr = (afs_int32 *) buffer;
289         if (ntoh_conv)
290             *lptr = ntohl(*lptr);
291         else
292             *lptr = htonl(*lptr);
293         lptr++;
294         if (ntoh_conv)
295             *lptr = ntohl(*lptr);
296         else
297             *lptr = htonl(*lptr);
298         break;
299     case VIOCGETAL & 0xffff:
300     case VIOCGETVOLSTAT & 0xffff:
301     case VIOCGETCACHEPARMS & 0xffff:
302     case VIOCFLUSH & 0xffff:
303     case VIOCSTAT & 0xffff:
304     case VIOCUNLOG & 0xffff:
305     case VIOCCKBACK & 0xffff:
306     case VIOCCKCONN & 0xffff:
307     case VIOCGETTIME & 0xffff:  /* Obsolete */
308     case VIOCWHEREIS & 0xffff:
309     case VIOCPREFETCH & 0xffff:
310     case VIOCNOP & 0xffff:      /* Obsolete */
311     case VIOCENGROUP & 0xffff:  /* Obsolete */
312     case VIOCDISGROUP & 0xffff: /* Obsolete */
313     case VIOCLISTGROUPS & 0xffff:       /* Obsolete */
314     case VIOCUNPAG & 0xffff:
315     case VIOCWAITFOREVER & 0xffff:      /* Obsolete */
316     case VIOCFLUSHCB & 0xffff:
317     case VIOCNEWCELL & 0xffff:
318     case VIOC_AFS_DELETE_MT_PT & 0xffff:
319     case VIOC_AFS_STAT_MT_PT & 0xffff:
320     case VIOC_FILE_CELL_NAME & 0xffff:
321     case VIOC_GET_WS_CELL & 0xffff:
322     case VIOC_GET_PRIMARY_CELL & 0xffff:
323     case VIOC_GETCELLSTATUS & 0xffff:
324     case VIOC_FLUSHVOLUME & 0xffff:
325     case VIOCGETFID & 0xffff:   /* nothing yet */
326         break;
327     default:
328         /* Note that new pioctls are supposed to be in network order! */
329         break;
330     }
331 }
332
333
334 void
335 outparam_conversion(afs_int32 cmd, char *buffer, afs_int32 ntoh_conv)
336 {
337     struct Acl *acl;
338     afs_int32 *lptr, i;
339     char *ptr;
340
341     switch (cmd & 0xffff) {
342     case VIOCGETAL & 0xffff:
343         acl = RParseAcl(buffer);
344         RAclToString(acl, buffer, ntoh_conv);
345         RCleanAcl(acl);
346         break;
347     case VIOCGETVOLSTAT & 0xffff:
348         RFetchVolumeStatus_conversion(buffer, ntoh_conv);
349         break;
350     case VIOCSETVOLSTAT & 0xffff:
351         RFetchVolumeStatus_conversion(buffer, ntoh_conv);
352         break;
353     case VIOCGETTOK & 0xffff:
354         lptr = (afs_int32 *) buffer;
355         /* i is set to sizeof secret ticket */
356         if (ntoh_conv) {
357             i = ntohl(*lptr);
358             *lptr = i;
359         } else {
360             i = *lptr;
361             *lptr = htonl(i);
362         }
363         lptr++;
364         ptr = (char *)lptr;
365         ptr += i;               /* skip over the ticket */
366         lptr = (afs_int32 *) ptr;
367         /* i is set to sizeof clear ticket */
368         if (ntoh_conv) {
369             i = ntohl(*lptr);
370             *lptr = i;
371         } else {
372             i = *lptr;
373             *lptr = htonl(i);
374         }
375         lptr++;
376         ptr = (char *)lptr;
377         RClearToken_convert(ptr, ntoh_conv);
378         ptr += i;               /* sizeof(struct ClearToken) */
379         lptr = (afs_int32 *) ptr;
380         if (ntoh_conv)
381             *lptr = ntohl(*lptr);
382         else
383             *lptr = htonl(*lptr);
384         lptr++;                 /* primary flag */
385         break;
386     case VIOCCKCONN & 0xffff:
387     case VIOC_AFS_MARINER_HOST & 0xffff:
388     case VIOC_VENUSLOG & 0xffff:
389     case VIOC_GETCELLSTATUS & 0xffff:
390     case VIOC_AFS_SYSNAME & 0xffff:
391     case VIOC_EXPORTAFS & 0xffff:
392         lptr = (afs_int32 *) buffer;
393         if (ntoh_conv)
394             *lptr = ntohl(*lptr);
395         else
396             *lptr = htonl(*lptr);
397         break;
398     case VIOCGETCACHEPARMS & 0xffff:
399         lptr = (afs_int32 *) buffer;
400         for (i = 0; i < MAXGCSIZE; i++, lptr++) {
401             if (ntoh_conv)
402                 *lptr = ntohl(*lptr);
403             else
404                 *lptr = htonl(*lptr);
405         }
406         break;
407     case VIOCUNLOG & 0xffff:
408     case VIOCCKSERV & 0xffff:   /* Already in network order */
409     case VIOCCKBACK & 0xffff:
410     case VIOCGETTIME & 0xffff:  /* Obsolete */
411     case VIOCWHEREIS & 0xffff:  /* Already in network order */
412     case VIOCPREFETCH & 0xffff:
413     case VIOCNOP & 0xffff:      /* Obsolete */
414     case VIOCENGROUP & 0xffff:  /* Obsolete */
415     case VIOCDISGROUP & 0xffff: /* Obsolete */
416     case VIOCLISTGROUPS & 0xffff:       /* Obsolete */
417     case VIOCACCESS & 0xffff:
418     case VIOCUNPAG & 0xffff:
419     case VIOCWAITFOREVER & 0xffff:      /* Obsolete */
420     case VIOCSETCACHESIZE & 0xffff:
421     case VIOCFLUSHCB & 0xffff:
422     case VIOCNEWCELL & 0xffff:
423     case VIOCGETCELL & 0xffff:  /* Already in network order */
424     case VIOC_AFS_DELETE_MT_PT & 0xffff:
425     case VIOC_AFS_STAT_MT_PT & 0xffff:
426     case VIOC_FILE_CELL_NAME & 0xffff:  /* no need to convert */
427     case VIOC_GET_WS_CELL & 0xffff:     /* no need to convert */
428     case VIOCGETFID & 0xffff:   /* nothing yet */
429     case VIOCSETAL & 0xffff:
430     case VIOCFLUSH & 0xffff:
431     case VIOCSTAT & 0xffff:
432     case VIOCSETTOK & 0xffff:
433     case VIOC_GET_PRIMARY_CELL & 0xffff:        /* The cell is returned here */
434     case VIOC_SETCELLSTATUS & 0xffff:
435     case VIOC_FLUSHVOLUME & 0xffff:
436         break;
437     default:
438         /* Note that new pioctls are supposed to be in network order! */
439         break;
440     }
441 }