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