xref: /freebsd/crypto/krb5/src/tests/localauth.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/localauth.c - test harness for kuserok and aname_to_lname */
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 <krb5.h>
34 #include <stdio.h>
35 
36 static krb5_context ctx;
37 
38 static void
39 check(krb5_error_code code)
40 {
41     const char *errmsg;
42 
43     if (code) {
44         errmsg = krb5_get_error_message(ctx, code);
45         fprintf(stderr, "%s\n", errmsg);
46         krb5_free_error_message(ctx, errmsg);
47         exit(1);
48     }
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54     krb5_principal princ;
55     char buf[1024];
56 
57     if (argc < 2 || argc > 3) {
58         fprintf(stderr, "Usage: localauth principal [localuser]\n");
59         return 1;
60     }
61     check(krb5_init_context(&ctx));
62     check(krb5_parse_name(ctx, argv[1], &princ));
63     if (argc == 3) {
64         printf("%s\n", krb5_kuserok(ctx, princ, argv[2]) ? "yes" : "no");
65     } else {
66         check(krb5_aname_to_localname(ctx, princ, sizeof(buf), buf));
67         printf("%s\n", buf);
68     }
69     krb5_free_principal(ctx, princ);
70     krb5_free_context(ctx);
71     return 0;
72 }
73