Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / kadmin / new_pwd.c
1 /*
2  * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "kadm_locl.h"
35
36 RCSID("$Id: new_pwd.c,v 1.14 1999/12/02 16:58:36 joda Exp $");
37
38 #ifdef NOENCRYPTION
39 #define read_long_pw_string placebo_read_pw_string
40 #else
41 #define read_long_pw_string des_read_pw_string
42 #endif
43
44 static char *
45 check_pw (char *pword)
46 {
47     int ret = kadm_check_pw(pword);
48     switch(ret) {
49     case 0:
50         return NULL;
51     case KADM_PASS_Q_NULL:
52         return "Null passwords are not allowed - "
53             "Please enter a longer password.";
54     case KADM_PASS_Q_TOOSHORT:
55         return "Password is to short - Please enter a longer password.";
56     case KADM_PASS_Q_CLASS:
57         /* XXX */
58         return "Please don't use an all-lower case password.\n"
59             "\tUnusual capitalization, delimiter characters or "
60             "digits are suggested.";
61     }
62     return "Password is insecure"; /* XXX this shouldn't happen */
63 }
64
65 int
66 get_pw_new_pwd(char *pword, int pwlen, krb_principal *pr, int print_realm)
67 {
68     char ppromp[40+ANAME_SZ+INST_SZ+REALM_SZ]; /* for the password prompt */
69     char npromp[40+ANAME_SZ+INST_SZ+REALM_SZ]; /* for the password prompt */
70     
71     char p[MAX_K_NAME_SZ];
72     
73     char local_realm[REALM_SZ];
74     int status;
75     char *expl;
76     
77     /*
78      * We don't care about failure; this is to determine whether or
79      * not to print the realm in the prompt for a new password. 
80      */
81     krb_get_lrealm(local_realm, 1);
82     
83     if (strcmp(local_realm, pr->realm))
84         print_realm++;
85     
86     {
87         char *q;
88         krb_unparse_name_r(pr, p);
89         if(print_realm == 0 && (q = strrchr(p, '@')))
90             *q = 0;
91     }
92
93     snprintf(ppromp, sizeof(ppromp), "Old password for %s:", p);
94     if (read_long_pw_string(pword, pwlen-1, ppromp, 0)) {
95         fprintf(stderr, "Error reading old password.\n");
96         return -1;
97     }
98
99     status = krb_get_pw_in_tkt(pr->name, pr->instance, pr->realm, 
100                                PWSERV_NAME, KADM_SINST, 1, pword);
101     if (status != KSUCCESS) {
102         if (status == INTK_BADPW) {
103             printf("Incorrect old password.\n");
104             return -1;
105         }
106         else {
107             fprintf(stderr, "Kerberos error: %s\n", krb_get_err_text(status));
108             return -1;
109         }
110     }
111     memset(pword, 0, pwlen);
112     
113     do {
114         char verify[MAX_KPW_LEN];
115
116         snprintf(npromp, sizeof(npromp), "New Password for %s:",p);
117         if (read_long_pw_string(pword, pwlen-1, npromp, 0)) {
118             fprintf(stderr,
119                     "Error reading new password, password unchanged.\n");
120             return -1;
121         }
122         expl = check_pw (pword);
123         if (expl) {
124             printf("\n\t%s\n\n", expl);
125             continue;
126         }
127         /* Now we got an ok password, verify it. */
128         snprintf(npromp, sizeof(npromp), "Verifying New Password for %s:", p);
129         if (read_long_pw_string(verify, MAX_KPW_LEN-1, npromp, 0)) {
130             fprintf(stderr,
131                     "Error reading new password, password unchanged.\n");
132             return -1;
133         }
134         if (strcmp(pword, verify) != 0) {
135             printf("Verify failure - try again\n");
136             expl = "";          /* continue */
137         }
138     } while (expl);
139     return 0;
140 }