system_cmds-735.20.1.tar.gz
[apple/system_cmds.git] / chkpasswd.tproj / passwd.c
0 / 174 (  0%)
CommitLineData
1/*
2 * Copyright (c) 1999-2016 Apple 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_OPEN_DIRECTORY 3
27#define INFO_PAM 4
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 "stringops.h"
43
44#ifdef __SLICK__
45#define _PASSWORD_LEN 8
46#endif
47
48#include "passwd.h"
49
50const char* progname = "chkpasswd";
51
52static int literal = 0;
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
80static void
81usage(void)
82{
83 fprintf(stderr, "usage: chkpasswd [-i infosystem] [-l location] [-c] [name]\n");
84 fprintf(stderr, " infosystem:\n");
85 fprintf(stderr, " file\n");
86 fprintf(stderr, " NIS\n");
87 fprintf(stderr, " OpenDirectory\n");
88 fprintf(stderr, " location (for infosystem):\n");
89 fprintf(stderr, " file location is path to file (default is %s)\n", _PASSWD_FILE);
90 fprintf(stderr, " NIS location is NIS domain name\n");
91 fprintf(stderr, " OpenDirectory location is directory node name\n");
92 fprintf(stderr, " -c: supplied password is compared verbatim without first\n");
93 fprintf(stderr, " being crypted\n");
94 exit(1);
95}
96
97int
98main(int argc, char *argv[])
99{
100 char* user = NULL;
101 char* locn = NULL;
102 int infosystem, ch;
103
104 infosystem = INFO_PAM;
105
106 while ((ch = getopt(argc, argv, "ci:l:")) != -1) {
107 switch(ch) {
108 case 'i':
109 if (!strcasecmp(optarg, "file")) {
110 infosystem = INFO_FILE;
111 } else if (!strcasecmp(optarg, "NIS")) {
112 infosystem = INFO_NIS;
113 } else if (!strcasecmp(optarg, "YP")) {
114 infosystem = INFO_NIS;
115 } else if (!strcasecmp(optarg, "opendirectory")) {
116 infosystem = INFO_OPEN_DIRECTORY;
117 } else if (!strcasecmp(optarg, "PAM")) {
118 infosystem = INFO_PAM;
119 } else {
120 fprintf(stderr, "%s: Unknown info system \'%s\'.\n",
121 progname, optarg);
122 usage();
123 }
124 break;
125 case 'l':
126 locn = optarg;
127 break;
128 case 'c':
129 literal++;
130 break;
131 case '?':
132 default:
133 usage();
134 break;
135 }
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (argc > 1) {
141 usage();
142 } else if (argc == 1) {
143 user = argv[0];
144 }
145
146 if (user == NULL) {
147 struct passwd* pw = getpwuid(getuid());
148 if (pw != NULL && pw->pw_name != NULL) {
149 user = strdup(pw->pw_name);
150 }
151 if (user == NULL) {
152 fprintf(stderr, "you don't have a login name\n");
153 exit(1);
154 }
155 }
156
157 switch (infosystem)
158 {
159 case INFO_FILE:
160 file_check_passwd(user, locn);
161 break;
162 case INFO_NIS:
163 nis_check_passwd(user, locn);
164 break;
165 case INFO_OPEN_DIRECTORY:
166 od_check_passwd(user, locn);
167 break;
168 case INFO_PAM:
169 pam_check_passwd(user);
170 break;
171 }
172
173 exit(0);
174}