Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c9b549

Browse files
committed
Minor code cleanups.
1 parent7663e6b commit3c9b549

File tree

1 file changed

+51
-56
lines changed

1 file changed

+51
-56
lines changed

‎src/backend/libpq/crypt.c

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*-------------------------------------------------------------------------
22
*
33
* crypt.c
4-
*Look intopg_shadow and check the encrypted password with
5-
*the one passed in from the frontend.
4+
*Look intothe password file and check the encrypted password with
5+
*the one passed in from the frontend.
66
*
7-
*Modification History
7+
*Original coding by Todd A. Brandys
88
*
9-
*Dec 17, 1997 - Todd A. Brandys
10-
*Orignal Version Completed.
9+
*Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
10+
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $Id:crypt.c,v 1.39 2001/10/25 05:49:30 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/libpq/crypt.c,v 1.40 2001/11/01 18:10:48 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
16+
#include"postgres.h"
1617

1718
#include<errno.h>
1819
#include<unistd.h>
1920

20-
#include"postgres.h"
2121
#include"libpq/crypt.h"
2222
#include"libpq/libpq.h"
2323
#include"miscadmin.h"
@@ -82,45 +82,45 @@ crypt_openpwdfile(void)
8282
pwdfile=AllocateFile(filename,"r");
8383

8484
if (pwdfile==NULL&&errno!=ENOENT)
85-
elog(DEBUG,"could not open %s: %s",filename,strerror(errno));
85+
elog(DEBUG,"could not open %s: %m",filename);
8686

8787
pfree(filename);
8888

8989
returnpwdfile;
9090
}
9191

92-
/*-------------------------------------------------------------------------*/
93-
92+
/*
93+
* Compare two password-file lines on the basis of their usernames.
94+
*
95+
* Can also be used to compare just a username against a password-file
96+
* line (for bsearch).
97+
*/
9498
staticint
9599
compar_user(constvoid*user_a,constvoid*user_b)
96100
{
97-
98-
intmin,
99-
value;
100101
char*login_a;
101102
char*login_b;
103+
intlen_a,
104+
len_b,
105+
result;
102106

103107
login_a=*((char**)user_a);
104108
login_b=*((char**)user_b);
105109

106110
/*
107-
* We only really want to compare the user logins which are first.We
108-
* look for the first SEPSTR char getting the number of chars there
109-
* are before it. We only need to compare to the min count from the
110-
* two strings.
111+
* We only really want to compare the user logins which are first
112+
* and are terminated by CRYPT_PWD_FILE_SEPSTR. (NB: this code
113+
* effectively assumes that CRYPT_PWD_FILE_SEPSTR is just one char.)
111114
*/
112-
min=strcspn(login_a,CRYPT_PWD_FILE_SEPSTR);
113-
value=strcspn(login_b,CRYPT_PWD_FILE_SEPSTR);
114-
if (value<min)
115-
min=value;
115+
len_a=strcspn(login_a,CRYPT_PWD_FILE_SEPSTR);
116+
len_b=strcspn(login_b,CRYPT_PWD_FILE_SEPSTR);
116117

117-
/*
118-
* We add one to min so that the separator character is included in
119-
* the comparison.Why? I believe this will prevent logins that are
120-
* proper prefixes of other logins from being 'masked out'. Being
121-
* conservative!
122-
*/
123-
returnstrncmp(login_a,login_b,min+1);
118+
result=strncmp(login_a,login_b,Min(len_a,len_b));
119+
120+
if (result==0)/* one could be a prefix of the other */
121+
result= (len_a-len_b);
122+
123+
returnresult;
124124
}
125125

126126
/*-------------------------------------------------------------------------*/
@@ -131,7 +131,7 @@ crypt_loadpwdfile(void)
131131
char*filename;
132132
intresult;
133133
FILE*pwd_file;
134-
charbuffer[256];
134+
charbuffer[1024];
135135

136136
filename=crypt_getpwdreloadfilename();
137137
result=unlink(filename);
@@ -145,9 +145,9 @@ crypt_loadpwdfile(void)
145145
*/
146146
if (!pwd_cache||result==0)
147147
{
148+
/* free the old data only if this is a reload */
148149
if (pwd_cache)
149-
{/* free the old data only if this is a
150-
* reload */
150+
{
151151
while (pwd_cache_count--)
152152
free((void*)pwd_cache[pwd_cache_count]);
153153
free((void*)pwd_cache);
@@ -161,7 +161,7 @@ crypt_loadpwdfile(void)
161161
/*
162162
* Here is where we load the data from pg_pwd.
163163
*/
164-
while (fgets(buffer,256,pwd_file)!=NULL)
164+
while (fgets(buffer,sizeof(buffer),pwd_file)!=NULL)
165165
{
166166
/*
167167
* We must remove the return char at the end of the string, as
@@ -170,7 +170,9 @@ crypt_loadpwdfile(void)
170170
if (buffer[(result=strlen(buffer)-1)]=='\n')
171171
buffer[result]='\0';
172172

173-
pwd_cache= (char**)realloc((void*)pwd_cache,sizeof(char*)* (pwd_cache_count+1));
173+
pwd_cache= (char**)
174+
realloc((void*)pwd_cache,
175+
sizeof(char*)* (pwd_cache_count+1));
174176
pwd_cache[pwd_cache_count++]=strdup(buffer);
175177
}
176178
FreeFile(pwd_file);
@@ -187,7 +189,6 @@ crypt_loadpwdfile(void)
187189
staticvoid
188190
crypt_parsepwdentry(char*buffer,char**pwd,char**valdate)
189191
{
190-
191192
char*parse=buffer;
192193
intcount,
193194
i;
@@ -208,7 +209,7 @@ crypt_parsepwdentry(char *buffer, char **pwd, char **valdate)
208209
parse+= (count+1);
209210

210211
/*
211-
* store a copy of date login becomes invalid
212+
* store a copy ofthedate login becomes invalid
212213
*/
213214
count=strcspn(parse,CRYPT_PWD_FILE_SEPSTR);
214215
*valdate= (char*)palloc(count+1);
@@ -222,32 +223,26 @@ crypt_parsepwdentry(char *buffer, char **pwd, char **valdate)
222223
staticint
223224
crypt_getloginfo(constchar*user,char**passwd,char**valuntil)
224225
{
225-
char*pwd,
226-
*valdate;
227-
void*fakeout;
228-
229-
*passwd=NULL;
230-
*valuntil=NULL;
231226
crypt_loadpwdfile();
232227

233228
if (pwd_cache)
234229
{
235230
char**pwd_entry;
236-
charuser_search[NAMEDATALEN+2];
237231

238-
snprintf(user_search,NAMEDATALEN+2,"%s\t",user);
239-
fakeout= (void*)&user_search;
240-
if ((pwd_entry= (char**)bsearch((void*)&fakeout, (void*)pwd_cache,pwd_cache_count,sizeof(char*),compar_user)))
232+
pwd_entry= (char**)bsearch((void*)&user,
233+
(void*)pwd_cache,
234+
pwd_cache_count,
235+
sizeof(char*),
236+
compar_user);
237+
if (pwd_entry)
241238
{
242-
crypt_parsepwdentry(*pwd_entry,&pwd,&valdate);
243-
*passwd=pwd;
244-
*valuntil=valdate;
239+
crypt_parsepwdentry(*pwd_entry,passwd,valuntil);
245240
returnSTATUS_OK;
246241
}
247-
248-
returnSTATUS_OK;
249242
}
250243

244+
*passwd=NULL;
245+
*valuntil=NULL;
251246
returnSTATUS_ERROR;
252247
}
253248

@@ -256,23 +251,20 @@ crypt_getloginfo(const char *user, char **passwd, char **valuntil)
256251
int
257252
md5_crypt_verify(constPort*port,constchar*user,constchar*pgpass)
258253
{
259-
260254
char*passwd,
261255
*valuntil,
262256
*crypt_pwd;
263257
intretval=STATUS_ERROR;
264-
AbsoluteTimevuntil,
265-
current;
266258

267259
if (crypt_getloginfo(user,&passwd,&valuntil)==STATUS_ERROR)
268260
returnSTATUS_ERROR;
269261

270262
if (passwd==NULL||*passwd=='\0')
271263
{
272264
if (passwd)
273-
pfree((void*)passwd);
265+
pfree(passwd);
274266
if (valuntil)
275-
pfree((void*)valuntil);
267+
pfree(valuntil);
276268
returnSTATUS_ERROR;
277269
}
278270

@@ -342,8 +334,11 @@ md5_crypt_verify(const Port *port, const char *user, const char *pgpass)
342334
if (strcmp(pgpass,crypt_pwd)==0)
343335
{
344336
/*
345-
*check here to be sure we are not past valuntil
337+
*Password OK, now check to be sure we are not past valuntil
346338
*/
339+
AbsoluteTimevuntil,
340+
current;
341+
347342
if (!valuntil||strcmp(valuntil,"\\N")==0)
348343
vuntil=INVALID_ABSTIME;
349344
else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp