* t_ser.c (stuff): New variable.
[idra/krb5.git] / src / lib / krb5 / krb / get_creds.c
1 /*
2  * lib/krb5/krb/get_creds.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * krb5_get_credentials()
25  */
26
27
28
29 /*
30  Attempts to use the credentials cache or TGS exchange to get an additional
31  ticket for the
32  client identified by in_creds->client, the server identified by
33  in_creds->server, with options options, expiration date specified in
34  in_creds->times.endtime (0 means as long as possible), session key type
35  specified in in_creds->keyblock.enctype (if non-zero)
36
37  Any returned ticket and intermediate ticket-granting tickets are
38  stored in ccache.
39
40  returns errors from encryption routines, system errors
41  */
42
43 #include "k5-int.h"
44
45 krb5_error_code INTERFACE
46 krb5_get_credentials(context, options, ccache, in_creds, out_creds)
47     krb5_context context;
48     const krb5_flags options;
49     krb5_ccache ccache;
50     krb5_creds *in_creds;
51     krb5_creds **out_creds;
52 {
53     krb5_error_code retval, rv2;
54     krb5_creds **tgts;
55     krb5_creds *ncreds;
56     krb5_creds mcreds;
57     krb5_flags fields;
58
59     if (!in_creds || !in_creds->server || !in_creds->client)
60         return EINVAL;
61
62     memset((char *)&mcreds, 0, sizeof(krb5_creds));
63     mcreds.magic = KV5M_CREDS;
64     mcreds.times.endtime = in_creds->times.endtime;
65 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT
66     mcreds.keyblock = in_creds->keyblock;
67 #else
68     memcpy(&mcreds.keyblock, &in_creds->keyblock, sizeof(krb5_keyblock));
69 #endif
70     mcreds.authdata = in_creds->authdata;
71     mcreds.server = in_creds->server;
72     mcreds.client = in_creds->client;
73     
74     fields = KRB5_TC_MATCH_TIMES /*XXX |KRB5_TC_MATCH_SKEY_TYPE */
75         | KRB5_TC_MATCH_AUTHDATA ;
76     if (mcreds.keyblock.enctype)
77         fields |= KRB5_TC_MATCH_KTYPE;
78     if (options & KRB5_GC_USER_USER) {
79         /* also match on identical 2nd tkt and tkt encrypted in a
80            session key */
81         fields |= KRB5_TC_MATCH_2ND_TKT|KRB5_TC_MATCH_IS_SKEY;
82         mcreds.is_skey = TRUE;
83         mcreds.second_ticket = in_creds->second_ticket;
84         if (!in_creds->second_ticket.length)
85             return KRB5_NO_2ND_TKT;
86     }
87
88     if ((ncreds = (krb5_creds *)malloc(sizeof(krb5_creds))) == NULL)
89         return ENOMEM;
90
91     memset((char *)ncreds, 0, sizeof(krb5_creds));
92     ncreds->magic = KV5M_CREDS;
93
94     /* The caller is now responsible for cleaning up in_creds */
95     if ((retval = krb5_cc_retrieve_cred(context, ccache, fields, &mcreds,
96                                         ncreds))) {
97         krb5_xfree(ncreds);
98         ncreds = in_creds;
99     } else {
100         *out_creds = ncreds;
101     }
102
103     if (retval != KRB5_CC_NOTFOUND || options & KRB5_GC_CACHED)
104         return retval;
105
106     retval = krb5_get_cred_from_kdc(context, ccache, ncreds, out_creds, &tgts);
107     if (tgts) {
108         register int i = 0;
109         while (tgts[i]) {
110             if ((rv2 = krb5_cc_store_cred(context, ccache, tgts[i]))) {
111                 retval = rv2;
112                 break;
113             }
114             i++;
115         }
116         krb5_free_tgt_creds(context, tgts);
117     }
118     if (!retval)
119         retval = krb5_cc_store_cred(context, ccache, *out_creds);
120     return retval;
121 }