@@ -19,14 +19,23 @@ extern char *crypt(const char *, const char *);
19
19
20
20
#endif
21
21
22
- #define PG_PASSWD_LEN 13/* not including null */
22
+ /*
23
+ * We assume that the output of crypt(3) is always 13 characters,
24
+ * and that at most 8 characters can usefully be sent to it.
25
+ *
26
+ * Postgres usernames are assumed to be less than NAMEDATALEN chars long.
27
+ */
28
+ #define CLEAR_PASSWD_LEN 8/* not including null */
29
+ #define CRYPTED_PASSWD_LEN 13/* not including null */
23
30
24
31
const char * progname ;
25
32
26
33
static void usage (void );
27
34
static void read_pwd_file (char * filename );
28
35
static void write_pwd_file (char * filename ,char * bkname );
29
- static void encrypt_pwd (char key [9 ],char salt [3 ],char passwd [PG_PASSWD_LEN + 1 ]);
36
+ static void encrypt_pwd (char key [CLEAR_PASSWD_LEN + 1 ],
37
+ char salt [3 ],
38
+ char passwd [CRYPTED_PASSWD_LEN + 1 ]);
30
39
static void prompt_for_username (char * username );
31
40
static void prompt_for_password (char * prompt ,char * password );
32
41
@@ -94,7 +103,9 @@ read_pwd_file(char *filename)
94
103
}
95
104
96
105
/* read all the entries */
97
- for (npwds = 0 ;npwds < MAXPWDS && fgets (line ,512 ,fp )!= NULL ;++ npwds )
106
+ for (npwds = 0 ;
107
+ npwds < MAXPWDS && fgets (line ,sizeof (line ),fp )!= NULL ;
108
+ ++ npwds )
98
109
{
99
110
int l ;
100
111
char * p ,
@@ -123,13 +134,13 @@ read_pwd_file(char *filename)
123
134
}
124
135
pwds [npwds ].uname = strdup (p );
125
136
126
- /* check duplicate */
137
+ /* checkfor duplicate user name */
127
138
for (i = 0 ;i < npwds ;++ i )
128
139
{
129
140
if (strcmp (pwds [i ].uname ,pwds [npwds ].uname )== 0 )
130
141
{
131
- fprintf (stderr ,"Duplicated entry: %s\n" ,
132
- pwds [npwds ].uname );
142
+ fprintf (stderr ,"Duplicate username %s in entry %d \n" ,
143
+ pwds [npwds ].uname , npwds + 1 );
133
144
exit (1 );
134
145
}
135
146
}
@@ -143,7 +154,7 @@ read_pwd_file(char *filename)
143
154
if (q != NULL )
144
155
* (q ++ )= '\0' ;
145
156
146
- if (strlen (p )!= PG_PASSWD_LEN && strcmp (p ,"+" )!= 0 )
157
+ if (strlen (p )!= CRYPTED_PASSWD_LEN && strcmp (p ,"+" )!= 0 )
147
158
{
148
159
fprintf (stderr ,"%s:%d: warning: invalid password length\n" ,
149
160
filename ,npwds + 1 );
@@ -209,11 +220,13 @@ write_pwd_file(char *filename, char *bkname)
209
220
}
210
221
211
222
static void
212
- encrypt_pwd (char key [9 ],char salt [3 ],char passwd [PG_PASSWD_LEN + 1 ])
223
+ encrypt_pwd (char key [CLEAR_PASSWD_LEN + 1 ],
224
+ char salt [3 ],
225
+ char passwd [CRYPTED_PASSWD_LEN + 1 ])
213
226
{
214
227
int n ;
215
228
216
- /*get encrypted password */
229
+ /*select a salt, if not already given */
217
230
if (salt [0 ]== '\0' )
218
231
{
219
232
srand (time (NULL ));
@@ -229,32 +242,16 @@ encrypt_pwd(char key[9], char salt[3], char passwd[PG_PASSWD_LEN + 1])
229
242
salt [1 ]= n ;
230
243
salt [2 ]= '\0' ;
231
244
}
245
+
246
+ /* get encrypted password */
232
247
strcpy (passwd ,crypt (key ,salt ));
233
248
249
+ #ifdef PG_PASSWD_DEBUG
234
250
/* show it */
235
-
236
- /*
237
- * fprintf(stderr, "key = %s, salt = %s, password = %s\n", key, salt,
238
- * passwd);
239
- */
240
- }
241
-
242
- #ifdef NOT_USED
243
- static int
244
- check_pwd (char key [9 ],char passwd [PG_PASSWD_LEN + 1 ])
245
- {
246
- char shouldbe [PG_PASSWD_LEN + 1 ];
247
- char salt [3 ];
248
-
249
- salt [0 ]= passwd [0 ];
250
- salt [1 ]= passwd [1 ];
251
- salt [2 ]= '\0' ;
252
- encrypt_pwd (key ,salt ,shouldbe );
253
-
254
- return strncmp (shouldbe ,passwd ,PG_PASSWD_LEN )== 0 ?1 :0 ;
255
- }
256
-
251
+ fprintf (stderr ,"key = %s, salt = %s, password = %s\n" ,
252
+ key ,salt ,passwd );
257
253
#endif
254
+ }
258
255
259
256
static void
260
257
prompt_for_username (char * username )
@@ -263,7 +260,7 @@ prompt_for_username(char *username)
263
260
264
261
printf ("Username: " );
265
262
fflush (stdout );
266
- if (fgets (username ,9 ,stdin )== NULL )
263
+ if (fgets (username ,NAMEDATALEN ,stdin )== NULL )
267
264
username [0 ]= '\0' ;
268
265
269
266
length = strlen (username );
@@ -295,16 +292,19 @@ prompt_for_password(char *prompt, char *password)
295
292
296
293
#endif
297
294
298
- printf (prompt );
299
- fflush (stdout );
300
295
#ifdef HAVE_TERMIOS_H
301
296
tcgetattr (0 ,& t );
302
297
t_orig = t ;
303
298
t .c_lflag &= ~ECHO ;
304
299
tcsetattr (0 ,TCSADRAIN ,& t );
305
300
#endif
306
- if (fgets (password ,9 ,stdin )== NULL )
301
+
302
+ printf (prompt );
303
+ fflush (stdout );
304
+
305
+ if (fgets (password ,CLEAR_PASSWD_LEN + 1 ,stdin )== NULL )
307
306
password [0 ]= '\0' ;
307
+
308
308
#ifdef HAVE_TERMIOS_H
309
309
tcsetattr (0 ,TCSADRAIN ,& t_orig );
310
310
#endif
@@ -332,13 +332,13 @@ prompt_for_password(char *prompt, char *password)
332
332
int
333
333
main (int argc ,char * argv [])
334
334
{
335
- static char bkname [MAXPGPATH ];
336
335
char * filename ;
337
- char username [9 ];
336
+ char bkname [MAXPGPATH ];
337
+ char username [NAMEDATALEN ];
338
338
char salt [3 ];
339
- char key [9 ],
340
- key2 [9 ];
341
- char e_passwd [PG_PASSWD_LEN + 1 ];
339
+ char key [CLEAR_PASSWD_LEN + 1 ],
340
+ key2 [CLEAR_PASSWD_LEN + 1 ];
341
+ char e_passwd [CRYPTED_PASSWD_LEN + 1 ];
342
342
int i ;
343
343
344
344
progname = argv [0 ];
@@ -376,7 +376,7 @@ main(int argc, char *argv[])
376
376
prompt_for_username (username );
377
377
prompt_for_password ("New password: " ,key );
378
378
prompt_for_password ("Re-enter new password: " ,key2 );
379
- if (strncmp (key ,key2 , 8 )!= 0 )
379
+ if (strcmp (key ,key2 )!= 0 )
380
380
{
381
381
fprintf (stderr ,"Password mismatch\n" );
382
382
exit (1 );
@@ -397,7 +397,7 @@ main(int argc, char *argv[])
397
397
{/* did not exist */
398
398
if (npwds == MAXPWDS )
399
399
{
400
- fprintf (stderr ,"Cannot handle somay entries\n" );
400
+ fprintf (stderr ,"Cannot handle somany entries\n" );
401
401
exit (1 );
402
402
}
403
403
pwds [npwds ].uname = strdup (username );