542ef6d4aee5c592c5d679b435d0f74fb8c647d0
[platform/upstream/krb5.git] / src / lib / krb5 / krb / mk_req.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
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 krb5_error_code KRB5_CALLCONV
50 krb5_mk_req(krb5_context context, krb5_auth_context *auth_context,
51             krb5_flags ap_req_options, char *service, char *hostname,
52             krb5_data *in_data, krb5_ccache ccache, krb5_data *outbuf)
53 {
54     krb5_error_code       retval;
55     krb5_principal        server;
56     krb5_creds          * credsp;
57     krb5_creds            creds;
58
59     retval = krb5_sname_to_principal(context, hostname, service,
60                                      KRB5_NT_SRV_HST, &server);
61     if (retval)
62         return retval;
63
64     /* obtain ticket & session key */
65     memset(&creds, 0, sizeof(creds));
66     if ((retval = krb5_copy_principal(context, server, &creds.server)))
67         goto cleanup_princ;
68
69     if ((retval = krb5_cc_get_principal(context, ccache, &creds.client)))
70         goto cleanup_creds;
71
72     if ((retval = krb5_get_credentials(context, 0,
73                                        ccache, &creds, &credsp)))
74         goto cleanup_creds;
75
76     retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
77                                   in_data, credsp, outbuf);
78
79     krb5_free_creds(context, credsp);
80
81 cleanup_creds:
82     krb5_free_cred_contents(context, &creds);
83
84 cleanup_princ:
85     krb5_free_principal(context, server);
86
87     return retval;
88 }