[BACK]Return to new_pwd.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / passwd

Annotation of src/usr.bin/passwd/new_pwd.c, Revision 1.5

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