xref: /freebsd/crypto/krb5/src/tests/rdreq.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/rdreq.c - Test harness for krb5_rd_req */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2014 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include <stdio.h>
34*7f2fe78bSCy Schubert #include <stdlib.h>
35*7f2fe78bSCy Schubert #include <string.h>
36*7f2fe78bSCy Schubert #include <assert.h>
37*7f2fe78bSCy Schubert #include <krb5.h>
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert int
main(int argc,char ** argv)40*7f2fe78bSCy Schubert main(int argc, char **argv)
41*7f2fe78bSCy Schubert {
42*7f2fe78bSCy Schubert     krb5_context context;
43*7f2fe78bSCy Schubert     krb5_principal client_princ, tkt_princ, server_princ;
44*7f2fe78bSCy Schubert     krb5_ccache ccache;
45*7f2fe78bSCy Schubert     krb5_creds *cred, mcred;
46*7f2fe78bSCy Schubert     krb5_auth_context auth_con;
47*7f2fe78bSCy Schubert     krb5_data apreq;
48*7f2fe78bSCy Schubert     krb5_error_code ret, code;
49*7f2fe78bSCy Schubert     const char *tkt_name, *server_name, *emsg;
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert     /* Parse arguments. */
52*7f2fe78bSCy Schubert     if (argc < 2 || argc > 3) {
53*7f2fe78bSCy Schubert         fprintf(stderr, "Usage: rdreq tktname [servername]\n");
54*7f2fe78bSCy Schubert         exit(1);
55*7f2fe78bSCy Schubert     }
56*7f2fe78bSCy Schubert     tkt_name = argv[1];
57*7f2fe78bSCy Schubert     server_name = argv[2];
58*7f2fe78bSCy Schubert 
59*7f2fe78bSCy Schubert     if (krb5_init_context(&context) != 0)
60*7f2fe78bSCy Schubert         abort();
61*7f2fe78bSCy Schubert 
62*7f2fe78bSCy Schubert     /* Parse the requested principal names. */
63*7f2fe78bSCy Schubert     if (krb5_parse_name(context, tkt_name, &tkt_princ) != 0)
64*7f2fe78bSCy Schubert         abort();
65*7f2fe78bSCy Schubert     if (server_name != NULL) {
66*7f2fe78bSCy Schubert         if (krb5_parse_name(context, server_name, &server_princ) != 0)
67*7f2fe78bSCy Schubert             abort();
68*7f2fe78bSCy Schubert         server_princ->type = KRB5_NT_SRV_HST;
69*7f2fe78bSCy Schubert     } else {
70*7f2fe78bSCy Schubert         server_princ = NULL;
71*7f2fe78bSCy Schubert     }
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert     /* Produce an AP-REQ message. */
74*7f2fe78bSCy Schubert     if (krb5_cc_default(context, &ccache) != 0)
75*7f2fe78bSCy Schubert         abort();
76*7f2fe78bSCy Schubert     if (krb5_cc_get_principal(context, ccache, &client_princ) != 0)
77*7f2fe78bSCy Schubert         abort();
78*7f2fe78bSCy Schubert     memset(&mcred, 0, sizeof(mcred));
79*7f2fe78bSCy Schubert     mcred.client = client_princ;
80*7f2fe78bSCy Schubert     mcred.server = tkt_princ;
81*7f2fe78bSCy Schubert     if (krb5_get_credentials(context, 0, ccache, &mcred, &cred) != 0)
82*7f2fe78bSCy Schubert         abort();
83*7f2fe78bSCy Schubert     auth_con = NULL;
84*7f2fe78bSCy Schubert     if (krb5_mk_req_extended(context, &auth_con, 0, NULL, cred, &apreq) != 0)
85*7f2fe78bSCy Schubert         abort();
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert     /* Consume the AP-REQ message without using a replay cache. */
88*7f2fe78bSCy Schubert     krb5_auth_con_free(context, auth_con);
89*7f2fe78bSCy Schubert     if (krb5_auth_con_init(context, &auth_con) != 0)
90*7f2fe78bSCy Schubert         abort();
91*7f2fe78bSCy Schubert     if (krb5_auth_con_setflags(context, auth_con, 0) != 0)
92*7f2fe78bSCy Schubert         abort();
93*7f2fe78bSCy Schubert     ret = krb5_rd_req(context, &auth_con, &apreq, server_princ, NULL, NULL,
94*7f2fe78bSCy Schubert                       NULL);
95*7f2fe78bSCy Schubert 
96*7f2fe78bSCy Schubert     /* Display the result. */
97*7f2fe78bSCy Schubert     if (ret) {
98*7f2fe78bSCy Schubert         code = ret - ERROR_TABLE_BASE_krb5;
99*7f2fe78bSCy Schubert         if (code < 0 || code > 127)
100*7f2fe78bSCy Schubert             code = 60;          /* KRB_ERR_GENERIC */
101*7f2fe78bSCy Schubert         emsg = krb5_get_error_message(context, ret);
102*7f2fe78bSCy Schubert         printf("%d %s\n", code, emsg);
103*7f2fe78bSCy Schubert         krb5_free_error_message(context, emsg);
104*7f2fe78bSCy Schubert     } else {
105*7f2fe78bSCy Schubert         printf("0 success\n");
106*7f2fe78bSCy Schubert     }
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert     krb5_free_data_contents(context, &apreq);
109*7f2fe78bSCy Schubert     assert(apreq.length == 0);
110*7f2fe78bSCy Schubert     krb5_auth_con_free(context, auth_con);
111*7f2fe78bSCy Schubert     krb5_free_creds(context, cred);
112*7f2fe78bSCy Schubert     krb5_cc_close(context, ccache);
113*7f2fe78bSCy Schubert     krb5_free_principal(context, client_princ);
114*7f2fe78bSCy Schubert     krb5_free_principal(context, tkt_princ);
115*7f2fe78bSCy Schubert     krb5_free_principal(context, server_princ);
116*7f2fe78bSCy Schubert     krb5_free_context(context);
117*7f2fe78bSCy Schubert     return 0;
118*7f2fe78bSCy Schubert }
119