xref: /freebsd/crypto/krb5/src/tests/s2p.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/s2p.c - krb5_name_to_principal test harness */
3 /*
4  * Copyright (C) 2013 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <assert.h>
36 #include <krb5.h>
37 
38 static krb5_context ctx;
39 
40 static void
check(krb5_error_code code)41 check(krb5_error_code code)
42 {
43     const char *errmsg;
44 
45     if (code) {
46         errmsg = krb5_get_error_message(ctx, code);
47         fprintf(stderr, "%s\n", errmsg);
48         krb5_free_error_message(ctx, errmsg);
49         exit(1);
50     }
51 }
52 
53 int
main(int argc,char ** argv)54 main(int argc, char **argv)
55 {
56     krb5_principal princ;
57     krb5_int32 type;
58     const char *service, *hostname;
59     char *name;
60 
61     /* Parse arguments. */
62     assert(argc == 4);
63     hostname = argv[1];
64     service = argv[2];
65     if (strcmp(argv[3], "unknown") == 0)
66         type = KRB5_NT_UNKNOWN;
67     else if (strcmp(argv[3], "srv-hst") == 0)
68         type = KRB5_NT_SRV_HST;
69     else
70         abort();
71 
72     check(krb5_init_context(&ctx));
73     check(krb5_sname_to_principal(ctx, hostname, service, type, &princ));
74     check(krb5_unparse_name(ctx, princ, &name));
75     printf("%s\n", name);
76 
77     krb5_free_unparsed_name(ctx, name);
78     krb5_free_principal(ctx, princ);
79     krb5_free_context(ctx);
80     return 0;
81 }
82