pull up r25483 and r25484 from trunk
[krb5.git] / src / lib / krb5 / krb / t_expire_warn.c
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 krb5_error_code
32 prompter_cb(krb5_context ctx, void *data, const char *name,
33             const char *banner, int num_prompts, krb5_prompt prompts[])
34 {
35     /* Not expecting any actual prompts, only banners. */
36     assert(num_prompts == 0);
37     assert(banner != NULL);
38     printf("Prompter: %s\n", banner);
39     return 0;
40 }
41
42 static void
43 expire_cb(krb5_context ctx, void *data, krb5_timestamp password_expiration,
44           krb5_timestamp account_expiration, krb5_boolean is_last_req)
45 {
46     printf("password_expiration = %ld\n", (long)password_expiration);
47     printf("account_expiration = %ld\n", (long)account_expiration);
48     printf("is_last_req = %d\n", (int)is_last_req);
49 }
50
51 int
52 main(int argc, char **argv)
53 {
54     krb5_context ctx;
55     krb5_get_init_creds_opt *opt;
56     char *user, *password, *service = NULL;
57     krb5_boolean use_cb;
58     krb5_principal client;
59     krb5_creds creds;
60
61     if (argc < 4) {
62         fprintf(stderr, "Usage: %s username password {1|0} [service]\n",
63                 argv[0]);
64         return 1;
65     }
66     user = argv[1];
67     password = argv[2];
68     use_cb = atoi(argv[3]);
69     if (argc >= 5)
70         service = argv[4];
71
72     assert(krb5_init_context(&ctx) == 0);
73     assert(krb5_get_init_creds_opt_alloc(ctx, &opt) == 0);
74     if (use_cb) {
75         assert(krb5_get_init_creds_opt_set_expire_callback(ctx, opt, expire_cb,
76                                                            &exp_dummy) == 0);
77     }
78     assert(krb5_parse_name(ctx, user, &client) == 0);
79     assert(krb5_get_init_creds_password(ctx, &creds, client, password,
80                                         prompter_cb, &prompt_dummy, 0, service,
81                                         opt) == 0);
82     krb5_get_init_creds_opt_free(ctx, opt);
83     krb5_free_principal(ctx, client);
84     krb5_free_cred_contents(ctx, &creds);
85     krb5_free_context(ctx);
86     return 0;
87 }