Annotation of src/usr.bin/passwd/new_pwd.c, Revision 1.2
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>
52:
53: #ifdef NOENCRYPTION
54: #define read_long_pw_string placebo_read_pw_string
55: #else
56: #define read_long_pw_string des_read_pw_string
57: #endif
58:
59: static char *
60: check_pw (char *pword)
61: {
1.2 ! deraadt 62: char *t;
! 63:
! 64: if (strlen(pword) == 0)
! 65: return "Null passwords are not allowed - Please enter a longer password.";
1.1 art 66:
1.2 ! deraadt 67: if (strlen(pword) < MIN_KPW_LEN)
! 68: return "Password is to short - Please enter a longer password.";
1.1 art 69:
1.2 ! deraadt 70: if (strcmp(pword, "s/key") == 0)
! 71: return "That password collides with a system feature. Choose another.\n";
! 72:
! 73: /* Don't allow all lower case passwords regardless of length */
1.1 art 74: for (t = pword; *t && islower(*t); t++)
1.2 ! deraadt 75: ;
1.1 art 76: if (*t == 0)
1.2 ! deraadt 77: return "Please don't use an all-lower case password.\n"
! 78: "\tUnusual capitalization, delimiter characters or "
! 79: "digits are suggested.";
! 80: return NULL;
1.1 art 81: }
82:
83: int
84: get_pw_new_pwd(char *pword, int pwlen, krb_principal *pr, int print_realm)
85: {
1.2 ! deraadt 86: char ppromp[40+ANAME_SZ+INST_SZ+REALM_SZ]; /* for the password prompt */
! 87: char npromp[40+ANAME_SZ+INST_SZ+REALM_SZ]; /* for the password prompt */
! 88: char p[MAX_K_NAME_SZ];
! 89: char local_realm[REALM_SZ];
! 90: int status;
! 91: char *expl;
! 92: char *q;
1.1 art 93:
1.2 ! deraadt 94: /*
! 95: * We don't care about failure; this is to determine whether or
! 96: * not to print the realm in the prompt for a new password.
! 97: */
! 98: krb_get_lrealm(local_realm, 1);
1.1 art 99:
1.2 ! deraadt 100: if (strcmp(local_realm, pr->realm))
! 101: print_realm++;
1.1 art 102: krb_unparse_name_r(pr, p);
1.2 ! deraadt 103: if (print_realm == 0 && (q = strrchr(p, '@')))
! 104: *q = 0;
! 105:
! 106: snprintf(ppromp, sizeof(ppromp), "Old password for %s:", p);
! 107: if (read_long_pw_string(pword, pwlen-1, ppromp, 0)) {
! 108: fprintf(stderr, "Error reading old password.\n");
! 109: return -1;
1.1 art 110: }
1.2 ! deraadt 111:
! 112: status = krb_get_pw_in_tkt(pr->name, pr->instance, pr->realm,
! 113: PWSERV_NAME, KADM_SINST, 1, pword);
! 114: if (status != KSUCCESS) {
! 115: if (status == INTK_BADPW) {
! 116: printf("Incorrect old password.\n");
! 117: return -1;
! 118: } else {
! 119: fprintf(stderr, "Kerberos error: %s\n",
! 120: krb_get_err_text(status));
! 121: return -1;
! 122: }
1.1 art 123: }
1.2 ! deraadt 124: memset(pword, 0, pwlen);
1.1 art 125:
1.2 ! deraadt 126: do {
! 127: char verify[MAX_KPW_LEN];
! 128: snprintf(npromp, sizeof(npromp), "New Password for %s:",p);
! 129: if (read_long_pw_string(pword, pwlen-1, npromp, 0)) {
! 130: fprintf(stderr,
! 131: "Error reading new password, password unchanged.\n");
! 132: return -1;
! 133: }
! 134: expl = check_pw (pword);
! 135: if (expl) {
! 136: printf("\n\t%s\n\n", expl);
! 137: continue;
! 138: }
! 139:
! 140: /* Now we got an ok password, verify it. */
! 141: snprintf(npromp, sizeof(npromp),
! 142: "Verifying New Password for %s:", p);
! 143: if (read_long_pw_string(verify, MAX_KPW_LEN-1, npromp, 0)) {
! 144: fprintf(stderr,
! 145: "Error reading new password, password unchanged.\n");
! 146: return -1;
! 147: }
! 148: if (strcmp(pword, verify) != 0) {
! 149: printf("Verify failure - try again\n");
! 150: expl = ""; /* continue */
! 151: }
! 152: } while (expl);
! 153: return 0;
1.1 art 154: }