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