パスワードライブラリ ヘッダ More...
#include "tools.h"
#include <pwd.h>
Go to the source code of this file.
Defines | |
#define | LEN_DOLLAR_SALT 12 |
#define | LEN_DOLLAR2_SALT 29 |
#define | LEN_DOLLAR5_SALT 20 |
#define | LEN_DOLLAR6_SALT 20 |
#define | LEN_MD5PASS 34 |
#define | LEN_MD5SALT 12 |
#define | LEN_DESPASS 13 |
#define | LEN_DESSALT 2 |
Functions | |
char * | get_passwd (char *user_id) |
void | free_pw (struct passwd *pw) |
int | check_passwd (char *passwd, char *cryptpass) |
int | check_salt (char *passwd) |
char * | x2crypt (char *pass, char *salt) |
Definition in file password.h.
#define LEN_DESPASS 13 |
Definition at line 55 of file password.h.
Referenced by check_passwd(), check_salt(), command_PASSWD(), command_USERID(), and x2crypt().
#define LEN_DESSALT 2 |
Definition at line 56 of file password.h.
Referenced by check_passwd(), command_USERID(), and x2crypt().
#define LEN_DOLLAR2_SALT 29 |
Definition at line 49 of file password.h.
Referenced by check_passwd(), and x2crypt().
#define LEN_DOLLAR5_SALT 20 |
Definition at line 50 of file password.h.
Referenced by check_passwd(), command_PASSWD(), command_USERID(), and x2crypt().
#define LEN_DOLLAR6_SALT 20 |
Definition at line 51 of file password.h.
Referenced by check_passwd(), command_PASSWD(), command_USERID(), and x2crypt().
#define LEN_DOLLAR_SALT 12 |
Definition at line 48 of file password.h.
Referenced by check_passwd(), command_PASSWD(), command_USERID(), and x2crypt().
#define LEN_MD5PASS 34 |
Definition at line 53 of file password.h.
#define LEN_MD5SALT 12 |
Definition at line 54 of file password.h.
int check_passwd | ( | char * | passwd, | |
char * | cryptpass | |||
) |
int check_passwd(char* passwd, char* cryptpass)
生パスワード passwd と暗号化されたパスワード cryptass(salt付き)が同じものであるかどうかチェックする.
使用できる暗号化(ハッシュ値化)は DES と $#$(自動判別)
passwd | 検査する生パスワード | |
cryptpass | 比較対象のハッシュ値化されたパスワード |
TRUE | 同じ | |
FALSE | 違う |
Definition at line 255 of file password.c.
References cut_str(), FALSE, LEN_DESPASS, LEN_DESSALT, LEN_DOLLAR2_SALT, LEN_DOLLAR5_SALT, LEN_DOLLAR6_SALT, LEN_DOLLAR_SALT, and TRUE.
00256 { 00257 int samepass=FALSE; 00258 char* salt; 00259 00260 if (passwd==NULL || cryptpass==NULL) return FALSE; 00261 00262 // if (!strncmp("$1$", cryptpass, 3) && strlen(cryptpass)==LEN_MD5PASS) { 00263 // if (cryptpass[0]=='$' && cryptpass[2]=='$' && cryptpass[11]=='$') { 00264 if (cryptpass[0]=='$') { 00265 int lsalt = 0; 00266 if (cryptpass[1]=='1') lsalt = LEN_DOLLAR_SALT; 00267 else if (cryptpass[1]=='2') lsalt = LEN_DOLLAR2_SALT; 00268 else if (cryptpass[1]=='5') lsalt = LEN_DOLLAR5_SALT; 00269 else if (cryptpass[1]=='6') lsalt = LEN_DOLLAR6_SALT; 00270 00271 if (lsalt!=0) { 00272 salt = cut_str(cryptpass, 0, lsalt-1); 00273 if (!strcmp(crypt(passwd, salt), cryptpass)) { 00274 samepass = TRUE; 00275 } 00276 free(salt); 00277 } 00278 } 00279 else if (strlen(cryptpass)==LEN_DESPASS) { 00280 // DES 00281 salt = cut_str(cryptpass, 0, LEN_DESSALT-1); 00282 if (!strcmp(crypt(passwd, salt), cryptpass)) { 00283 samepass = TRUE; 00284 } 00285 free(salt); 00286 } 00287 00288 return samepass; 00289 }
int check_salt | ( | char * | passwd | ) |
パスワード passwd のsaltをチェックする
DES -- 0 $#$ -- # saltn無し -- -1
passwd | チェックする文字列(パスワード) |
slat | の種類 | |
-1 | saltが無い | |
0 | DES | |
number | $#$........$ の # |
Definition at line 309 of file password.c.
References LEN_DESPASS.
00310 { 00311 int ret = -1; 00312 int len = strlen(passwd); 00313 00314 if (LEN_DESPASS<len) { 00315 if (passwd[0]=='$' && passwd[2]=='$' && passwd[11]=='$') { 00316 ret = (short int)passwd[1] - (short int)'1' + 1; 00317 } 00318 } 00319 else if (LEN_DESPASS==len) { 00320 ret = 0; 00321 } 00322 00323 return ret; 00324 }
void free_pw | ( | struct passwd * | pw | ) |
void free_pw(struct passwd* pw)
パスワード構造体 struct pw* を free する.getnisnam()の返す構造体にのみ適用すること.
一般のUNIXライブラリの返すパスワード構造体に対して適用してはいけない(セグメンテーションフォルトを起こす).
pw | 開放する struct pw型変数へのポインタ. |
Definition at line 172 of file password.c.
References freeNull.
Referenced by get_passwd().
00173 { 00174 if (pw==NULL) return; 00175 00176 freeNull(pw->pw_name); 00177 freeNull(pw->pw_passwd); 00178 freeNull(pw->pw_gecos); 00179 freeNull(pw->pw_dir); 00180 freeNull(pw->pw_shell); 00181 freeNull(pw); 00182 return; 00183 }
char* get_passwd | ( | char * | user_id | ) |
char* get_passwd(char* user_id)
ユーザ user_id のパスワードを得る.
パスワードの検索順は /etc/passwd, /etc/shadow, NIS
user_id | ユーザ名 |
NULL | 失敗 |
Definition at line 27 of file password.c.
References free_pw(), and LPASS.
Referenced by command_USERID().
00028 { 00029 struct passwd* pw; 00030 struct spwd* sp; 00031 char* pass; 00032 00033 pass = (char*)malloc(LPASS+1); 00034 if (pass==NULL) return NULL; 00035 memset(pass, 0, LPASS+1); 00036 00037 // for /etc/passwd 00038 pw = getpwnam((const char*)user_id); 00039 if (pw==NULL) { 00040 free(pass); 00041 return NULL; 00042 } 00043 strncpy(pass, pw->pw_passwd, LPASS); 00044 if (strcmp(pass, "*") && strcmp(pass, "x") && strcmp(pass, "!")) return pass; 00045 00046 #ifdef HAVE_GETSPNAM 00047 // for /etc/shadow 00048 sp = getspnam((const char*)user_id); 00049 if (sp!=NULL) { 00050 strncpy(pass, sp->sp_pwdp, LPASS); 00051 return pass; 00052 } 00053 #endif 00054 00055 #ifdef HAVE_RPCSVC_YPCLNT_H 00056 // for NIS 00057 pw = getnisnam(user_id); 00058 if (pw!=NULL) { 00059 strncpy(pass, pw->pw_passwd, LPASS); 00060 free_pw(pw); 00061 return pass; 00062 } 00063 #endif 00064 00065 return NULL; 00066 }
char* x2crypt | ( | char * | pass, | |
char * | bsalt | |||
) |
char* x2crypt(char* pass, char* bsalt)
pass を bsalt で2回暗号化(ハッシュ値化)する.
bsaltには改行コード(\nまたは \r\n)で区切られた2個のsaltが入っている必要がある.
pass | ハッシュ値化するパスワード | |
bsalt | 改行コード(\nまたは \r\n)で区切られた2個の salt. |
Definition at line 200 of file password.c.
References cut_str(), freeNull, get_line(), LEN_DESPASS, LEN_DESSALT, LEN_DOLLAR2_SALT, LEN_DOLLAR5_SALT, LEN_DOLLAR6_SALT, and LEN_DOLLAR_SALT.
Referenced by check_auth().
00201 { 00202 char* cpass; 00203 char* dpass = NULL; 00204 char* csalt; 00205 00206 if (pass==NULL || bsalt==NULL) return NULL; 00207 00208 csalt = get_line(bsalt, 1); 00209 cpass = crypt(pass, csalt); 00210 freeNull(csalt); 00211 csalt = get_line(bsalt, 2); 00212 if (csalt==NULL) return NULL; 00213 00214 if (cpass[0]=='$') { 00215 int lsalt = 0; 00216 if (cpass[1]=='1') lsalt = LEN_DOLLAR_SALT; 00217 else if (cpass[1]=='2') lsalt = LEN_DOLLAR2_SALT; 00218 else if (cpass[1]=='5') lsalt = LEN_DOLLAR5_SALT; 00219 else if (cpass[1]=='6') lsalt = LEN_DOLLAR6_SALT; 00220 00221 if (lsalt!=0) { 00222 int passlen = strlen(cpass); 00223 dpass = cut_str(cpass, lsalt, passlen-1); 00224 cpass = crypt(dpass, csalt); 00225 freeNull(dpass); 00226 dpass = cut_str(cpass, lsalt, passlen-1); 00227 } 00228 } 00229 else if (strlen(cpass)==LEN_DESPASS) { 00230 dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1); 00231 freeNull(cpass); 00232 cpass = crypt(dpass, csalt); 00233 freeNull(dpass); 00234 dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1); 00235 } 00236 00237 freeNull(csalt); 00238 return dpass; 00239 }