forked from yellowman/nsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwd.c
187 lines (162 loc) · 4.4 KB
/
passwd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2004
* Christian Gut. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "externs.h"
int read_pass(char *, size_t);
int write_pass(char *);
int gen_salt(char *, size_t);
char *bcrypt_gensalt(u_int8_t);
/* read_pass reads the (blowfish crypted) password from a file */
int
read_pass(char *pass, size_t size)
{
FILE *pwdhandle;
pwdhandle = fopen(NSHPASSWD_TEMP, "r");
if (pwdhandle == NULL)
return (0);
fgets(pass, size, pwdhandle);
fclose(pwdhandle);
return (1);
}
/* write the crypted password to the passwd-temp file */
int
write_pass(char *cpass)
{
FILE *pwdhandle;
umask(S_IWGRP | S_IRWXO);
/* maybe we should flock here? */
pwdhandle = fopen(NSHPASSWD_TEMP, "w");
if (pwdhandle == NULL) {
printf("%% Unable to write run-time crypt repository: %s\n",
strerror(errno));
return (0);
}
fprintf(pwdhandle, "%s", cpass);
fclose(pwdhandle);
return (1);
}
int
gen_salt(char *salt, size_t saltlen)
{
/* 6 is a rounds value like from localcipher option of login.conf */
strlcpy(salt, bcrypt_gensalt(6), saltlen);
return 1;
}
/*
* enable privileged mode
*/
int
enable(int argc, char **argv)
{
char *p, *cpass;
char salt[_PASSWORD_LEN];
char pass[_PASSWORD_LEN + 1];
switch (argc) {
case 1:
if (priv == 1)
return 0;
/* try to read pass */
if (!(read_pass(pass, sizeof(pass)))) {
if (errno == ENOENT) {
/* no password file, so enable */
priv = 1;
return 1;
} else {
/* cant read password file */
printf("%% Unable to read password: %s\n",
strerror(errno));
return 0;
}
}
p = getpass("Password:");
if (p == NULL || *p == '\0')
return 0;
if (strcmp(crypt(p, pass), pass) == 0) {
priv = 1;
return 1;
} else {
printf("%% Password incorrect\n");
return 0;
}
case 2:
if (argv[1][0] == '?') {
/* print help */
printf("%% enable\t\t\t\tenable privileged mode\n");
printf("%% enable ?\t\t\t\tShow Options\n");
printf("%% enable secret <password>\t\tSet password"
"(plaintext)\n");
printf("%% enable secret <cipher> <hash>\t\tSet"
" password(ciphertext)\n");
return 1;
} else {
printf("%% Invalid argument: %s\n", argv[1]);
return 0;
}
case 3:
if (!isprefix(argv[1], "secret")) {
printf("%% Invalid argument: %s\n", argv[1]);
return 0;
}
if (priv != 1) {
printf("%% Privilege required\n");
return 0;
}
/* crypt plaintext and save as pass */
strlcpy(pass, argv[2], sizeof(pass));
gen_salt(salt, sizeof(salt));
if ((cpass = crypt(pass, salt)) == NULL) {
printf("%% crypt failed\n");
return 0;
}
return(write_pass(cpass));
case 4:
if (!isprefix(argv[1], "secret")) {
printf("%% Invalid argument: %s\n", argv[2]);
return 0;
}
if (!isprefix(argv[2], "blowfish")) {
printf("%% Invalid cipher: %s\n", argv[3]);
return 0;
}
/* privileged? */
if (priv != 1) {
printf("%% Privilege required\n");
return 0;
}
/* set crypted pass */
strlcpy(pass, argv[3], sizeof(pass));
return (write_pass(pass));
default:
printf("%% Too many arguments\n");
return 0;
}
}