|
| 1 | +#include<postgres.h> |
| 2 | +#include<libpq/password.h> |
| 3 | +#include<libpq/hba.h> |
| 4 | +#include<libpq/libpq.h> |
| 5 | +#include<string.h> |
| 6 | +#include<unistd.h> |
| 7 | + |
| 8 | +int |
| 9 | +verify_password(char*user,char*password,Port*port, |
| 10 | +char*database,char*DataDir) |
| 11 | +{ |
| 12 | +boolhost_ok; |
| 13 | +enumUserauthuserauth; |
| 14 | +charpw_file_name[PWFILE_NAME_SIZE+1]; |
| 15 | + |
| 16 | +char*pw_file_fullname; |
| 17 | +FILE*pw_file; |
| 18 | + |
| 19 | +charpw_file_line[255]; |
| 20 | +char*p,*test_user,*test_pw; |
| 21 | +charsalt[3]; |
| 22 | + |
| 23 | +find_hba_entry(DataDir,port->raddr.sin_addr,database, |
| 24 | +&host_ok,&userauth,pw_file_name, true); |
| 25 | + |
| 26 | +if(!host_ok) { |
| 27 | +sprintf(PQerrormsg, |
| 28 | +"verify_password: couldn't find entry for connecting host\n"); |
| 29 | +fputs(PQerrormsg,stderr); |
| 30 | +pqdebug("%s",PQerrormsg); |
| 31 | +returnSTATUS_ERROR; |
| 32 | + } |
| 33 | + |
| 34 | +if(userauth!=Password) { |
| 35 | +sprintf(PQerrormsg, |
| 36 | +"verify_password: couldn't find entry of type 'password' " |
| 37 | +"for this host\n"); |
| 38 | +fputs(PQerrormsg,stderr); |
| 39 | +pqdebug("%s",PQerrormsg); |
| 40 | +returnSTATUS_ERROR; |
| 41 | + } |
| 42 | + |
| 43 | +if(!pw_file_name||pw_file_name[0]=='\0') { |
| 44 | +sprintf(PQerrormsg, |
| 45 | +"verify_password: no password file specified\n"); |
| 46 | +fputs(PQerrormsg,stderr); |
| 47 | +pqdebug("%s",PQerrormsg); |
| 48 | +returnSTATUS_ERROR; |
| 49 | + } |
| 50 | + |
| 51 | +pw_file_fullname= (char*)malloc(strlen(DataDir)+strlen(pw_file_name)+2); |
| 52 | +strcpy(pw_file_fullname,DataDir); |
| 53 | +strcat(pw_file_fullname,"/"); |
| 54 | +strcat(pw_file_fullname,pw_file_name); |
| 55 | + |
| 56 | +pw_file=fopen(pw_file_fullname,"r"); |
| 57 | +if(!pw_file) { |
| 58 | +sprintf(PQerrormsg, |
| 59 | +"verify_password: couldn't open password file '%s'\n", |
| 60 | +pw_file_fullname); |
| 61 | +fputs(PQerrormsg,stderr); |
| 62 | +pqdebug("%s",PQerrormsg); |
| 63 | +returnSTATUS_ERROR; |
| 64 | + } |
| 65 | + |
| 66 | +while(!feof(pw_file)) { |
| 67 | +fgets(pw_file_line,255,pw_file); |
| 68 | +p=pw_file_line; |
| 69 | + |
| 70 | +test_user=strsep(&p,":"); |
| 71 | +test_pw=p; |
| 72 | +if(!test_user|| !test_pw|| |
| 73 | +test_user[0]=='\0'||test_pw[0]=='\0') { |
| 74 | +continue; |
| 75 | +} |
| 76 | + |
| 77 | +/* kill the newline */ |
| 78 | +test_pw[strlen(test_pw)-1]='\0'; |
| 79 | + |
| 80 | +strncpy(salt,test_pw,2); |
| 81 | +salt[2]='\0'; |
| 82 | + |
| 83 | +if(strcmp(user,test_user)==0) { |
| 84 | +/* we're outta here one way or the other. */ |
| 85 | +fclose(pw_file); |
| 86 | + |
| 87 | +if(strcmp(crypt(password,salt),test_pw)==0) { |
| 88 | +/* it matched. */ |
| 89 | +returnSTATUS_OK; |
| 90 | + } |
| 91 | + |
| 92 | +sprintf(PQerrormsg, |
| 93 | +"verify_password: password mismatch for '%s'.\n", |
| 94 | +user); |
| 95 | +fputs(PQerrormsg,stderr); |
| 96 | +pqdebug("%s",PQerrormsg); |
| 97 | +returnSTATUS_ERROR; |
| 98 | +} |
| 99 | + } |
| 100 | + |
| 101 | +sprintf(PQerrormsg, |
| 102 | +"verify_password: user '%s' not found in password file.\n", |
| 103 | +user); |
| 104 | +fputs(PQerrormsg,stderr); |
| 105 | +pqdebug("%s",PQerrormsg); |
| 106 | +returnSTATUS_ERROR; |
| 107 | +} |