Use the RX_SECIDX_* enums in more places
[openafs.git] / src / butc / tcprocs.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 /* procedures invoked by the rpc stub */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #include <afs/procmgmt.h>
16 #include <roken.h>
17
18 #include <afs/opr.h>
19 #include <rx/rx.h>
20 #include <afs/afsint.h>
21 #include <afs/prs_fs.h>
22 #include <afs/nfs.h>
23 #include <lwp.h>
24 #include <lock.h>
25 #include <afs/cellconfig.h>
26 #include <afs/keys.h>
27 #include <ubik.h>
28 #include <afs/tcdata.h>
29 #include <afs/budb_client.h>
30 #include <afs/bucoord_prototypes.h>
31
32 #include "error_macros.h"
33 #include "butc_xbsa.h"
34 #include "butc_prototypes.h"
35 #include "butc_internal.h"
36
37 static int CopyDumpDesc(struct tc_dumpDesc *, tc_dumpArray *);
38 static int CopyRestoreDesc(struct tc_restoreDesc *, tc_restoreArray *);
39 static int CopyTapeSetDesc(struct tc_tapeSet *, struct tc_tapeSet *);
40
41 int
42 callPermitted(struct rx_call *call)
43 {
44     /*
45      * Before this code can be used, the rx connection, on the bucoord side,
46      * must be changed so that it will set up for token passing instead of
47      * using a simple rx connection that, below, returns a value of
48      * RX_SECIDX_NULL from rx_SecurityClassOf.
49      */
50     return 1;
51 }
52
53 /* -----------------------------
54  * misc. routines
55  * -----------------------------
56  */
57
58 static int
59 CopyDumpDesc(struct tc_dumpDesc *toDump, tc_dumpArray *fromDump)
60 {
61     struct tc_dumpDesc *toPtr, *fromPtr;
62     int i;
63
64     toPtr = toDump;
65     fromPtr = fromDump->tc_dumpArray_val;
66     for (i = 0; i < fromDump->tc_dumpArray_len; i++) {
67         toPtr->vid = fromPtr->vid;
68         toPtr->vtype = fromPtr->vtype;
69         toPtr->partition = fromPtr->partition;
70         toPtr->date = fromPtr->date;
71         toPtr->cloneDate = fromPtr->cloneDate;
72         toPtr->hostAddr = fromPtr->hostAddr;
73         strcpy(toPtr->name, fromPtr->name);
74         fromPtr++;
75         toPtr++;
76     }
77     return 0;
78 }
79
80
81 static int
82 CopyRestoreDesc(struct tc_restoreDesc *toRestore, tc_restoreArray *fromRestore)
83 {
84     struct tc_restoreDesc *toPtr, *fromPtr;
85     int i;
86
87     toPtr = toRestore;
88     fromPtr = fromRestore->tc_restoreArray_val;
89     for (i = 0; i < fromRestore->tc_restoreArray_len; i++) {
90         toPtr->flags = fromPtr->flags;
91         toPtr->position = fromPtr->position;
92         strcpy(toPtr->tapeName, fromPtr->tapeName);
93         toPtr->dbDumpId = fromPtr->dbDumpId;
94         toPtr->initialDumpId = fromPtr->initialDumpId;
95         toPtr->origVid = fromPtr->origVid;
96         toPtr->vid = fromPtr->vid;
97         toPtr->partition = fromPtr->partition;
98         toPtr->dumpLevel = fromPtr->dumpLevel;
99         toPtr->hostAddr = fromPtr->hostAddr;
100         strcpy(toPtr->newName, fromPtr->newName);
101         strcpy(toPtr->oldName, fromPtr->oldName);
102         fromPtr++;
103         toPtr++;
104
105     }
106     return 0;
107 }
108
109 static int
110 CopyTapeSetDesc(struct tc_tapeSet *toPtr, struct tc_tapeSet *fromPtr)
111 {
112
113     toPtr->id = fromPtr->id;
114     toPtr->maxTapes = fromPtr->maxTapes;
115     toPtr->a = fromPtr->a;
116     toPtr->b = fromPtr->b;
117     strcpy(toPtr->tapeServer, fromPtr->tapeServer);
118     strcpy(toPtr->format, fromPtr->format);
119
120     toPtr->expDate = fromPtr->expDate;
121     toPtr->expType = fromPtr->expType;
122     return 0;
123 }
124
125 /* -------------------------
126  * butc - interface routines - alphabetic order
127  * -------------------------
128  */
129
130 afs_int32
131 STC_LabelTape(struct rx_call *acid, struct tc_tapeLabel *label, afs_uint32 *taskId)
132 {
133 #ifdef AFS_PTHREAD_ENV
134     pthread_t pid;
135     pthread_attr_t tattr;
136     AFS_SIGSET_DECL;
137 #else
138     PROCESS pid;
139 #endif
140     struct labelTapeIf *ptr;
141     statusP statusPtr = NULL;
142     afs_int32 code;
143
144 #ifdef xbsa
145     if (CONF_XBSA)
146         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
147 #endif
148
149     if (callPermitted(acid) == 0)
150         return (TC_NOTPERMITTED);
151
152     ptr = malloc(sizeof(*ptr));
153     if (!ptr)
154         ERROR_EXIT(TC_NOMEMORY);
155     memcpy(&ptr->label, label, sizeof(ptr->label));
156
157     /* set up the status node */
158     *taskId = allocTaskId();    /* for bucoord */
159     ptr->taskId = *taskId;
160
161     statusPtr = createStatusNode();
162     if (!statusPtr)
163         ERROR_EXIT(TC_INTERNALERROR);
164
165     lock_Status();
166     statusPtr->taskId = *taskId;
167     statusPtr->lastPolled = time(0);
168     statusPtr->flags &= ~STARTING;      /* ok to examine */
169     strncpy(statusPtr->taskName, "Labeltape", sizeof(statusPtr->taskName));
170     unlock_Status();
171
172     /* create the LWP to do the real work behind the scenes */
173 #ifdef AFS_PTHREAD_ENV
174     code = pthread_attr_init(&tattr);
175     if (code)
176         ERROR_EXIT(code);
177
178     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
179     if (code)
180         ERROR_EXIT(code);
181
182     AFS_SIGSET_CLEAR();
183     code = pthread_create(&pid, &tattr, Labeller, ptr);
184     AFS_SIGSET_RESTORE();
185 #else
186     code =
187         LWP_CreateProcess(Labeller, 32768, 1, (void *)ptr, "labeller process",
188                           &pid);
189 #endif
190
191   error_exit:
192     if (code) {
193         if (statusPtr)
194             deleteStatusNode(statusPtr);
195         if (ptr)
196             free(ptr);
197     }
198
199     return (code);
200 }
201
202 /* STC_PerformDump
203  *      Tape coordinator server routine to do a dump
204  */
205
206 afs_int32
207 STC_PerformDump(struct rx_call *rxCallId, struct tc_dumpInterface *tcdiPtr, tc_dumpArray *tc_dumpArrayPtr, afs_int32 *taskId)
208 {
209     struct dumpNode *newNode = 0;
210     statusP statusPtr = 0;
211 #ifdef AFS_PTHREAD_ENV
212     pthread_t pid;
213     pthread_attr_t tattr;
214     AFS_SIGSET_DECL;
215 #else
216     PROCESS pid;
217 #endif
218     afs_int32 code = 0;
219
220     if (callPermitted(rxCallId) == 0)
221         return (TC_NOTPERMITTED);
222
223     /* should be verifying parameter validity */
224     *taskId = 0;
225
226     /* this creates a node in list, alots an id for it and prepares it for locking */
227     CreateNode(&newNode);
228
229     /*set up the parameters in the node, to be used by LWP */
230     strcpy(newNode->dumpSetName, tcdiPtr->dumpName);
231
232     newNode->dumpName = strdup(tcdiPtr->dumpPath);
233     newNode->volumeSetName = strdup(tcdiPtr->volumeSetName);
234
235     CopyTapeSetDesc(&(newNode->tapeSetDesc), &tcdiPtr->tapeSet);
236
237     newNode->dumps = malloc(sizeof(struct tc_dumpDesc) *
238                             tc_dumpArrayPtr->tc_dumpArray_len);
239     newNode->arraySize = tc_dumpArrayPtr->tc_dumpArray_len;
240     CopyDumpDesc(newNode->dumps, tc_dumpArrayPtr);
241
242     newNode->parent = tcdiPtr->parentDumpId;
243     newNode->level = tcdiPtr->dumpLevel;
244     newNode->doAppend = tcdiPtr->doAppend;
245 #ifdef xbsa
246     if (CONF_XBSA)
247         newNode->doAppend = 0;  /* Append flag is ignored if talking to XBSA */
248 #endif
249
250     /* create the status node */
251     statusPtr = createStatusNode();
252     if (!statusPtr)
253         ERROR_EXIT(TC_INTERNALERROR);
254
255     lock_Status();
256     statusPtr->taskId = newNode->taskID;
257     statusPtr->lastPolled = time(0);
258     statusPtr->flags &= ~STARTING;      /* ok to examine */
259     strncpy(statusPtr->taskName, "Dump", sizeof(statusPtr->taskName));
260     unlock_Status();
261
262     newNode->statusNodePtr = statusPtr;
263
264     /* create the LWP to do the real work behind the scenes */
265 #ifdef AFS_PTHREAD_ENV
266     code = pthread_attr_init(&tattr);
267     if (code)
268         ERROR_EXIT(code);
269
270     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
271     if (code)
272         ERROR_EXIT(code);
273
274     AFS_SIGSET_CLEAR();
275     code = pthread_create(&pid, &tattr, Dumper, newNode);
276     AFS_SIGSET_RESTORE();
277 #else
278     code =
279         LWP_CreateProcess(Dumper, 32768, 1, (void *)newNode, "dumper process",
280                           &pid);
281 #endif
282     if (code)
283         ERROR_EXIT(code);
284
285     *taskId = newNode->taskID;
286
287   error_exit:
288     if (code) {
289         if (statusPtr)
290             deleteStatusNode(statusPtr);
291         FreeNode(newNode->taskID);      /*  failed to create LWP to do the dump. */
292     }
293
294     return (code);
295 }
296
297 afs_int32
298 STC_PerformRestore(struct rx_call *acid, char *dumpSetName, tc_restoreArray *arestores, afs_int32 *taskID)
299 {
300     struct dumpNode *newNode;
301     statusP statusPtr;
302     afs_int32 code = 0;
303 #ifdef AFS_PTHREAD_ENV
304     pthread_t pid;
305     pthread_attr_t tattr;
306     AFS_SIGSET_DECL;
307 #else
308     PROCESS pid;
309 #endif
310
311     if (callPermitted(acid) == 0)
312         return (TC_NOTPERMITTED);
313
314     /* should  verify parameter validity */
315
316     /* this creates a node in list, alots an id for it and prepares it for locking */
317     CreateNode(&newNode);
318
319     newNode->restores = malloc(sizeof(struct tc_restoreDesc) *
320                                arestores->tc_restoreArray_len);
321     newNode->arraySize = arestores->tc_restoreArray_len;
322     CopyRestoreDesc(newNode->restores, arestores);
323     *taskID = newNode->taskID;
324
325     /* should log the intent */
326
327     /* create the status node */
328     statusPtr = createStatusNode();
329     if (!statusPtr)
330         ERROR_EXIT(TC_INTERNALERROR);
331
332     lock_Status();
333     statusPtr->taskId = newNode->taskID;
334     statusPtr->flags &= ~STARTING;      /* ok to examine */
335     statusPtr->lastPolled = time(0);
336     strncpy(statusPtr->taskName, "Restore", sizeof(statusPtr->taskName));
337     unlock_Status();
338
339     newNode->statusNodePtr = statusPtr;
340
341     /* create the LWP to do the real work behind the scenes */
342 #ifdef AFS_PTHREAD_ENV
343     code = pthread_attr_init(&tattr);
344     if (code)
345         ERROR_EXIT(code);
346
347     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
348     if (code)
349         ERROR_EXIT(code);
350
351     AFS_SIGSET_CLEAR();
352     code = pthread_create(&pid, &tattr, Restorer, newNode);
353     AFS_SIGSET_RESTORE();
354 #else
355     code =
356         LWP_CreateProcess(Restorer, 65368, 1, (void *)newNode,
357                           "restorer process", &pid);
358 #endif
359
360   error_exit:
361     if (code) {
362         if (statusPtr)
363             deleteStatusNode(statusPtr);
364         FreeNode(newNode->taskID);      /*  failed to create LWP to do the dump. */
365     }
366
367     return (code);
368 }
369
370 afs_int32
371 STC_ReadLabel(struct rx_call *acid, struct tc_tapeLabel *label, afs_uint32 *taskId)
372 {
373     afs_int32 code;
374
375 #ifdef xbsa
376     if (CONF_XBSA)
377         return (TC_BADTASK);    /* ReadLabel does not apply if XBSA */
378 #endif
379
380     if (callPermitted(acid) == 0)
381         return (TC_NOTPERMITTED);
382
383     code = ReadLabel(label);    /* Synchronous */
384     return code;
385 }
386
387 /* STC_RestoreDb
388  *      restore the backup database from tape
389  */
390
391 afs_int32
392 STC_RestoreDb(struct rx_call *rxCall, afs_uint32 *taskId)
393 {
394 #ifdef AFS_PTHREAD_ENV
395     pthread_t pid;
396     pthread_attr_t tattr;
397     AFS_SIGSET_DECL;
398 #else
399     PROCESS pid;
400 #endif
401     statusP statusPtr;
402     afs_int32 code = 0;
403
404 #ifdef xbsa
405     if (CONF_XBSA)
406         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
407 #endif
408
409     if (callPermitted(rxCall) == 0)
410         return (TC_NOTPERMITTED);
411
412     *taskId = allocTaskId();
413
414     /* create the status node */
415     statusPtr = createStatusNode();
416     if (!statusPtr)
417         ERROR_EXIT(TC_INTERNALERROR);
418
419     lock_Status();
420     statusPtr->taskId = *taskId;
421     statusPtr->flags &= ~STARTING;      /* ok to examine */
422     statusPtr->lastPolled = time(0);
423     strncpy(statusPtr->taskName, "RestoreDb", sizeof(statusPtr->taskName));
424     unlock_Status();
425
426 #ifdef AFS_PTHREAD_ENV
427     code = pthread_attr_init(&tattr);
428     if (code)
429         ERROR_EXIT(code);
430
431     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
432     if (code)
433         ERROR_EXIT(code);
434
435     AFS_SIGSET_CLEAR();
436     code = pthread_create(&pid, &tattr, restoreDbFromTape, (void *)(intptr_t)*taskId);
437     AFS_SIGSET_RESTORE();
438 #else
439     code =
440         LWP_CreateProcess(restoreDbFromTape, 32768, 1, (void *)(intptr_t)*taskId,
441                           "Db restore", &pid);
442 #endif
443
444   error_exit:
445     if (code) {
446         if (statusPtr)
447             deleteStatusNode(statusPtr);
448     }
449
450     return (code);
451 }
452
453 /* STC_SaveDb
454  *      restore the backup database from tape
455  */
456
457 afs_int32
458 STC_SaveDb(struct rx_call *rxCall, Date archiveTime, afs_uint32 *taskId)
459 {
460 #ifdef AFS_PTHREAD_ENV
461     pthread_t pid;
462     pthread_attr_t tattr;
463     AFS_SIGSET_DECL;
464 #else
465     PROCESS pid;
466 #endif
467     statusP statusPtr = NULL;
468     afs_int32 code = 0;
469     struct saveDbIf *ptr;
470
471 #ifdef xbsa
472     if (CONF_XBSA)
473         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
474 #endif
475
476     if (callPermitted(rxCall) == 0)
477         return (TC_NOTPERMITTED);
478
479     *taskId = allocTaskId();
480
481     ptr = malloc(sizeof(struct saveDbIf));
482     if (!ptr)
483         ERROR_EXIT(TC_NOMEMORY);
484     ptr->archiveTime = archiveTime;
485     ptr->taskId = *taskId;
486
487     /* create the status node */
488     statusPtr = createStatusNode();
489     if (!statusPtr)
490         ERROR_EXIT(TC_INTERNALERROR);
491
492     lock_Status();
493     statusPtr->taskId = *taskId;
494     statusPtr->lastPolled = time(0);
495     statusPtr->flags &= ~STARTING;      /* ok to examine */
496     strncpy(statusPtr->taskName, "SaveDb", sizeof(statusPtr->taskName));
497     unlock_Status();
498
499     ptr->statusPtr = statusPtr;
500
501 #ifdef AFS_PTHREAD_ENV
502     code = pthread_attr_init(&tattr);
503     if (code)
504         ERROR_EXIT(code);
505
506     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
507     if (code)
508         ERROR_EXIT(code);
509
510     AFS_SIGSET_CLEAR();
511     code = pthread_create(&pid, &tattr, saveDbToTape, ptr);
512     AFS_SIGSET_RESTORE();
513 #else
514     code = LWP_CreateProcess(saveDbToTape, 32768, 1, ptr, "Db save", &pid);
515 #endif
516
517   error_exit:
518     if (code) {
519         if (statusPtr)
520             deleteStatusNode(statusPtr);
521         if (ptr)
522             free(ptr);
523     }
524
525     return (code);
526 }
527
528
529 /* STC_ScanDumps
530  *      read a dump (maybe more than one tape), and print out a summary
531  *      of its contents. If the flag is set, add to the database.
532  * entry:
533  *      addDbFlag - if set, the information will be added to the database
534  */
535
536 afs_int32
537 STC_ScanDumps(struct rx_call *acid, afs_int32 addDbFlag, afs_uint32 *taskId)
538 {
539 #ifdef AFS_PTHREAD_ENV
540     pthread_t pid;
541     pthread_attr_t tattr;
542     AFS_SIGSET_DECL;
543 #else
544     PROCESS pid;
545 #endif
546     struct scanTapeIf *ptr;
547     statusP statusPtr = NULL;
548     afs_int32 code = 0;
549
550 #ifdef xbsa
551     if (CONF_XBSA)
552         return (TC_BADTASK);    /* ScanDumps does not apply if XBSA */
553 #endif
554
555     if (callPermitted(acid) == 0)
556         return (TC_NOTPERMITTED);
557
558     *taskId = allocTaskId();
559
560     ptr = malloc(sizeof(*ptr));
561     if (!ptr)
562         ERROR_EXIT(TC_NOMEMORY);
563     ptr->addDbFlag = addDbFlag;
564     ptr->taskId = *taskId;
565
566     /* create the status node */
567     statusPtr = createStatusNode();
568     if (!statusPtr)
569         ERROR_EXIT(TC_INTERNALERROR);
570
571     lock_Status();
572     statusPtr->taskId = *taskId;
573     statusPtr->lastPolled = time(0);
574     statusPtr->flags &= ~STARTING;      /* ok to examine */
575     strncpy(statusPtr->taskName, "Scantape", sizeof(statusPtr->taskName));
576     unlock_Status();
577
578 #ifdef AFS_PTHREAD_ENV
579     code = pthread_attr_init(&tattr);
580     if (code)
581         ERROR_EXIT(code);
582
583     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
584     if (code)
585         ERROR_EXIT(code);
586
587     AFS_SIGSET_CLEAR();
588     code = pthread_create(&pid, &tattr, ScanDumps, ptr);
589     AFS_SIGSET_RESTORE();
590 #else
591     code =
592         LWP_CreateProcess(ScanDumps, 32768, 1, ptr, "scandump process", &pid);
593 #endif
594
595   error_exit:
596     if (code) {
597         if (statusPtr)
598             deleteStatusNode(statusPtr);
599         if (ptr)
600             free(ptr);
601     }
602
603     return code;
604 }
605
606 /* STC_TCInfo
607  *      return information about the tape coordinator. Currently this
608  *      is just the version number of the interface
609  */
610
611 afs_int32
612 STC_TCInfo(struct rx_call *acid, struct tc_tcInfo *tciptr)
613 {
614     if (callPermitted(acid) == 0)
615         return (TC_NOTPERMITTED);
616
617     tciptr->tcVersion = CUR_BUTC_VERSION;
618     return (0);
619 }
620
621 /* STC_DeleteDump
622  */
623 afs_int32
624 STC_DeleteDump(struct rx_call *acid, afs_uint32 dumpID, afs_uint32 *taskId)
625 {
626     afs_int32 code = TC_BADTASK;        /* If not compiled -Dxbsa then fail */
627 #ifdef xbsa
628     struct deleteDumpIf *ptr = 0;
629     statusP statusPtr = 0;
630 #ifdef AFS_PTHREAD_ENV
631     pthread_t pid;
632     pthread_attr_t tattr;
633     AFS_SIGSET_DECL;
634 #else
635     PROCESS pid;
636 #endif
637 #endif
638
639     *taskId = 0;
640     if (!CONF_XBSA)
641         return (TC_BADTASK);    /* Only do if butc is started as XBSA */
642
643 #ifdef xbsa
644     code = 0;
645     if (callPermitted(acid) == 0)
646         return (TC_NOTPERMITTED);
647
648     ptr = malloc(sizeof(*ptr));
649     if (!ptr)
650         ERROR_EXIT(TC_NOMEMORY);
651
652     *taskId = allocTaskId();
653     ptr->dumpID = dumpID;
654     ptr->taskId = *taskId;
655
656     statusPtr = createStatusNode();
657     if (!statusPtr)
658         ERROR_EXIT(TC_INTERNALERROR);
659
660     lock_Status();
661     statusPtr->taskId = *taskId;
662     statusPtr->lastPolled = time(0);
663     statusPtr->flags &= ~STARTING;
664     strncpy(statusPtr->taskName, "DeleteDump", sizeof(statusPtr->taskName));
665     unlock_Status();
666
667 #ifdef AFS_PTHREAD_ENV
668     code = pthread_attr_init(&tattr);
669     if (code)
670         ERROR_EXIT(code);
671
672     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
673     if (code)
674         ERROR_EXIT(code);
675
676     AFS_SIGSET_CLEAR();
677     code = pthread_create(&pid, &tattr, DeleteDump, ptr);
678     AFS_SIGSET_RESTORE();
679 #else
680     code =
681         LWP_CreateProcess(DeleteDump, 32768, 1, ptr, "deletedump process",
682                           &pid);
683 #endif
684
685   error_exit:
686     if (code) {
687         if (statusPtr)
688             deleteStatusNode(statusPtr);
689         if (ptr)
690             free(ptr);
691     }
692 #endif /* xbsa */
693
694     return (code);
695 }
696