155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2005, 2007 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
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.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "kuser_locl.h"
3555682Smarkm
3690926Snectarstatic int help_flag = 0;
3790926Snectarstatic int version_flag = 0;
3890926Snectar
3990926Snectarstatic struct getargs args[] = {
4090926Snectar    { "version", 	0,   arg_flag, &version_flag },
4190926Snectar    { "help",		0,   arg_flag, &help_flag }
4290926Snectar};
4390926Snectar
4490926Snectarstatic void
4590926Snectarusage (int ret)
4690926Snectar{
4790926Snectar    arg_printusage (args,
4890926Snectar		    sizeof(args)/sizeof(*args),
4990926Snectar		    NULL,
5090926Snectar		    "[principal]");
5190926Snectar    exit (ret);
5290926Snectar}
5390926Snectar
5455682Smarkmint
5555682Smarkmmain(int argc, char **argv)
5655682Smarkm{
5755682Smarkm    krb5_context context;
5855682Smarkm    krb5_error_code ret;
5955682Smarkm    krb5_creds cred;
6055682Smarkm    krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
61178825Sdfr    krb5_get_init_creds_opt *get_options;
6255682Smarkm    krb5_verify_init_creds_opt verify_options;
63178825Sdfr    krb5_principal principal = NULL;
64178825Sdfr    int optidx = 0;
6555682Smarkm
6690926Snectar    setprogname (argv[0]);
6790926Snectar
68178825Sdfr    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
6990926Snectar	usage(1);
70233294Sstas
7190926Snectar    if (help_flag)
7290926Snectar	usage (0);
7390926Snectar
7490926Snectar    if(version_flag) {
7590926Snectar	print_version(NULL);
7690926Snectar	exit(0);
7790926Snectar    }
78233294Sstas
79178825Sdfr    argc -= optidx;
80178825Sdfr    argv += optidx;
8190926Snectar
8272445Sassar    ret = krb5_init_context(&context);
8372445Sassar    if (ret)
8472445Sassar	errx (1, "krb5_init_context failed: %d", ret);
8555682Smarkm
86178825Sdfr    ret = krb5_get_init_creds_opt_alloc (context, &get_options);
87178825Sdfr    if (ret)
88178825Sdfr	krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
8955682Smarkm
90178825Sdfr    krb5_get_init_creds_opt_set_preauth_list (get_options,
9155682Smarkm					      pre_auth_types,
9255682Smarkm					      1);
9355682Smarkm
9455682Smarkm    krb5_verify_init_creds_opt_init (&verify_options);
95233294Sstas
96178825Sdfr    if (argc) {
97178825Sdfr	ret = krb5_parse_name(context, argv[0], &principal);
98178825Sdfr	if (ret)
99178825Sdfr	    krb5_err(context, 1, ret, "krb5_parse_name: %s", argv[0]);
100233294Sstas    } else {
101233294Sstas	ret = krb5_get_default_principal(context, &principal);
102233294Sstas	if (ret)
103233294Sstas	    krb5_err(context, 1, ret, "krb5_get_default_principal");
104233294Sstas
105178825Sdfr    }
106178825Sdfr
10755682Smarkm    ret = krb5_get_init_creds_password (context,
10855682Smarkm					&cred,
109178825Sdfr					principal,
11055682Smarkm					NULL,
11155682Smarkm					krb5_prompter_posix,
11255682Smarkm					NULL,
11355682Smarkm					0,
11455682Smarkm					NULL,
115178825Sdfr					get_options);
11655682Smarkm    if (ret)
117233294Sstas	krb5_err(context, 1, ret,  "krb5_get_init_creds");
11855682Smarkm
11955682Smarkm    ret = krb5_verify_init_creds (context,
12055682Smarkm				  &cred,
12155682Smarkm				  NULL,
12255682Smarkm				  NULL,
12355682Smarkm				  NULL,
12455682Smarkm				  &verify_options);
12555682Smarkm    if (ret)
126233294Sstas	krb5_err(context, 1, ret, "krb5_verify_init_creds");
127178825Sdfr    krb5_free_cred_contents (context, &cred);
12855682Smarkm    krb5_free_context (context);
12955682Smarkm    return 0;
13055682Smarkm}
131