3d86741fc188e2185c3ea83dbf0165a3d69f22f1
[krb5.git] / src / lib / krb5 / krb / mk_req.c
1 /*
2  * lib/krb5/krb/mk_req.c
3  *
4  * Copyright 1990,1991 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_mk_req() routine.
25  */
26
27 #include "k5-int.h"
28 #include "auth_con.h"
29
30 /*
31  Formats a KRB_AP_REQ message into outbuf.
32
33  server specifies the principal of the server to receive the message; if
34  credentials are not present in the credentials cache for this server, the
35  TGS request with default parameters is used in an attempt to obtain
36  such credentials, and they are stored in ccache.
37
38  kdc_options specifies the options requested for the 
39  ap_req_options specifies the KRB_AP_REQ options desired.
40
41  checksum specifies the checksum to be used in the authenticator.
42
43  The outbuf buffer storage is allocated, and should be freed by the
44  caller when finished.
45
46  returns system errors
47 */
48
49 extern krb5_flags krb5_kdc_default_options;
50
51 krb5_error_code INTERFACE
52 krb5_mk_req(context, auth_context, ap_req_options, service, hostname, in_data,
53               ccache, outbuf)
54     krb5_context          context;
55     krb5_auth_context  ** auth_context;
56     const krb5_flags      ap_req_options;
57     char                * service;
58     char                * hostname;
59     krb5_data           * in_data;
60     krb5_ccache           ccache;
61     krb5_data           * outbuf;
62 {
63     krb5_error_code       retval;
64     krb5_principal        server;
65     krb5_creds          * credsp;
66     krb5_creds            creds;
67     char               ** realm;
68
69     /* get realm */
70     if (retval = krb5_get_host_realm(context, hostname, &realm)) 
71         return retval;
72
73     /* build principal */
74     if (retval = krb5_build_principal(context, &server, strlen(realm[0]),
75                                       realm[0], service, hostname, NULL))
76         goto cleanup_realm;
77                                       
78     /* obtain ticket & session key */
79     memset((char *)&creds, 0, sizeof(creds));
80     if (retval = krb5_copy_principal(context, server, &creds.server))
81         goto cleanup_princ;
82
83     if (retval = krb5_cc_get_principal(context, ccache, &creds.client))
84         goto cleanup_creds;
85
86     if (retval = krb5_get_credentials(context, krb5_kdc_default_options,
87                                       ccache, &creds, &credsp))
88         goto cleanup_creds;
89
90     retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
91                                   in_data, credsp, outbuf);
92
93     krb5_free_creds(context, credsp);
94
95 cleanup_creds:
96     krb5_free_cred_contents(context, &creds);
97
98 cleanup_princ:
99     krb5_free_principal(context, server);
100
101 cleanup_realm:
102     krb5_free_host_realm(context, realm);
103     return retval;
104 }