Annotation of src/usr.bin/passwd/passwd.c, Revision 1.18
1.18 ! hin 1: /* $OpenBSD: passwd.c,v 1.17 2003/07/10 00:06:51 david Exp $ */
1.4 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1988 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.16 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #ifndef lint
33: char copyright[] =
34: "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
35: All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
1.11 millert 39: /*static const char sccsid[] = "from: @(#)passwd.c 5.5 (Berkeley) 7/6/91";*/
1.18 ! hin 40: static const char rcsid[] = "$OpenBSD: passwd.c,v 1.17 2003/07/10 00:06:51 david Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: #include <stdio.h>
1.17 david 44: #include <stdlib.h>
1.1 deraadt 45: #include <string.h>
46: #include <unistd.h>
1.12 millert 47: #include <err.h>
1.1 deraadt 48:
49: /*
50: * Note on configuration:
51: * Generally one would not use both Kerberos and YP
52: * to maintain passwords.
53: *
54: */
55:
56: int use_kerberos;
57: int use_yp;
58:
59: #ifdef YP
60: int force_yp;
61: #endif
62:
1.9 millert 63: extern int local_passwd(char *, int);
1.6 weingart 64: extern int yp_passwd(char *);
1.10 hin 65: extern int krb5_passwd(int, char **);
1.14 hugh 66: extern int _yp_check(char **);
1.15 deraadt 67: void usage(int retval);
1.6 weingart 68:
69: int
1.15 deraadt 70: main(int argc, char **argv)
1.1 deraadt 71: {
72: extern int optind;
1.15 deraadt 73: char *username;
1.13 mpech 74: int ch;
1.14 hugh 75: #ifdef YP
1.1 deraadt 76: int status = 0;
1.14 hugh 77: #endif
1.15 deraadt 78:
1.18 ! hin 79: #if defined(KERBEROS5)
1.3 tholo 80: extern char realm[];
1.1 deraadt 81:
1.3 tholo 82: if (krb_get_lrealm(realm,1) == KSUCCESS)
83: use_kerberos = 1;
1.1 deraadt 84: #endif
85: #ifdef YP
86: use_yp = _yp_check(NULL);
87: #endif
88:
1.6 weingart 89: /* Process args and options */
1.18 ! hin 90: while ((ch = getopt(argc, argv, "lyK")) != -1)
1.1 deraadt 91: switch (ch) {
92: case 'l': /* change local password file */
93: use_kerberos = 0;
94: use_yp = 0;
95: break;
1.10 hin 96: case 'K':
97: #ifdef KRB5
98: /* Skip programname and '-K' option */
1.15 deraadt 99: argc -= 2;
100: argv += 2;
1.10 hin 101: exit(krb5_passwd(argc, argv));
1.15 deraadt 102: #else
1.10 hin 103: errx(1, "KerberosV support not enabled");
104: break;
1.1 deraadt 105: #endif
106: case 'y': /* change YP password */
107: #ifdef YP
108: if (!use_yp) {
109: fprintf(stderr, "passwd: YP not in use.\n");
110: exit(1);
111: }
112: use_kerberos = 0;
113: use_yp = 1;
114: force_yp = 1;
115: break;
116: #else
117: fprintf(stderr, "passwd: YP not compiled in\n");
118: exit(1);
119: #endif
120: default:
1.8 ericj 121: usage(1);
1.1 deraadt 122: }
123:
124: argc -= optind;
125: argv += optind;
126:
127: username = getlogin();
128: if (username == NULL) {
129: fprintf(stderr, "passwd: who are you ??\n");
130: exit(1);
131: }
1.15 deraadt 132:
133: switch (argc) {
1.1 deraadt 134: case 0:
135: break;
136: case 1:
1.18 ! hin 137: #if defined(KERBEROS5)
1.15 deraadt 138: if (use_kerberos && strcmp(argv[0], username)) {
139: (void)fprintf(stderr, "passwd: %s\n\t%s\n%s\n",
140: "to change another user's Kerberos password, do",
1.18 ! hin 141: "\"passwd -K -u <user>\";",
1.15 deraadt 142: "to change a user's local passwd, use \"passwd -l <user>\"");
143: exit(1);
144: }
1.1 deraadt 145: #endif
146: username = argv[0];
147: break;
148: default:
1.8 ericj 149: usage(1);
1.1 deraadt 150: }
1.7 art 151:
1.1 deraadt 152: #ifdef YP
1.9 millert 153: if (force_yp || ((status = local_passwd(username, 0)) && use_yp))
1.1 deraadt 154: exit(yp_passwd(username));
155: exit(status);
156: #endif
1.9 millert 157: exit(local_passwd(username, 0));
1.1 deraadt 158: }
159:
1.6 weingart 160: void
1.15 deraadt 161: usage(int retval)
1.1 deraadt 162: {
1.15 deraadt 163: fprintf(stderr, "usage: passwd [-l] [-y] [-k [-n name] [-i instance] "
164: "[-r realm] [-u username[.instance][@realm]] [user]\n");
1.8 ericj 165: exit(retval);
1.1 deraadt 166: }