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