JunkBox_Lib  1.10.2
password.h File Reference

パスワードライブラリ ヘッダ More...

#include "tools.h"
#include <pwd.h>
Include dependency graph for password.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#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)
 

Detailed Description

Author
Fumi.Iseki (C)

Definition in file password.h.

Macro Definition Documentation

◆ LEN_DESPASS

#define LEN_DESPASS   13

Definition at line 51 of file password.h.

◆ LEN_DESSALT

#define LEN_DESSALT   2

Definition at line 52 of file password.h.

◆ LEN_DOLLAR2_SALT

#define LEN_DOLLAR2_SALT   29

Definition at line 45 of file password.h.

◆ LEN_DOLLAR5_SALT

#define LEN_DOLLAR5_SALT   20

Definition at line 46 of file password.h.

◆ LEN_DOLLAR6_SALT

#define LEN_DOLLAR6_SALT   20

Definition at line 47 of file password.h.

◆ LEN_DOLLAR_SALT

#define LEN_DOLLAR_SALT   12

Definition at line 44 of file password.h.

◆ LEN_MD5PASS

#define LEN_MD5PASS   34

Definition at line 49 of file password.h.

◆ LEN_MD5SALT

#define LEN_MD5SALT   12

Definition at line 50 of file password.h.

Function Documentation

◆ check_passwd()

int check_passwd ( char *  passwd,
char *  cryptpass 
)

int check_passwd(char* passwd, char* cryptpass)

生パスワード passwd と暗号化されたパスワード cryptass(salt付き)が同じものであるかどうかチェックする.
使用できる暗号化(ハッシュ値化)は DES と $#$(自動判別)

Parameters
passwd検査する生パスワード
cryptpass比較対象のハッシュ値化されたパスワード
Return values
TRUE同じ
FALSE違う

Definition at line 248 of file password.c.

249 {
250  int samepass=FALSE;
251  char* salt;
252 
253  if (passwd==NULL || cryptpass==NULL) return FALSE;
254 
255 // if (!strncmp("$1$", cryptpass, 3) && strlen(cryptpass)==LEN_MD5PASS) {
256 // if (cryptpass[0]=='$' && cryptpass[2]=='$' && cryptpass[11]=='$') {
257  if (cryptpass[0]=='$') {
258  int lsalt = 0;
259  if (cryptpass[1]=='1') lsalt = LEN_DOLLAR_SALT;
260  else if (cryptpass[1]=='2') lsalt = LEN_DOLLAR2_SALT;
261  else if (cryptpass[1]=='5') lsalt = LEN_DOLLAR5_SALT;
262  else if (cryptpass[1]=='6') lsalt = LEN_DOLLAR6_SALT;
263 
264  if (lsalt!=0) {
265  salt = cut_str(cryptpass, 0, lsalt-1);
266  if (!strcmp(crypt(passwd, salt), cryptpass)) {
267  samepass = TRUE;
268  }
269  free(salt);
270  }
271  }
272  else if (strlen(cryptpass)==LEN_DESPASS) {
273  // DES
274  salt = cut_str(cryptpass, 0, LEN_DESSALT-1);
275  if (!strcmp(crypt(passwd, salt), cryptpass)) {
276  samepass = TRUE;
277  }
278  free(salt);
279  }
280 
281  return samepass;
282 }
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223
#define LEN_DOLLAR5_SALT
Definition: password.h:46
#define LEN_DOLLAR2_SALT
Definition: password.h:45
#define LEN_DOLLAR_SALT
Definition: password.h:44
#define LEN_DESPASS
Definition: password.h:51
#define LEN_DOLLAR6_SALT
Definition: password.h:47
#define LEN_DESSALT
Definition: password.h:52
char * cut_str(char *buf, int ls, int le)
buf[ls]〜buf[le] を切り出す.要 free()
Definition: tools.c:1338

References cut_str(), FALSE, LEN_DESPASS, LEN_DESSALT, LEN_DOLLAR2_SALT, LEN_DOLLAR5_SALT, LEN_DOLLAR6_SALT, LEN_DOLLAR_SALT, and TRUE.

Here is the call graph for this function:

◆ check_salt()

int check_salt ( char *  passwd)

int check_salt(char* passwd)

パスワード passwd のsaltをチェックする

DES – 0 $#$ – # saltn無し – -1

Parameters
passwdチェックする文字列(パスワード)
Return values
slatの種類
-1saltが無い
0DES
number$#$........$ の #

Definition at line 301 of file password.c.

302 {
303  int ret = -1;
304  int len = strlen(passwd);
305 
306  if (LEN_DESPASS<len) {
307  if (passwd[0]=='$' && passwd[2]=='$' && passwd[11]=='$') {
308  ret = (short int)passwd[1] - (short int)'1' + 1;
309  }
310  }
311  else if (LEN_DESPASS==len) {
312  ret = 0;
313  }
314 
315  return ret;
316 }
unsigned char unsigned long * len
Definition: jpeg_tool.h:96

References len, and LEN_DESPASS.

◆ free_pw()

void free_pw ( struct passwd *  pw)

void free_pw(struct passwd* pw)

パスワード構造体 struct pw* を free する.getnisnam()の返す構造体にのみ適用すること.
一般のUNIXライブラリの返すパスワード構造体に対して適用してはいけない(セグメンテーションフォルトを起こす).

Parameters
pw開放する struct pw型変数へのポインタ.
See also
struct pw

Definition at line 167 of file password.c.

168 {
169  if (pw==NULL) return;
170 
171  freeNull(pw->pw_name);
172  freeNull(pw->pw_passwd);
173  freeNull(pw->pw_gecos);
174  freeNull(pw->pw_dir);
175  freeNull(pw->pw_shell);
176  freeNull(pw);
177  return;
178 }
#define freeNull(p)
Definition: tools.h:201

References freeNull.

Referenced by get_passwd().

Here is the caller graph for this function:

◆ get_passwd()

char* get_passwd ( char *  user_id)

char* get_passwd(char* user_id)

ユーザ user_id のパスワードを得る.
パスワードの検索順は /etc/passwd, /etc/shadow, NIS

Parameters
user_idユーザ名
Returns
ユーザの暗号化(ハッシュ値化)されたパスワード
Return values
NULL失敗

Definition at line 25 of file password.c.

26 {
27  struct passwd* pw;
28  struct spwd* sp;
29  char* pass;
30 
31  pass = (char*)malloc(LPASS+1);
32  if (pass==NULL) return NULL;
33  memset(pass, 0, LPASS+1);
34 
35  // for /etc/passwd
36  pw = getpwnam((const char*)user_id);
37  if (pw==NULL) {
38  free(pass);
39  return NULL;
40  }
41  strncpy(pass, pw->pw_passwd, LPASS);
42  if (strcmp(pass, "*") && strcmp(pass, "x") && strcmp(pass, "!")) return pass;
43 
44 #ifdef HAVE_GETSPNAM
45  // for /etc/shadow
46  sp = getspnam((const char*)user_id);
47  if (sp!=NULL) {
48  strncpy(pass, sp->sp_pwdp, LPASS);
49  return pass;
50  }
51 #endif
52 
53 #ifdef HAVE_RPCSVC_YPCLNT_H
54  // for NIS
55  pw = getnisnam(user_id);
56  if (pw!=NULL) {
57  strncpy(pass, pw->pw_passwd, LPASS);
58  free_pw(pw);
59  return pass;
60  }
61 #endif
62 
63  return pass; // *, x, !
64 }
#define LPASS
$6$ -> 98文字
Definition: common.h:155
void free_pw(struct passwd *pw)
Definition: password.c:167

References free_pw(), and LPASS.

Referenced by command_USERID().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ x2crypt()

char* x2crypt ( char *  pass,
char *  bsalt 
)

char* x2crypt(char* pass, char* bsalt)

pass を bsalt で2回暗号化(ハッシュ値化)する.
bsaltには改行コード(\nまたは \r\n)で区切られた2個のsaltが入っている必要がある.

Parameters
passハッシュ値化するパスワード
bsalt改行コード(\nまたは \r\n)で区切られた2個の salt.
Returns
2度ハッシュ値化された文字列.要 free
Bug:
bsaltに 2個のslatが入っていなかったときの動作は? -> 不明

Definition at line 194 of file password.c.

195 {
196  char* cpass;
197  char* dpass = NULL;
198  char* csalt;
199 
200  if (pass==NULL || bsalt==NULL) return NULL;
201 
202  csalt = get_line(bsalt, 1);
203  cpass = crypt(pass, csalt);
204  freeNull(csalt);
205  csalt = get_line(bsalt, 2);
206  if (csalt==NULL) return NULL;
207 
208  if (cpass[0]=='$') {
209  int lsalt = 0;
210  if (cpass[1]=='1') lsalt = LEN_DOLLAR_SALT;
211  else if (cpass[1]=='2') lsalt = LEN_DOLLAR2_SALT;
212  else if (cpass[1]=='5') lsalt = LEN_DOLLAR5_SALT;
213  else if (cpass[1]=='6') lsalt = LEN_DOLLAR6_SALT;
214 
215  if (lsalt!=0) {
216  int passlen = strlen(cpass);
217  dpass = cut_str(cpass, lsalt, passlen-1);
218  cpass = crypt(dpass, csalt);
219  freeNull(dpass);
220  dpass = cut_str(cpass, lsalt, passlen-1);
221  }
222  }
223  else if (strlen(cpass)==LEN_DESPASS) {
224  dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1);
225  freeNull(cpass);
226  cpass = crypt(dpass, csalt);
227  freeNull(dpass);
228  dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1);
229  }
230 
231  freeNull(csalt);
232  return dpass;
233 }
char * get_line(char *buf, int n)
文字型データbufのバッファ内の n行目を取り出す.改行コードは削除される.要 free()
Definition: tools.c:484

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().

Here is the call graph for this function:
Here is the caller graph for this function: