xref: /freebsd/crypto/krb5/src/lib/krb5/krb/t_expire_warn.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/t_expire_warn.c - Test harness for password expiry warnings */
3 /*
4  * Copyright (C) 2010 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include "k5-int.h"
28 
29 static int exp_dummy, prompt_dummy;
30 
31 static void
check(krb5_error_code code)32 check(krb5_error_code code)
33 {
34     if (code != 0)
35         abort();
36 }
37 
38 static krb5_error_code
prompter_cb(krb5_context ctx,void * data,const char * name,const char * banner,int num_prompts,krb5_prompt prompts[])39 prompter_cb(krb5_context ctx, void *data, const char *name,
40             const char *banner, int num_prompts, krb5_prompt prompts[])
41 {
42     /* Not expecting any actual prompts, only banners. */
43     assert(num_prompts == 0);
44     assert(banner != NULL);
45     printf("Prompter: %s\n", banner);
46     return 0;
47 }
48 
49 static void
expire_cb(krb5_context ctx,void * data,krb5_timestamp password_expiration,krb5_timestamp account_expiration,krb5_boolean is_last_req)50 expire_cb(krb5_context ctx, void *data, krb5_timestamp password_expiration,
51           krb5_timestamp account_expiration, krb5_boolean is_last_req)
52 {
53     printf("password_expiration = %ld\n", (long)password_expiration);
54     printf("account_expiration = %ld\n", (long)account_expiration);
55     printf("is_last_req = %d\n", (int)is_last_req);
56 }
57 
58 int
main(int argc,char ** argv)59 main(int argc, char **argv)
60 {
61     krb5_context ctx;
62     krb5_init_creds_context icctx;
63     krb5_get_init_creds_opt *opt;
64     char *user, *password, *service = NULL;
65     krb5_boolean use_cb, stepwise;
66     krb5_principal client;
67     krb5_creds creds;
68 
69     if (argc < 5) {
70         fprintf(stderr, "Usage: %s username password {1|0} {1|0} [service]\n",
71                 argv[0]);
72         return 1;
73     }
74     user = argv[1];
75     password = argv[2];
76     use_cb = atoi(argv[3]);
77     stepwise = atoi(argv[4]);
78     if (argc >= 6)
79         service = argv[5];
80 
81     check(krb5_init_context(&ctx));
82     check(krb5_get_init_creds_opt_alloc(ctx, &opt));
83     if (use_cb) {
84         check(krb5_get_init_creds_opt_set_expire_callback(ctx, opt, expire_cb,
85                                                           &exp_dummy));
86     }
87     check(krb5_parse_name(ctx, user, &client));
88     if (stepwise) {
89         check(krb5_init_creds_init(ctx, client, prompter_cb, &prompt_dummy, 0,
90                                    opt, &icctx));
91         krb5_init_creds_set_password(ctx, icctx, password);
92         if (service != NULL)
93             check(krb5_init_creds_set_service(ctx, icctx, service));
94         check(krb5_init_creds_get(ctx, icctx));
95         krb5_init_creds_free(ctx, icctx);
96     } else {
97         check(krb5_get_init_creds_password(ctx, &creds, client, password,
98                                            prompter_cb, &prompt_dummy, 0,
99                                            service, opt));
100         krb5_free_cred_contents(ctx, &creds);
101     }
102     krb5_get_init_creds_opt_free(ctx, opt);
103     krb5_free_principal(ctx, client);
104     krb5_free_context(ctx);
105     return 0;
106 }
107