pr-initialize-silence-error-on-empty-dir-20031211
[openafs.git] / src / ptserver / ptuser.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 #if defined(UKERNEL)
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID
18     ("$Header$");
19
20 #if defined(UKERNEL)
21 #include "afs/sysincludes.h"
22 #include "afs_usrops.h"
23 #include "afsincludes.h"
24 #include "afs/stds.h"
25 #include "rx/rx.h"
26 #include "rx/xdr.h"
27 #include "rx/rxkad.h"
28 #include "afs/auth.h"
29 #include "afs/cellconfig.h"
30 #include "afs/afsutil.h"
31 #include "afs/ptclient.h"
32 #include "afs/pterror.h"
33 #else /* defined(UKERNEL) */
34 #include <afs/stds.h>
35 #include <ctype.h>
36 #include <sys/types.h>
37 #ifdef AFS_NT40_ENV
38 #include <winsock2.h>
39 #else
40 #include <netinet/in.h>
41 #endif
42 #include <stdio.h>
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #else
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
49 #endif
50 #include <rx/rx.h>
51 #include <rx/xdr.h>
52 #include <rx/rxkad.h>
53 #include <afs/auth.h>
54 #include <afs/cellconfig.h>
55 #include <afs/afsutil.h>
56 #include "ptclient.h"
57 #include "pterror.h"
58 #endif /* defined(UKERNEL) */
59
60
61 struct ubik_client *pruclient = 0;
62 static afs_int32 lastLevel;     /* security level pruclient, if any */
63
64 static char *whoami = "libprot";
65
66 afs_int32
67 pr_Initialize(secLevel, confDir, cell)
68      IN afs_int32 secLevel;
69      IN char *confDir;
70      IN char *cell;
71 {
72     afs_int32 code;
73     struct rx_connection *serverconns[MAXSERVERS];
74     struct rx_securityClass *sc[3];
75     static struct afsconf_dir *tdir = 0;        /* only do this once */
76     static char tconfDir[100];
77     struct ktc_token ttoken;
78     afs_int32 scIndex;
79     static struct afsconf_cell info;
80     afs_int32 i;
81     char cellstr[64];
82
83     initialize_PT_error_table();
84     initialize_RXK_error_table();
85     initialize_ACFG_error_table();
86     initialize_KTC_error_table();
87
88     if (strcmp(confDir, tconfDir)) {
89         /*
90          * Different conf dir; force re-evaluation.
91          */
92         tdir = (struct afsconf_dir *)0;
93         pruclient = (struct ubik_client *)0;
94     }
95     if (tdir == 0) {
96         strncpy(tconfDir, confDir, sizeof(tconfDir));
97 #if defined(UKERNEL)
98         tdir = afs_cdir;
99         if (!cell) {
100             cell = afs_LclCellName;
101         }
102 #else /* defined(UKERNEL) */
103         tdir = afsconf_Open(confDir);
104         if (!tdir) {
105             if (confDir && strcmp(confDir, ""))
106                 fprintf(stderr,
107                         "libprot: Could not open configuration directory: %s.\n",
108                         confDir);
109             return -1;
110         }
111
112         if (!cell) {
113             code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
114             if (code) {
115                 fprintf(stderr,
116                         "vos: can't get local cell name - check %s/%s\n",
117                         confDir, AFSDIR_THISCELL_FILE);
118                 exit(1);
119             }
120             cell = cellstr;
121         }
122 #endif /* defined(UKERNEL) */
123
124         code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
125         if (code) {
126             fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
127                     cell, confDir, AFSDIR_CELLSERVDB_FILE);
128             return code;
129         }
130     }
131
132     /* If we already have a client and it is at the security level we
133      * want, don't get a new one. Unless the security level is 2 in
134      * which case we will get one (and re-read the key file).
135      */
136     if (pruclient && (lastLevel == secLevel) && (secLevel != 2))
137         return 0;
138
139     code = rx_Init(0);
140     if (code) {
141         fprintf(stderr, "libprot:  Could not initialize rx.\n");
142         return code;
143     }
144
145     scIndex = secLevel;
146     sc[0] = 0;
147     sc[1] = 0;
148     sc[2] = 0;
149     /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
150      * to force use of the KeyFile.  secLevel == 0 implies -noauth was
151      * specified. */
152     if ((secLevel == 2) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
153         /* If secLevel is two assume we're on a file server and use
154          * ClientAuthSecure if possible. */
155         code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
156         if (code)
157             fprintf(stderr,
158                     "libprot: clientauthsecure returns %d %s"
159                     " (so trying noauth)\n", code, error_message(code));
160         if (code)
161             scIndex = 0;        /* use noauth */
162         if (scIndex != 2)
163             /* if there was a problem, an unauthenticated conn is returned */
164             sc[scIndex] = sc[2];
165     } else if (secLevel > 0) {
166         struct ktc_principal sname;
167         strcpy(sname.cell, info.name);
168         sname.instance[0] = 0;
169         strcpy(sname.name, "afs");
170         code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
171         if (code)
172             scIndex = 0;
173         else {
174             if (ttoken.kvno >= 0 && ttoken.kvno <= 255)
175                 /* this is a kerberos ticket, set scIndex accordingly */
176                 scIndex = 2;
177             else {
178                 fprintf(stderr,
179                         "libprot: funny kvno (%d) in ticket, proceeding\n",
180                         ttoken.kvno);
181                 scIndex = 2;
182             }
183             sc[2] =
184                 rxkad_NewClientSecurityObject(secLevel, &ttoken.sessionKey,
185                                               ttoken.kvno, ttoken.ticketLen,
186                                               ttoken.ticket);
187         }
188     }
189     if (scIndex == 1)
190         return PRBADARG;
191     if ((scIndex == 0) && (sc[0] == 0))
192         sc[0] = rxnull_NewClientSecurityObject();
193     if ((scIndex == 0) && (secLevel != 0))
194         com_err(whoami, code,
195                 "Could not get afs tokens, running unauthenticated.");
196
197     memset(serverconns, 0, sizeof(serverconns));        /* terminate list!!! */
198     for (i = 0; i < info.numServers; i++)
199         serverconns[i] =
200             rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
201                              info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
202                              scIndex);
203
204     code = ubik_ClientInit(serverconns, &pruclient);
205     if (code) {
206         com_err(whoami, code, "ubik client init failed.");
207         return code;
208     }
209     lastLevel = scIndex;
210
211     code = rxs_Release(sc[scIndex]);
212     return code;
213 }
214
215
216 pr_End()
217 {
218     int code = 0;
219
220     if (pruclient) {
221         code = ubik_ClientDestroy(pruclient);
222         pruclient = 0;
223     }
224     return code;
225 }
226
227
228
229 pr_CreateUser(name, id)
230      char name[PR_MAXNAMELEN];
231      afs_int32 *id;
232 {
233     register afs_int32 code;
234
235     stolower(name);
236     if (*id) {
237         code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, 0);
238         return code;
239     } else {
240         code = ubik_Call(PR_NewEntry, pruclient, 0, name, 0, 0, id);
241         return code;
242     }
243
244 }
245
246 pr_CreateGroup(name, owner, id)
247      char name[PR_MAXNAMELEN];
248      char owner[PR_MAXNAMELEN];
249      afs_int32 *id;
250 {
251     register afs_int32 code;
252     afs_int32 oid = 0;
253     afs_int32 flags = 0;
254
255     stolower(name);
256     if (owner) {
257         code = pr_SNameToId(owner, &oid);
258         if (code)
259             return code;
260         if (oid == ANONYMOUSID)
261             return PRNOENT;
262     }
263     flags |= PRGRP;
264     if (*id) {
265         code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, oid);
266         return code;
267     } else {
268         code = ubik_Call(PR_NewEntry, pruclient, 0, name, flags, oid, id);
269         return code;
270     }
271 }
272
273 pr_Delete(name)
274      char *name;
275 {
276     register afs_int32 code;
277     afs_int32 id;
278
279     stolower(name);
280     code = pr_SNameToId(name, &id);
281     if (code)
282         return code;
283     if (id == ANONYMOUSID)
284         return PRNOENT;
285     code = ubik_Call(PR_Delete, pruclient, 0, id);
286     return code;
287 }
288
289 pr_DeleteByID(id)
290      afs_int32 id;
291 {
292     register afs_int32 code;
293
294     code = ubik_Call(PR_Delete, pruclient, 0, id);
295     return code;
296 }
297
298 pr_AddToGroup(user, group)
299      char *user;
300      char *group;
301 {
302     register afs_int32 code;
303     namelist lnames;
304     idlist lids;
305
306     lnames.namelist_len = 2;
307     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
308     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
309     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
310     lids.idlist_val = 0;
311     lids.idlist_len = 0;
312     code = pr_NameToId(&lnames, &lids);
313     if (code)
314         goto done;
315     /* if here, still could be missing an entry */
316     if (lids.idlist_val[0] == ANONYMOUSID
317         || lids.idlist_val[1] == ANONYMOUSID) {
318         code = PRNOENT;
319         goto done;
320     }
321     code =
322         ubik_Call(PR_AddToGroup, pruclient, 0, lids.idlist_val[0],
323                   lids.idlist_val[1]);
324   done:
325     if (lnames.namelist_val)
326         free(lnames.namelist_val);
327     if (lids.idlist_val)
328         free(lids.idlist_val);
329     return code;
330 }
331
332 pr_RemoveUserFromGroup(user, group)
333      char *user;
334      char *group;
335 {
336     register afs_int32 code;
337     namelist lnames;
338     idlist lids;
339
340     lnames.namelist_len = 2;
341     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
342     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
343     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
344     lids.idlist_val = 0;
345     lids.idlist_len = 0;
346     code = pr_NameToId(&lnames, &lids);
347     if (code)
348         goto done;
349
350     if (lids.idlist_val[0] == ANONYMOUSID
351         || lids.idlist_val[1] == ANONYMOUSID) {
352         code = PRNOENT;
353         goto done;
354     }
355     code =
356         ubik_Call(PR_RemoveFromGroup, pruclient, 0, lids.idlist_val[0],
357                   lids.idlist_val[1]);
358   done:
359     if (lnames.namelist_val)
360         free(lnames.namelist_val);
361     if (lids.idlist_val)
362         free(lids.idlist_val);
363     return code;
364
365 }
366
367 pr_NameToId(names, ids)
368      namelist *names;
369      idlist *ids;
370 {
371     register afs_int32 code;
372     register afs_int32 i;
373
374     for (i = 0; i < names->namelist_len; i++)
375         stolower(names->namelist_val[i]);
376     code = ubik_Call(PR_NameToID, pruclient, 0, names, ids);
377     return code;
378 }
379
380 pr_SNameToId(name, id)
381      char name[PR_MAXNAMELEN];
382      afs_int32 *id;
383 {
384     namelist lnames;
385     idlist lids;
386     register afs_int32 code;
387
388     lids.idlist_len = 0;
389     lids.idlist_val = 0;
390     lnames.namelist_len = 1;
391     lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
392     stolower(name);
393     strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
394     code = ubik_Call(PR_NameToID, pruclient, 0, &lnames, &lids);
395     if (lids.idlist_val) {
396         *id = *lids.idlist_val;
397         free(lids.idlist_val);
398     }
399     if (lnames.namelist_val)
400         free(lnames.namelist_val);
401     return code;
402 }
403
404
405
406 pr_IdToName(ids, names)
407      idlist *ids;
408      namelist *names;
409 {
410     register afs_int32 code;
411
412     code = ubik_Call(PR_IDToName, pruclient, 0, ids, names);
413     return code;
414 }
415
416 pr_SIdToName(id, name)
417      afs_int32 id;
418      char name[PR_MAXNAMELEN];
419 {
420     namelist lnames;
421     idlist lids;
422     register afs_int32 code;
423
424     lids.idlist_len = 1;
425     lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
426     *lids.idlist_val = id;
427     lnames.namelist_len = 0;
428     lnames.namelist_val = 0;
429     code = ubik_Call(PR_IDToName, pruclient, 0, &lids, &lnames);
430     if (lnames.namelist_val) {
431         strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
432         free(lnames.namelist_val);
433     }
434     if (lids.idlist_val)
435         free(lids.idlist_val);
436     return code;
437 }
438
439
440
441 pr_GetCPS(id, CPS)
442      afs_int32 id;
443      prlist *CPS;
444 {
445     register afs_int32 code;
446     afs_int32 over;
447
448     over = 0;
449     code = ubik_Call(PR_GetCPS, pruclient, 0, id, CPS, &over);
450     if (code != PRSUCCESS)
451         return code;
452     if (over) {
453         /* do something about this, probably make a new call */
454         /* don't forget there's a hard limit in the interface */
455         fprintf(stderr, "membership list for id %d exceeds display limit\n",
456                 id);
457     }
458     return 0;
459 }
460
461
462 pr_GetCPS2(id, host, CPS)
463      afs_int32 id;
464      afs_int32 host;
465      prlist *CPS;
466 {
467     register afs_int32 code;
468     afs_int32 over;
469
470     over = 0;
471     code = ubik_Call(PR_GetCPS2, pruclient, 0, id, host, CPS, &over);
472     if (code != PRSUCCESS)
473         return code;
474     if (over) {
475         /* do something about this, probably make a new call */
476         /* don't forget there's a hard limit in the interface */
477         fprintf(stderr, "membership list for id %d exceeds display limit\n",
478                 id);
479     }
480     return 0;
481 }
482
483 pr_GetHostCPS(host, CPS)
484      afs_int32 host;
485      prlist *CPS;
486 {
487     register afs_int32 code;
488     afs_int32 over;
489
490     over = 0;
491     code = ubik_Call(PR_GetHostCPS, pruclient, 0, host, CPS, &over);
492     if (code != PRSUCCESS)
493         return code;
494     if (over) {
495         /* do something about this, probably make a new call */
496         /* don't forget there's a hard limit in the interface */
497         fprintf(stderr,
498                 "membership list for host id %d exceeds display limit\n",
499                 host);
500     }
501     return 0;
502 }
503
504
505 pr_ListMembers(group, lnames)
506      char *group;
507      namelist *lnames;
508 {
509     register afs_int32 code;
510     afs_int32 gid;
511
512     code = pr_SNameToId(group, &gid);
513     if (code)
514         return code;
515     if (gid == ANONYMOUSID)
516         return PRNOENT;
517     code = pr_IDListMembers(gid, lnames);
518     return code;
519 }
520
521 pr_ListOwned(oid, lnames, moreP)
522      afs_int32 oid;
523      namelist *lnames;
524      afs_int32 *moreP;
525 {
526     register afs_int32 code;
527     prlist alist;
528     idlist *lids;
529
530     alist.prlist_len = 0;
531     alist.prlist_val = 0;
532     code = ubik_Call(PR_ListOwned, pruclient, 0, oid, &alist, moreP);
533     if (code)
534         return code;
535     if (*moreP == 1) {
536         /* Remain backwards compatible when moreP was a T/F bit */
537         fprintf(stderr, "membership list for id %d exceeds display limit\n",
538                 oid);
539         *moreP = 0;
540     }
541     lids = (idlist *) & alist;
542     code = pr_IdToName(lids, lnames);
543     if (code)
544         return code;
545     if (alist.prlist_val)
546         free(alist.prlist_val);
547     return PRSUCCESS;
548 }
549
550 pr_IDListMembers(gid, lnames)
551      afs_int32 gid;
552      namelist *lnames;
553 {
554     register afs_int32 code;
555     prlist alist;
556     idlist *lids;
557     afs_int32 over;
558
559     alist.prlist_len = 0;
560     alist.prlist_val = 0;
561     code = ubik_Call(PR_ListElements, pruclient, 0, gid, &alist, &over);
562     if (code)
563         return code;
564     if (over) {
565         fprintf(stderr, "membership list for id %d exceeds display limit\n",
566                 gid);
567     }
568     lids = (idlist *) & alist;
569     code = pr_IdToName(lids, lnames);
570     if (code)
571         return code;
572     if (alist.prlist_val)
573         free(alist.prlist_val);
574     return PRSUCCESS;
575 }
576
577
578 pr_ListEntry(id, aentry)
579      afs_int32 id;
580      struct prcheckentry *aentry;
581 {
582     register afs_int32 code;
583
584     code = ubik_Call(PR_ListEntry, pruclient, 0, id, aentry);
585     return code;
586 }
587
588 afs_int32
589 pr_ListEntries(flag, startindex, nentries, entries, nextstartindex)
590      afs_int32 startindex;
591      afs_int32 *nentries;
592      struct prlistentries **entries;
593      afs_int32 *nextstartindex;
594 {
595     afs_int32 code;
596     prentries bulkentries;
597
598     *nentries = 0;
599     *entries = NULL;
600     *nextstartindex = -1;
601     bulkentries.prentries_val = 0;
602     bulkentries.prentries_len = 0;
603
604     code =
605         ubik_Call(PR_ListEntries, pruclient, 0, flag, startindex,
606                   &bulkentries, nextstartindex);
607     *nentries = bulkentries.prentries_len;
608     *entries = bulkentries.prentries_val;
609     return code;
610 }
611
612 pr_CheckEntryByName(name, id, owner, creator)
613      char *name;
614      afs_int32 *id;
615      char *owner;
616      char *creator;
617 {
618     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
619     register afs_int32 code;
620     struct prcheckentry aentry;
621
622     code = pr_SNameToId(name, id);
623     if (code)
624         return code;
625     if (*id == ANONYMOUSID)
626         return PRNOENT;
627     code = ubik_Call(PR_ListEntry, pruclient, 0, *id, &aentry);
628     if (code)
629         return code;
630     /* this should be done in one RPC, but I'm lazy. */
631     code = pr_SIdToName(aentry.owner, owner);
632     if (code)
633         return code;
634     code = pr_SIdToName(aentry.creator, creator);
635     if (code)
636         return code;
637     return PRSUCCESS;
638 }
639
640 pr_CheckEntryById(name, id, owner, creator)
641      char *name;
642      afs_int32 id;
643      char *owner;
644      char *creator;
645 {
646     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
647     register afs_int32 code;
648     struct prcheckentry aentry;
649
650     code = pr_SIdToName(id, name);
651     if (code)
652         return code;
653     if (id == ANONYMOUSID)
654         return PRNOENT;
655     code = ubik_Call(PR_ListEntry, pruclient, 0, id, &aentry);
656     if (code)
657         return code;
658     /* this should be done in one RPC, but I'm lazy. */
659     code = pr_SIdToName(aentry.owner, owner);
660     if (code)
661         return code;
662     code = pr_SIdToName(aentry.creator, creator);
663     if (code)
664         return code;
665     return PRSUCCESS;
666 }
667
668 pr_ChangeEntry(oldname, newname, newid, newowner)
669      char *oldname;
670      char *newname;
671      afs_int32 *newid;
672      char *newowner;
673 {
674     register afs_int32 code;
675     afs_int32 id;
676     afs_int32 oid = 0;
677
678     code = pr_SNameToId(oldname, &id);
679     if (code)
680         return code;
681     if (id == ANONYMOUSID)
682         return PRNOENT;
683     if (newowner && *newowner) {
684         code = pr_SNameToId(newowner, &oid);
685         if (code)
686             return code;
687         if (oid == ANONYMOUSID)
688             return PRNOENT;
689     }
690     code = ubik_Call(PR_ChangeEntry, pruclient, 0, id, newname, oid, newid);
691     return code;
692 }
693
694 pr_IsAMemberOf(uname, gname, flag)
695      char *uname;
696      char *gname;
697      afs_int32 *flag;
698 {
699     register afs_int32 code;
700     namelist lnames;
701     idlist lids;
702
703     stolower(uname);
704     stolower(gname);
705     lnames.namelist_len = 2;
706     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
707     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
708     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
709     lids.idlist_val = 0;
710     lids.idlist_len = 0;
711     code = pr_NameToId(&lnames, &lids);
712     if (code) {
713         if (lnames.namelist_val)
714             free(lnames.namelist_val);
715         if (lids.idlist_val)
716             free(lids.idlist_val);
717         return code;
718     }
719     code =
720         ubik_Call(PR_IsAMemberOf, pruclient, 0, lids.idlist_val[0],
721                   lids.idlist_val[1], flag);
722     if (lnames.namelist_val)
723         free(lnames.namelist_val);
724     if (lids.idlist_val)
725         free(lids.idlist_val);
726     return code;
727 }
728
729
730 pr_ListMaxUserId(mid)
731      afs_int32 *mid;
732 {
733     register afs_int32 code;
734     afs_int32 gid;
735     code = ubik_Call(PR_ListMax, pruclient, 0, mid, &gid);
736     return code;
737 }
738
739 pr_SetMaxUserId(mid)
740      afs_int32 mid;
741 {
742     register afs_int32 code;
743     afs_int32 flag = 0;
744     code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
745     return code;
746 }
747
748 pr_ListMaxGroupId(mid)
749      afs_int32 *mid;
750 {
751     register afs_int32 code;
752     afs_int32 id;
753     code = ubik_Call(PR_ListMax, pruclient, 0, &id, mid);
754     return code;
755 }
756
757 pr_SetMaxGroupId(mid)
758      afs_int32 mid;
759 {
760     register afs_int32 code;
761     afs_int32 flag = 0;
762
763     flag |= PRGRP;
764     code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
765     return code;
766 }
767
768 afs_int32
769 pr_SetFieldsEntry(id, mask, flags, ngroups, nusers)
770      afs_int32 id;
771      afs_int32 mask;
772      afs_int32 flags, ngroups, nusers;
773 {
774     register afs_int32 code;
775
776     code =
777         ubik_Call(PR_SetFieldsEntry, pruclient, 0, id, mask, flags, ngroups,
778                   nusers, 0, 0);
779     return code;
780 }
781
782
783 int
784 stolower(s)
785      char *s;
786 {
787     while (*s) {
788         if (isupper(*s))
789             *s = tolower(*s);
790         s++;
791     }
792     return 0;
793 }