]> git.saurik.com Git - apple/system_cmds.git/blame - chkpasswd.tproj/passwd.c
system_cmds-336.1.2.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 *
2fc1e207
A
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.
1815bff5
A
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,
2fc1e207
A
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."
1815bff5
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24#define INFO_NETINFO 0
25#define INFO_FILE 1
26#define INFO_NIS 2
20e66415 27#define INFO_DIRECTORYSERVICES 3
1815bff5
A
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 *);
20e66415 54extern int ds_check_passwd(char *, char *);
1815bff5
A
55
56void
57checkpasswd(char *name, char *old_pw)
58{
59 int isNull;
60 char *p;
61
62 printf("Checking password for %s.\n", name);
63
64 p = "";
65 isNull = 0;
66 if (old_pw == NULL) isNull = 1;
67 if ((isNull == 0) && (old_pw[0] == '\0')) isNull = 1;
68 if (isNull == 0)
69 {
70 p = getpass("Password:");
71 sleep(1); // make sure this doesn't go too quickly
72 if (strcmp(literal ? p : crypt(p, old_pw), old_pw))
73 {
74 errno = EACCES;
75 fprintf(stderr, "Sorry\n");
76 exit(1);
77 }
78 }
79 return;
80}
81
82void
83usage()
84{
85 fprintf(stderr, "usage: chkpasswd [-i infosystem] [-l location] [-c] [name]\n");
86 fprintf(stderr, "supported infosystems are:\n");
87 fprintf(stderr, " netinfo\n");
88 fprintf(stderr, " file\n");
89 fprintf(stderr, " nis\n");
20e66415 90 fprintf(stderr, " opendirectory\n");
1815bff5
A
91 fprintf(stderr, "for netinfo, location may be a domain name or server/tag\n");
92 fprintf(stderr, "for file, location may be a file name (%s is the default)\n",
93 _PASSWD_FILE);
94 fprintf(stderr, "for nis, location may be a NIS domainname\n");
20e66415 95 fprintf(stderr, "for opendirectory, location may be a directory node name\n");
1815bff5
A
96 fprintf(stderr, "if -c is specified, the password you supply is compared\n");
97 fprintf(stderr, "verbatim without first being crypted\n");
98 exit(1);
99}
100
101int
102main(int argc, char *argv[])
103{
104 char *user, *locn;
105 int i, infosystem;
106 struct passwd *pw;
107
20e66415 108 infosystem = INFO_DIRECTORYSERVICES;
1815bff5
A
109 user = NULL;
110 locn = NULL;
111
112 for (i = 1; i < argc; i++)
113 {
114 if (!strcmp(argv[i], "-i"))
115 {
116 if (++i >= argc)
117 {
118 fprintf(stderr, "no argument for -i option\n");
119 usage();
120 }
121
122 if (!strcmp(argv[i], "NetInfo")) infosystem = INFO_NETINFO;
123 else if (!strcmp(argv[i], "netinfo")) infosystem = INFO_NETINFO;
124 else if (!strcmp(argv[i], "File")) infosystem = INFO_FILE;
125 else if (!strcmp(argv[i], "file")) infosystem = INFO_FILE;
126 else if (!strcmp(argv[i], "NIS")) infosystem = INFO_NIS;
127 else if (!strcmp(argv[i], "nis")) infosystem = INFO_NIS;
128 else if (!strcmp(argv[i], "YP")) infosystem = INFO_NIS;
129 else if (!strcmp(argv[i], "yp")) infosystem = INFO_NIS;
20e66415 130 else if (!strcasecmp(argv[i], "opendirectory")) infosystem = INFO_DIRECTORYSERVICES;
1815bff5
A
131 else
132 {
133 fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
134 usage();
135 }
136 }
137
138 else if (!strcmp(argv[i], "-l"))
139 {
140 if (++i >= argc)
141 {
142 fprintf(stderr, "no argument for -l option\n");
143 usage();
144 }
145 locn = argv[i];
146 }
147
148 else if (!strcmp(argv[i], "-c")) literal++;
149 else if (user == NULL) user = argv[i];
150 else usage();
151 }
152
153 if (user == NULL)
154 {
155 if ((pw = getpwuid(getuid())) == NULL || (user = pw->pw_name) == NULL)
156 {
157 fprintf(stderr, "you don't have a login name\n");
158 exit(1);
159 }
160 }
161
162 switch (infosystem)
163 {
164 case INFO_NETINFO:
165 netinfo_check_passwd(user, locn);
166 break;
167 case INFO_FILE:
168 file_check_passwd(user, locn);
169 break;
170 case INFO_NIS:
171 nis_check_passwd(user, locn);
172 break;
20e66415
A
173 case INFO_DIRECTORYSERVICES:
174 ds_check_passwd(user, locn);
175 break;
1815bff5
A
176 }
177
178 exit(0);
179}
180