aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_passwd/pg_passwd.c
blob: de9ffd65a7c9a505ff1ba7cb9983952ad18957b3 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * @(#) pg_passwd.c 1.8 09:13:16 97/07/02		Y. Ichikawa
 */
#include "postgres.h"

#include <stdio.h>
#include <stdlib.h>
#if defined(HAVE_STRING_H)
#include <string.h>
#else
#include <strings.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#define issaltchar(c)	(isalnum(c) || (c) == '.' || (c) == '/')

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#else
extern char *crypt(const char *, const char *);

#endif

char	   *comname;
void		usage(FILE *stream);
void		read_pwd_file(char *filename);
void		write_pwd_file(char *filename, char *bkname);
void		encrypt_pwd(char key[9], char salt[3], char passwd[14]);
int			check_pwd(char key[9], char passwd[14]);
void		prompt_for_username(char *username);
void		prompt_for_password(char *prompt, char *password);

void
usage(FILE *stream)
{
	fprintf(stream, "Usage: %s <password file>\n", comname);
}

typedef struct
{
	char	   *uname;
	char	   *pwd;
	char	   *rest;
} pg_pwd;

#define MAXPWDS 1024

pg_pwd		pwds[MAXPWDS];
int			npwds = 0;


void
read_pwd_file(char *filename)
{
	FILE	   *fp;
	static char line[512];
	static char ans[128];
	int			i;

try_again:
	fp = fopen(filename, "r");
	if (fp == NULL)
	{
		if (errno == ENOENT)
		{
			printf("File \"%s\" does not exist.  Create? (y/n): ", filename);
			fflush(stdout);
			fgets(ans, 128, stdin);
			switch (ans[0])
			{
				case 'y':
				case 'Y':
					fp = fopen(filename, "w");
					if (fp == NULL)
					{
						perror(filename);
						exit(1);
					}
					fclose(fp);
					goto try_again;
				default:
					/* cannot continue */
					exit(1);
			}
		}
		else
		{
			perror(filename);
			exit(1);
		}
	}

	/* read all the entries */
	for (npwds = 0; npwds < MAXPWDS && fgets(line, 512, fp) != NULL; ++npwds)
	{
		int			l;
		char	   *p,
				   *q;

		l = strlen(line);
		if (line[l - 1] == '\n')
			line[l - 1] = '\0';
		else
		{						/* too long */
			fprintf(stderr, "%s: line %d: line too long.\n",
					filename, npwds + 1);
			exit(1);
		}

		/* get user name */
		p = line;
		if ((q = strchr(p, ':')) == NULL)
		{
			fprintf(stderr, "%s: line %d: illegal format.\n",
					filename, npwds + 1);
			exit(1);
		}
		*(q++) = '\0';
		if (strlen(p) == 0)
		{
			fprintf(stderr, "%s: line %d: null user name.\n",
					filename, npwds + 1);
			exit(1);
		}
		pwds[npwds].uname = strdup(p);

		/* check duplicate */
		for (i = 0; i < npwds; ++i)
		{
			if (strcmp(pwds[i].uname, pwds[npwds].uname) == 0)
			{
				fprintf(stderr, "%s: duplicated entry.\n", pwds[npwds].uname);
				exit(1);
			}
		}

		/* get password field */
		p = q;
		q = strchr(p, ':');

		/*
		 * --- don't care ----- if ((q = strchr(p, ':')) == NULL) {
		 * fprintf(stderr, "%s: line %d: illegal format.\n", filename,
		 * npwds + 1); exit(1); }
		 */

		if (q != NULL)
			*(q++) = '\0';
		if (strlen(p) != 13)
		{
			fprintf(stderr, "WARNING: %s: line %d: illegal password length.\n",
					filename, npwds + 1);
		}
		pwds[npwds].pwd = strdup(p);

		/* rest of the line is treated as is */
		if (q == NULL)
			pwds[npwds].rest = NULL;
		else
			pwds[npwds].rest = strdup(q);
	}

	fclose(fp);
}

void
write_pwd_file(char *filename, char *bkname)
{
	FILE	   *fp;
	int			i;

	/* make the backup file */
link_again:
	if (link(filename, bkname))
	{
		if (errno == EEXIST)
		{
			unlink(bkname);
			goto link_again;
		}
		perror(bkname);
		exit(1);
	}
	if (unlink(filename))
	{
		perror(filename);
		exit(1);
	}

	/* open file */
	if ((fp = fopen(filename, "w")) == NULL)
	{
		perror(filename);
		exit(1);
	}

	/* write file */
	for (i = 0; i < npwds; ++i)
	{
		fprintf(fp, "%s:%s%s%s\n", pwds[i].uname, pwds[i].pwd,
				pwds[i].rest ? ":" : "",
				pwds[i].rest ? pwds[i].rest : "");
	}

	fclose(fp);
}

void
encrypt_pwd(char key[9], char salt[3], char passwd[14])
{
	int			n;

	/* get encrypted password */
	if (salt[0] == '\0')
	{
		srand(time(NULL));
		do
		{
			n = rand() % 256;
		} while (!issaltchar(n));
		salt[0] = n;
		do
		{
			n = rand() % 256;
		} while (!issaltchar(n));
		salt[1] = n;
		salt[2] = '\0';
	}
	strcpy(passwd, crypt(key, salt));

	/* show it */

	/*
	 * fprintf(stderr, "key = %s, salt = %s, password = %s\n", key, salt,
	 * passwd);
	 */
}

int
check_pwd(char key[9], char passwd[14])
{
	char		shouldbe[14];
	char		salt[3];

	salt[0] = passwd[0];
	salt[1] = passwd[1];
	salt[2] = '\0';
	encrypt_pwd(key, salt, shouldbe);

	return strncmp(shouldbe, passwd, 13) == 0 ? 1 : 0;
}

void
prompt_for_username(char *username)
{
	int			length;

	printf("Username: ");
	fgets(username, 9, stdin);
	length = strlen(username);

	/* skip rest of the line */
	if (length > 0 && username[length - 1] != '\n')
	{
		static char buf[512];

		do
		{
			fgets(buf, 512, stdin);
		} while (buf[strlen(buf) - 1] != '\n');
	}
	if (length > 0 && username[length - 1] == '\n')
		username[length - 1] = '\0';
}

void
prompt_for_password(char *prompt, char *password)
{
	int			length;

#ifdef HAVE_TERMIOS_H
	struct termios t_orig,
				t;

#endif

	printf(prompt);
#ifdef HAVE_TERMIOS_H
	tcgetattr(0, &t);
	t_orig = t;
	t.c_lflag &= ~ECHO;
	tcsetattr(0, TCSADRAIN, &t);
#endif
	fgets(password, 9, stdin);
#ifdef HAVE_TERMIOS_H
	tcsetattr(0, TCSADRAIN, &t_orig);
#endif

	length = strlen(password);
	/* skip rest of the line */
	if (length > 0 && password[length - 1] != '\n')
	{
		static char buf[512];

		do
		{
			fgets(buf, 512, stdin);
		} while (buf[strlen(buf) - 1] != '\n');
	}
	if (length > 0 && password[length - 1] == '\n')
		password[length - 1] = '\0';
	printf("\n");
}


int
main(int argc, char *argv[])
{
	static char bkname[512];
	char		username[9];
	char		salt[3];
	char		key[9],
				key2[9];
	char		e_passwd[14];
	int			i;

	comname = argv[0];
	if (argc != 2)
	{
		usage(stderr);
		exit(1);
	}


	/* open file */
	read_pwd_file(argv[1]);

	/* ask for the user name and the password */
	prompt_for_username(username);
	prompt_for_password("New password: ", key);
	prompt_for_password("Re-enter new password: ", key2);
	if (strncmp(key, key2, 8) != 0)
	{
		fprintf(stderr, "Password mismatch.\n");
		exit(1);
	}
	salt[0] = '\0';
	encrypt_pwd(key, salt, e_passwd);

	/* check password entry */
	for (i = 0; i < npwds; ++i)
	{
		if (strcmp(pwds[i].uname, username) == 0)
		{						/* found */
			pwds[i].pwd = strdup(e_passwd);
			break;
		}
	}
	if (i == npwds)
	{							/* did not exist */
		if (npwds == MAXPWDS)
		{
			fprintf(stderr, "%s: cannot handle so may entries.\n", comname);
			exit(1);
		}
		pwds[npwds].uname = strdup(username);
		pwds[npwds].pwd = strdup(e_passwd);
		pwds[npwds].rest = NULL;
		++npwds;
	}

	/* write back the file */
	sprintf(bkname, "%s.bk", argv[1]);
	write_pwd_file(argv[1], bkname);

	return 0;
}