]> git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/passwd.c
ac50cd0dfa7e574e0ddda2cacd08b855d883f8be
[apple/system_cmds.git] / chkpasswd.tproj / passwd.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #define INFO_FILE 1
25 #define INFO_NIS 2
26 #define INFO_DIRECTORYSERVICES 3
27
28 #ifndef __SLICK__
29 #define _PASSWD_FILE "/etc/master.passwd"
30 #else
31 #define _PASSWD_FILE "/etc/passwd"
32 #endif
33
34 #include <stdio.h>
35 #include <errno.h>
36 #include <pwd.h>
37 #include <libc.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <pwd.h>
41 #include "stringops.h"
42
43 #ifdef __SLICK__
44 #define _PASSWORD_LEN 8
45 #endif
46
47 static int literal = 0;
48
49 extern int file_check_passwd(char *, char *);
50 extern int nis_check_passwd(char *, char *);
51 extern int ds_check_passwd(char *, char *);
52
53 void
54 checkpasswd(char *name, char *old_pw)
55 {
56 int isNull;
57 char *p;
58
59 printf("Checking password for %s.\n", name);
60
61 p = "";
62 isNull = 0;
63 if (old_pw == NULL) isNull = 1;
64 if ((isNull == 0) && (old_pw[0] == '\0')) isNull = 1;
65 if (isNull == 0)
66 {
67 p = getpass("Password:");
68 sleep(1); // make sure this doesn't go too quickly
69 if (strcmp(literal ? p : crypt(p, old_pw), old_pw))
70 {
71 errno = EACCES;
72 fprintf(stderr, "Sorry\n");
73 exit(1);
74 }
75 }
76 return;
77 }
78
79 void
80 usage()
81 {
82 fprintf(stderr, "usage: chkpasswd [-i infosystem] [-l location] [-c] [name]\n");
83 fprintf(stderr, "supported infosystems are:\n");
84 fprintf(stderr, " file\n");
85 fprintf(stderr, " nis\n");
86 fprintf(stderr, " opendirectory\n");
87 fprintf(stderr, "for file, location may be a file name (%s is the default)\n",
88 _PASSWD_FILE);
89 fprintf(stderr, "for nis, location may be a NIS domainname\n");
90 fprintf(stderr, "for opendirectory, location may be a directory node name\n");
91 fprintf(stderr, "if -c is specified, the password you supply is compared\n");
92 fprintf(stderr, "verbatim without first being crypted\n");
93 exit(1);
94 }
95
96 int
97 main(int argc, char *argv[])
98 {
99 char *user, *locn;
100 int i, infosystem;
101 struct passwd *pw;
102
103 infosystem = INFO_DIRECTORYSERVICES;
104 user = NULL;
105 locn = NULL;
106
107 for (i = 1; i < argc; i++)
108 {
109 if (!strcmp(argv[i], "-i"))
110 {
111 if (++i >= argc)
112 {
113 fprintf(stderr, "no argument for -i option\n");
114 usage();
115 }
116
117 if (!strcmp(argv[i], "File")) infosystem = INFO_FILE;
118 else if (!strcmp(argv[i], "file")) infosystem = INFO_FILE;
119 else if (!strcmp(argv[i], "NIS")) infosystem = INFO_NIS;
120 else if (!strcmp(argv[i], "nis")) infosystem = INFO_NIS;
121 else if (!strcmp(argv[i], "YP")) infosystem = INFO_NIS;
122 else if (!strcmp(argv[i], "yp")) infosystem = INFO_NIS;
123 else if (!strcasecmp(argv[i], "opendirectory")) infosystem = INFO_DIRECTORYSERVICES;
124 else
125 {
126 fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
127 usage();
128 }
129 }
130
131 else if (!strcmp(argv[i], "-l"))
132 {
133 if (++i >= argc)
134 {
135 fprintf(stderr, "no argument for -l option\n");
136 usage();
137 }
138 locn = argv[i];
139 }
140
141 else if (!strcmp(argv[i], "-c")) literal++;
142 else if (user == NULL) user = argv[i];
143 else usage();
144 }
145
146 if (user == NULL)
147 {
148 if ((pw = getpwuid(getuid())) == NULL || (user = pw->pw_name) == NULL)
149 {
150 fprintf(stderr, "you don't have a login name\n");
151 exit(1);
152 }
153 }
154
155 switch (infosystem)
156 {
157 case INFO_FILE:
158 file_check_passwd(user, locn);
159 break;
160 case INFO_NIS:
161 nis_check_passwd(user, locn);
162 break;
163 case INFO_DIRECTORYSERVICES:
164 ds_check_passwd(user, locn);
165 break;
166 }
167
168 exit(0);
169 }
170