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