1178825Sdfr/*
2233294Sstas * Copyright (c) 2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
9178825Sdfr *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of KTH nor the names of its contributors may be
18178825Sdfr *    used to endorse or promote products derived from this software without
19178825Sdfr *    specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22178825Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24178825Sdfr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25178825Sdfr * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26178825Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27178825Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28178825Sdfr * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29178825Sdfr * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30178825Sdfr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31178825Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32178825Sdfr
33178825Sdfr#include "krb5_locl.h"
34178825Sdfr#include <getarg.h>
35178825Sdfr#include <err.h>
36178825Sdfr
37178825Sdfrstatic int version_flag = 0;
38178825Sdfrstatic int help_flag	= 0;
39178825Sdfr
40178825Sdfrstatic struct getargs args[] = {
41178825Sdfr    {"version",	0,	arg_flag,	&version_flag,
42178825Sdfr     "print version", NULL },
43178825Sdfr    {"help",	0,	arg_flag,	&help_flag,
44178825Sdfr     NULL, NULL }
45178825Sdfr};
46178825Sdfr
47178825Sdfrstatic void
48178825Sdfrusage (int ret)
49178825Sdfr{
50178825Sdfr    arg_printusage (args,
51178825Sdfr		    sizeof(args)/sizeof(*args),
52178825Sdfr		    NULL,
53178825Sdfr		    "principal luser");
54178825Sdfr    exit (ret);
55178825Sdfr}
56178825Sdfr
57178825Sdfrint
58178825Sdfrmain(int argc, char **argv)
59178825Sdfr{
60178825Sdfr    krb5_context context;
61178825Sdfr    krb5_error_code ret;
62178825Sdfr    krb5_principal principal;
63178825Sdfr    char *p;
64178825Sdfr    int o = 0;
65178825Sdfr
66178825Sdfr    setprogname(argv[0]);
67178825Sdfr
68178825Sdfr    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &o))
69178825Sdfr	usage(1);
70233294Sstas
71178825Sdfr    if (help_flag)
72178825Sdfr	usage (0);
73178825Sdfr
74178825Sdfr    if(version_flag){
75178825Sdfr	print_version(NULL);
76178825Sdfr	exit(0);
77178825Sdfr    }
78178825Sdfr
79178825Sdfr    argc -= o;
80178825Sdfr    argv += o;
81178825Sdfr
82178825Sdfr    ret = krb5_init_context(&context);
83178825Sdfr    if (ret)
84178825Sdfr	errx (1, "krb5_init_context failed: %d", ret);
85178825Sdfr
86178825Sdfr    if (argc != 2)
87178825Sdfr	usage(1);
88178825Sdfr
89178825Sdfr    ret = krb5_parse_name(context, argv[0], &principal);
90178825Sdfr    if (ret)
91178825Sdfr	krb5_err(context, 1, ret, "krb5_parse_name");
92233294Sstas
93178825Sdfr    ret = krb5_unparse_name(context, principal, &p);
94178825Sdfr    if (ret)
95178825Sdfr	krb5_err(context, 1, ret, "krb5_unparse_name");
96178825Sdfr
97178825Sdfr    ret = krb5_kuserok(context, principal, argv[1]);
98178825Sdfr
99178825Sdfr    krb5_free_context(context);
100178825Sdfr
101178825Sdfr    printf("%s is %sallowed to login as %s\n", p, ret ? "" : "NOT ", argv[1]);
102178825Sdfr
103178825Sdfr    return 0;
104178825Sdfr}
105