Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / get_cred.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
4    Export of this software from the United States of America is assumed
5    to require a specific license from the United States Government.
6    It is the responsibility of any person or organization contemplating
7    export to obtain such a license before exporting.
8
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission.  M.I.T. makes no representations about the suitability of
17 this software for any purpose.  It is provided "as is" without express
18 or implied warranty.
19
20   */
21
22 #include "krb_locl.h"
23
24 RCSID("$Id: get_cred.c,v 1.7 1997/12/15 17:12:55 assar Exp $");
25
26 /*
27  * krb_get_cred takes a service name, instance, and realm, and a
28  * structure of type CREDENTIALS to be filled in with ticket
29  * information.  It then searches the ticket file for the appropriate
30  * ticket and fills in the structure with the corresponding
31  * information from the file.  If successful, it returns KSUCCESS.
32  * On failure it returns a Kerberos error code.
33  */
34
35 int
36 krb_get_cred(char *service,     /* Service name */
37              char *instance,    /* Instance */
38              char *realm,       /* Auth domain */
39              CREDENTIALS *c)    /* Credentials struct */
40 {
41     int tf_status;              /* return value of tf function calls */
42     CREDENTIALS cr;
43
44     if (c == NULL)
45         c = &cr;
46
47     /* Open ticket file and lock it for shared reading */
48     if ((tf_status = tf_init(TKT_FILE, R_TKT_FIL)) != KSUCCESS)
49         return(tf_status);
50
51     /* Copy principal's name and instance into the CREDENTIALS struc c */
52
53     if ( (tf_status = tf_get_pname(c->pname)) != KSUCCESS ||
54          (tf_status = tf_get_pinst(c->pinst)) != KSUCCESS )
55         return (tf_status);
56
57     /* Search for requested service credentials and copy into c */
58        
59     while ((tf_status = tf_get_cred(c)) == KSUCCESS) {
60         if ((strcmp(c->service,service) == 0) &&
61            (strcmp(c->instance,instance) == 0) &&
62            (strcmp(c->realm,realm) == 0))
63                    break;
64     }
65     tf_close();
66
67     if (tf_status == EOF)
68         return (GC_NOTKT);
69     return(tf_status);
70 }