xref: /freebsd/crypto/krb5/src/tests/misc/test_chpw_message.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/misc/test_getpw.c */
3 /*
4  * Copyright (C) 2012 by the Red Hat Inc.
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 "autoconf.h"
28 #include "krb5.h"
29 
30 #include <sys/types.h>
31 #include <assert.h>
32 #include <locale.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 static krb5_data result_utf8 = {
38     0, 23, "This is a valid string.",
39 };
40 
41 static krb5_data result_invalid_utf8 = {
42     0, 19, "\0This is not valid.",
43 };
44 
45 static krb5_data result_ad_complex = {
46     0, 30,
47     "\0\0"             /* zero bytes */
48     "\0\0\0\0"         /* min length */
49     "\0\0\0\0"         /* history */
50     "\0\0\0\1"         /* properties, complex */
51     "\0\0\0\0\0\0\0\0" /* expire */
52     "\0\0\0\0\0\0\0\0" /* min age */
53 };
54 
55 static krb5_data result_ad_length = {
56     0, 30,
57     "\0\0"             /* zero bytes */
58     "\0\0\0\x0d"       /* min length, 13 characters */
59     "\0\0\0\0"         /* history */
60     "\0\0\0\0"         /* properties */
61     "\0\0\0\0\0\0\0\0" /* expire */
62     "\0\0\0\0\0\0\0\0" /* min age */
63 };
64 
65 static krb5_data result_ad_history = {
66     0, 30,
67     "\0\0"             /* zero bytes */
68     "\0\0\0\0"         /* min length */
69     "\0\0\0\x09"       /* history, 9 passwords */
70     "\0\0\0\0"         /* properties */
71     "\0\0\0\0\0\0\0\0" /* expire */
72     "\0\0\0\0\0\0\0\0" /* min age */
73 };
74 
75 static krb5_data result_ad_age = {
76     0, 30,
77     "\0\0"                        /* zero bytes */
78     "\0\0\0\0"                    /* min length */
79     "\0\0\0\0"                    /* history, 9 passwords */
80     "\0\0\0\0"                    /* properties */
81     "\0\0\0\0\0\0\0\0"            /* expire */
82     "\0\0\x01\x92\x54\xd3\x80\0"  /* min age, 2 days */
83 };
84 
85 static krb5_data result_ad_all = {
86     0, 30,
87     "\0\0"                      /* zero bytes */
88     "\0\0\0\x05"                /* min length, 5 characters */
89     "\0\0\0\x0D"                /* history, 13 passwords */
90     "\0\0\0\x01"                /* properties, complex */
91     "\0\0\0\0\0\0\0\0"          /* expire */
92     "\0\0\0\xc9\x2a\x69\xc0\0"  /* min age, 1 day */
93 };
94 
95 static void
96 check(krb5_error_code code)
97 {
98     if (code != 0) {
99         com_err("t_vfy_increds", code, "");
100         abort();
101     }
102 }
103 
104 static void
105 check_msg(const char *real, const char *expected)
106 {
107     if (strstr(real, expected) == NULL) {
108         fprintf(stderr, "Expected to see: %s\n", expected);
109         abort();
110     }
111 }
112 
113 int
114 main(void)
115 {
116     krb5_context context;
117     char *msg;
118 
119     setlocale(LC_ALL, "C");
120 
121     check(krb5_init_context(&context));
122 
123     /* Valid utf-8 data in the result should be returned as is */
124     check(krb5_chpw_message(context, &result_utf8, &msg));
125     printf("  UTF8 valid:   %s\n", msg);
126     check_msg(msg, "This is a valid string.");
127     free(msg);
128 
129     /* Invalid data should have a generic message. */
130     check(krb5_chpw_message(context, &result_invalid_utf8, &msg));
131     printf("  UTF8 invalid: %s\n", msg);
132     check_msg(msg, "contact your administrator");
133     free(msg);
134 
135     /* AD data with complex data requirement */
136     check(krb5_chpw_message(context, &result_ad_complex, &msg));
137     printf("  AD complex:   %s\n", msg);
138     check_msg(msg, "The password must include numbers or symbols.");
139     check_msg(msg, "Don't include any part of your name in the password.");
140     free(msg);
141 
142     /* AD data with min password length */
143     check(krb5_chpw_message(context, &result_ad_length, &msg));
144     printf("  AD length:    %s\n", msg);
145     check_msg(msg, "The password must contain at least 13 characters.");
146     free(msg);
147 
148     /* AD data with history requirements */
149     check(krb5_chpw_message(context, &result_ad_history, &msg));
150     printf("  AD history:   %s\n", msg);
151     check_msg(msg, "The password must be different from the previous 9 "
152               "passwords.");
153     free(msg);
154 
155     /* AD data with minimum age */
156     check(krb5_chpw_message(context, &result_ad_age, &msg));
157     printf("  AD min age:   %s\n", msg);
158     check_msg(msg, "The password can only be changed every 2 days.");
159     free(msg);
160 
161     /* AD data with all */
162     check(krb5_chpw_message(context, &result_ad_all, &msg));
163     printf("  AD all:       %s\n", msg);
164     check_msg(msg, "The password can only be changed once a day.");
165     check_msg(msg, "The password must be different from the previous 13 "
166               "passwords.");
167     check_msg(msg, "The password must contain at least 5 characters.");
168     check_msg(msg, "The password must include numbers or symbols.");
169     check_msg(msg, "Don't include any part of your name in the password.");
170     free(msg);
171 
172     krb5_free_context(context);
173     exit(0);
174 }
175