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