1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/krb/rd_req.c
4  *
5  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  *
28  * krb5_rd_req()
29  */
30 
31 #include <k5-int.h>
32 #include <auth_con.h>
33 
34 /*
35  *  Parses a KRB_AP_REQ message, returning its contents.
36  *
37  *  server specifies the expected server's name for the ticket.
38  *
39  *  keyproc specifies a procedure to generate a decryption key for the
40  *  ticket.  If keyproc is non-NULL, keyprocarg is passed to it, and the result
41  *  used as a decryption key. If keyproc is NULL, then fetchfrom is checked;
42  *  if it is non-NULL, it specifies a parameter name from which to retrieve the
43  *  decryption key.  If fetchfrom is NULL, then the default key store is
44  *  consulted.
45  *
46  *  returns system errors, encryption errors, replay errors
47  */
48 krb5_error_code KRB5_CALLCONV
49 krb5_rd_req(krb5_context context, krb5_auth_context *auth_context, const krb5_data *inbuf, krb5_const_principal server, krb5_keytab keytab, krb5_flags *ap_req_options, krb5_ticket **ticket)
50 {
51     krb5_error_code 	  retval;
52     krb5_ap_req 	* request;
53     krb5_auth_context	  new_auth_context;
54     krb5_keytab           new_keytab = NULL;
55 
56     if (!krb5_is_ap_req(inbuf))
57 	return KRB5KRB_AP_ERR_MSG_TYPE;
58     if ((retval = decode_krb5_ap_req(inbuf, &request))) {
59     	switch (retval) {
60 	case KRB5_BADMSGTYPE:
61 	    return KRB5KRB_AP_ERR_BADVERSION;
62 	default:
63 	    return(retval);
64 	}
65     }
66 
67     /* Get an auth context if necessary. */
68     new_auth_context = NULL;
69     if (*auth_context == NULL) {
70 	if ((retval = krb5_auth_con_init(context, &new_auth_context)))
71 	    goto cleanup_request;
72         *auth_context = new_auth_context;
73     }
74 
75     if (!server) {
76 	server = request->ticket->server;
77     }
78     /* Get an rcache if necessary. */
79     if (((*auth_context)->rcache == NULL)
80 	&& ((*auth_context)->auth_context_flags & KRB5_AUTH_CONTEXT_DO_TIME)
81 	&& server) {
82 	if ((retval = krb5_get_server_rcache(context,
83      krb5_princ_component(context,server,0), &(*auth_context)->rcache)))
84 	    goto cleanup_auth_context;
85     }
86 
87     /* Get a keytab if necessary. */
88     if (keytab == NULL) {
89 	if ((retval = krb5_kt_default(context, &new_keytab)))
90 	    goto cleanup_auth_context;
91 	keytab = new_keytab;
92     }
93 
94     retval = krb5_rd_req_decoded(context, auth_context, request, server,
95 				 keytab, ap_req_options, ticket);
96 
97     if (new_keytab != NULL)
98         (void) krb5_kt_close(context, new_keytab);
99 
100 cleanup_auth_context:
101     if (new_auth_context && retval) {
102 	krb5_auth_con_free(context, new_auth_context);
103 	*auth_context = NULL;
104     }
105 
106 cleanup_request:
107     krb5_free_ap_req(context, request);
108     return retval;
109 }
110 
111