Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8606221

Browse files
committed
pg_passwd cleanup.
1 parent c6e7baa commit 8606221

File tree

2 files changed

+301
-36
lines changed

2 files changed

+301
-36
lines changed

contrib/datetime/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
D=/usr/postgres
2+
P=$D/lib/datetime_functions.so
3+
CFLAGS=-fpic -O -I../../src/include -I../../src/backend
4+
5+
all: $P datetime_functions.sql
6+
7+
$P:datetime_functions.o
8+
ld -Bshareable -o $P datetime_functions.o
9+
10+
datetime_functions.sql: datetime.prot
11+
sh datetime.prot $P
12+
psql -c "\idatetime_functions.sql" template1

src/bin/pg_passwd/pg_passwd.c

+289-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
* @(#) pg_passwd.c 1.8 09:13:16 97/07/02 Y. Ichikawa
2+
* @(#) pg_passwd.c 1.8 09:13:16 97/07/02 Y. Ichikawa
33
*/
4+
45
#include <stdio.h>
56
#include <stdlib.h>
67
#include <string.h>
@@ -9,66 +10,318 @@
910
#include <errno.h>
1011
#include <sys/time.h>
1112
#include <ctype.h>
12-
#define issaltchar(c) (isalnum(c) || (c) == '.' || (c) == '/')
13+
#define issaltchar(c) (isalnum(c) || (c) == '.' || (c) == '/')
14+
1315
#include "postgres.h"
1416
#ifdef HAVE_TERMIOS_H
1517
#include <termios.h>
1618
#endif
1719
#ifdef HAVE_CRYPT_H
1820
#include <crypt.h>
1921
#else
20-
extern char *crypt(const char *, const char *);
22+
extern char *crypt(const char *, const char *);
2123
#endif
22-
char *comname;
24+
25+
char *comname;
2326
void usage(FILE *stream);
2427
void read_pwd_file(char *filename);
2528
void write_pwd_file(char *filename, char *bkname);
2629
void encrypt_pwd(char key[9], char salt[3], char passwd[14]);
2730
int check_pwd(char key[9], char passwd[14]);
2831
void prompt_for_username(char *username);
2932
void prompt_for_password(char *prompt, char *password);
33+
3034
void usage(FILE *stream)
3135
{
32-
fprintf(stream, "Usage: %s <password file>\n", comname);
36+
fprintf(stream, "Usage: %s <password file>\n", comname);
3337
}
38+
3439
typedef struct {
35-
char *uname;
36-
char *pwd;
37-
char *rest;
40+
char *uname;
41+
char *pwd;
42+
char *rest;
3843
} pg_pwd;
39-
#define MAXPWDS 1024
40-
pg_pwd pwds[MAXPWDS];
41-
int npwds = 0;
44+
45+
#define MAXPWDS 1024
46+
47+
pg_pwd pwds[MAXPWDS];
48+
int npwds = 0;
49+
50+
4251
void read_pwd_file(char *filename)
4352
{
44-
FILE *fp;
45-
static char line[512];
53+
FILE *fp;
54+
static char line[512];
4655
static char ans[128];
47-
int i;
56+
int i;
57+
4858
try_again:
4959
fp = fopen(filename, "r");
5060
if (fp == NULL) {
51-
if (errno == ENOENT) {
52-
printf("File \"%s\" does not exist. Create? (y/n): ", filename);
53-
fflush(stdout);
54-
fgets(ans, 128, stdin);
55-
switch (ans[0]) {
56-
case 'y': case 'Y':
57-
fp = fopen(filename, "w");
58-
if (fp == NULL) {
59-
perror(filename);
60-
exit(1);
61-
}
62-
fclose(fp);
63-
goto try_again;
64-
default:
65-
/* cannot continue */
66-
exit(1);
67-
}
68-
} else {
69-
perror(filename);
70-
exit(1);
71-
}
61+
if (errno == ENOENT) {
62+
printf("File \"%s\" does not exist. Create? (y/n): ", filename);
63+
fflush(stdout);
64+
fgets(ans, 128, stdin);
65+
switch (ans[0]) {
66+
case 'y': case 'Y':
67+
fp = fopen(filename, "w");
68+
if (fp == NULL) {
69+
perror(filename);
70+
exit(1);
71+
}
72+
fclose(fp);
73+
goto try_again;
74+
default:
75+
/* cannot continue */
76+
exit(1);
77+
}
78+
} else {
79+
perror(filename);
80+
exit(1);
81+
}
7282
}
83+
7384
/* read all the entries */
74-
for (npwds = 0; npwds < MAXPWDS && fgets(line, 512, fp) != NULL; ++npwds)
85+
for (npwds = 0; npwds < MAXPWDS && fgets(line, 512, fp) != NULL; ++npwds) {
86+
int l;
87+
char *p, *q;
88+
l = strlen(line);
89+
if (line[l-1] == '\n')
90+
line[l-1] = '\0';
91+
else { /* too long */
92+
fprintf(stderr, "%s: line %d: line too long.\n",
93+
filename, npwds + 1);
94+
exit(1);
95+
}
96+
97+
/* get user name */
98+
p = line;
99+
if ((q = index(p, ':')) == NULL) {
100+
fprintf(stderr, "%s: line %d: illegal format.\n",
101+
filename, npwds + 1);
102+
exit(1);
103+
}
104+
*(q++) = '\0';
105+
if (strlen(p) == 0) {
106+
fprintf(stderr, "%s: line %d: null user name.\n",
107+
filename, npwds + 1);
108+
exit(1);
109+
}
110+
pwds[npwds].uname = strdup(p);
111+
112+
/* check duplicate */
113+
for (i = 0; i < npwds; ++i) {
114+
if (strcmp(pwds[i].uname, pwds[npwds].uname) == 0) {
115+
fprintf(stderr, "%s: duplicated entry.\n", pwds[npwds].uname);
116+
exit(1);
117+
}
118+
}
119+
120+
/* get password field */
121+
p = q;
122+
q = index(p, ':');
123+
/*
124+
* --- don't care -----
125+
if ((q = index(p, ':')) == NULL) {
126+
fprintf(stderr, "%s: line %d: illegal format.\n",
127+
filename, npwds + 1);
128+
exit(1);
129+
}
130+
*/
131+
132+
if (q != NULL) *(q++) = '\0';
133+
if (strlen(p) != 13) {
134+
fprintf(stderr, "WARNING: %s: line %d: illegal password length.\n",
135+
filename, npwds + 1);
136+
}
137+
pwds[npwds].pwd = strdup(p);
138+
139+
/* rest of the line is treated as is */
140+
if (q == NULL)
141+
pwds[npwds].rest = NULL;
142+
else
143+
pwds[npwds].rest = strdup(q);
144+
}
145+
146+
fclose(fp);
147+
}
148+
149+
void write_pwd_file(char *filename, char *bkname)
150+
{
151+
FILE* fp;
152+
int i;
153+
154+
/* make the backup file */
155+
link_again:
156+
if (link(filename, bkname)) {
157+
if (errno == EEXIST) {
158+
unlink(bkname);
159+
goto link_again;
160+
}
161+
perror(bkname);
162+
exit(1);
163+
}
164+
if (unlink(filename)) {
165+
perror(filename);
166+
exit(1);
167+
}
168+
169+
/* open file */
170+
if ((fp = fopen(filename, "w")) == NULL) {
171+
perror(filename);
172+
exit(1);
173+
}
174+
175+
/* write file */
176+
for (i = 0; i < npwds; ++i) {
177+
fprintf(fp, "%s:%s%s%s\n", pwds[i].uname, pwds[i].pwd,
178+
pwds[i].rest ? ":" : "",
179+
pwds[i].rest ? pwds[i].rest : "");
180+
}
181+
182+
fclose(fp);
183+
}
184+
185+
void encrypt_pwd(char key[9], char salt[3], char passwd[14])
186+
{
187+
int n;
188+
189+
/* get encrypted password */
190+
if (salt[0] == '\0') {
191+
struct timeval tm;
192+
gettimeofday(&tm, NULL);
193+
srand(tm.tv_sec ? tm.tv_sec : 1);
194+
do {
195+
n = rand() % 256;
196+
} while (! issaltchar(n));
197+
salt[0] = n;
198+
do {
199+
n = rand() % 256;
200+
} while (! issaltchar(n));
201+
salt[1] = n;
202+
salt[2] = '\0';
203+
}
204+
strcpy(passwd, crypt(key, salt));
205+
206+
/* show it */
207+
/* fprintf(stderr, "key = %s, salt = %s, password = %s\n",
208+
key, salt, passwd); */
209+
}
210+
211+
int check_pwd(char key[9], char passwd[14])
212+
{
213+
char shouldbe[14];
214+
char salt[3];
215+
216+
salt[0] = passwd[0];
217+
salt[1] = passwd[1];
218+
salt[2] = '\0';
219+
encrypt_pwd(key, salt, shouldbe);
220+
221+
return strncmp(shouldbe, passwd, 13) == 0 ? 1 : 0;
222+
}
223+
224+
void prompt_for_username(char *username)
225+
{
226+
int length;
227+
228+
printf("Username: ");
229+
fgets(username, 9, stdin);
230+
length = strlen(username);
231+
232+
/* skip rest of the line */
233+
if (length > 0 && username[length-1] != '\n') {
234+
static char buf[512];
235+
do {
236+
fgets(buf, 512, stdin);
237+
} while (buf[strlen(buf)-1] != '\n');
238+
}
239+
if(length > 0 && username[length-1] == '\n') username[length-1] = '\0';
240+
}
241+
242+
void prompt_for_password(char *prompt, char *password)
243+
{
244+
int length;
245+
#ifdef HAVE_TERMIOS_H
246+
struct termios t_orig, t;
247+
#endif
248+
249+
printf(prompt);
250+
#ifdef HAVE_TERMIOS_H
251+
tcgetattr(0, &t);
252+
t_orig = t;
253+
t.c_lflag &= ~ECHO;
254+
tcsetattr(0, TCSADRAIN, &t);
255+
#endif
256+
fgets(password, 9, stdin);
257+
#ifdef HAVE_TERMIOS_H
258+
tcsetattr(0, TCSADRAIN, &t_orig);
259+
#endif
260+
261+
length = strlen(password);
262+
/* skip rest of the line */
263+
if (length > 0 && password[length-1] != '\n') {
264+
static char buf[512];
265+
do {
266+
fgets(buf, 512, stdin);
267+
} while (buf[strlen(buf)-1] != '\n');
268+
}
269+
if(length > 0 && password[length-1] == '\n') password[length-1] = '\0';
270+
printf("\n");
271+
}
272+
273+
274+
int main(int argc, char *argv[])
275+
{
276+
static char bkname[512];
277+
char username[9];
278+
char salt[3];
279+
char key[9], key2[9];
280+
char e_passwd[14];
281+
int i;
282+
283+
comname = argv[0];
284+
if (argc != 2) {
285+
usage(stderr);
286+
exit(1);
287+
}
288+
289+
290+
/* open file */
291+
read_pwd_file(argv[1]);
292+
293+
/* ask for the user name and the password */
294+
prompt_for_username(username);
295+
prompt_for_password("New password: ", key);
296+
prompt_for_password("Re-enter new password: ", key2);
297+
if (strncmp(key, key2, 8) != 0) {
298+
fprintf(stderr, "Password mismatch.\n");
299+
exit(1);
300+
}
301+
salt[0] = '\0';
302+
encrypt_pwd(key, salt, e_passwd);
303+
304+
/* check password entry */
305+
for (i = 0; i < npwds; ++i) {
306+
if (strcmp(pwds[i].uname, username) == 0) { /* found */
307+
pwds[i].pwd = strdup(e_passwd);
308+
break;
309+
}
310+
}
311+
if (i == npwds) { /* did not exist */
312+
if (npwds == MAXPWDS) {
313+
fprintf(stderr, "%s: cannot handle so may entries.\n", comname);
314+
exit(1);
315+
}
316+
pwds[npwds].uname = strdup(username);
317+
pwds[npwds].pwd = strdup(e_passwd);
318+
pwds[npwds].rest = NULL;
319+
++npwds;
320+
}
321+
322+
/* write back the file */
323+
sprintf(bkname, "%s.bk", argv[1]);
324+
write_pwd_file(argv[1], bkname);
325+
326+
return 0;
327+
}

0 commit comments

Comments
 (0)