JunkBox_Lib 1.10.1
Loading...
Searching...
No Matches
isnet_server.c
Go to the documentation of this file.
1
4
6EVP_CIPHER* CRYPT_Type = NULL;
7DH* DHkey = NULL;
8
9Buffer* User_ID = NULL;
12
13int No_isNet_Chlng = FALSE; // チャレンジキーを使用する (サーバ側の状態を表す)
16
17
18#ifdef ENABLE_LDAP
19JBXL_LDAP_Host* LdapHost = NULL;
20JBXL_LDAP_Dn* LdapBind = NULL;
21#endif
22
23
25// サーバ側でのコマンド受信処理
26//
27
45int command_USERID(Buffer operand, Buffer comment, int sock)
46{
47 char* pass = NULL;
48 char* salt0 = NULL;
49 char* salt1 = NULL;
50 char* salt2 = NULL;
51 Buffer buf;
52
53 UNUSED(comment);
54
55 pass = get_passwd((char*)operand.buf);
56 if (pass==NULL) {
57 tcp_send_crypt_mesg(sock, (char*)"ERR 121 passwd get error.\r\n", CRYPT_SharedKey, CRYPT_Type);
58 DEBUG_MODE PRINT_MESG("ERR 121 passwd get error.\n");
59 return 121;
60 }
61
63 tcp_send_crypt_mesg(sock, (char*)"ERR 122 both No Challenge Key mode and No Crypt mode are not used.\r\n", CRYPT_SharedKey, CRYPT_Type);
64 DEBUG_MODE PRINT_MESG("ERR 122 both No Challenge Key mode and No Crypt mode are not used.\n");
65 return 122;
66 }
67
69 tcp_send_crypt_mesg(sock, (char*)"ERR 123 both Challenge Key mode and Ldap mode are not used.\r\n", CRYPT_SharedKey, CRYPT_Type);
70 DEBUG_MODE PRINT_MESG("ERR 123 both Challenge Key mode and Ldap mode are not used.\n");
71 return 123;
72 }
73
74 if (User_ID!=NULL) del_Buffer(&User_ID);
76 if (User_Salt!=NULL) del_Buffer(&User_Salt);
80 *User_ID = dup_Buffer(operand);
82
83 //
84 if (strlen(pass)<=2) {
85#ifdef ENABLE_LDAP
86 int cc;
87 if (!strcmp((const char*)User_Passwd->buf, "*") && Use_isNet_Ldap) {
92 //
93 if (ld!=NULL) {
94 cc = simple_check_ldap_passwd(ld, (char*)User_ID->buf, NULL, LdapBind);
96 if (cc==JBXL_LDAP_PASSWD_ERROR) {
97 cc = tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
98 return 0;
99 }
100 else {
101 cc = tcp_send_crypt_mesg(sock, (char*)"ERR 128 USERID error with LDAP.\r\n", CRYPT_SharedKey, CRYPT_Type);
102 DEBUG_MODE PRINT_MESG("ERR 128 USERID error with LDAP.\n");
103 return 128;
104 }
105 }
106 else {
107 cc = tcp_send_crypt_mesg(sock, (char*)"ERR 127 USERID connection error with LDAP.\r\n", CRYPT_SharedKey, CRYPT_Type);
108 DEBUG_MODE PRINT_MESG("ERR 127 USERID connection error with LDAP.\n");
109 return 127;
110 }
111 }
112#endif
113 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
114 return 0;
115 }
116
117 //
118 if (pass[0]=='$') {
119 int lsalt = 0;
120 if (pass[1]=='1') lsalt = LEN_DOLLAR_SALT;
121 //else if (pass[1]=='2') lsalt = LEN_DOLLAR2_SALT; // Blowfish 未対応
122 else if (pass[1]=='5') lsalt = LEN_DOLLAR5_SALT;
123 else if (pass[1]=='6') lsalt = LEN_DOLLAR6_SALT;
124
125 if (lsalt!=0) {
126 *User_Salt = make_Buffer(lsalt*2 + 5); // \r\n \r\n \0 で +5文字
127 salt0 = cut_str(pass, 0, 2);
128 salt1 = cut_str(pass, 0, lsalt-1);
129 salt2 = randstr(lsalt-4);
130 copy_s2Buffer(salt1, User_Salt);
131 cat_s2Buffer("\r\n", User_Salt);
132 cat_s2Buffer(salt0, User_Salt);
133 cat_s2Buffer(salt2, User_Salt);
135 }
136 }
137 // DES
138 else if (strlen((const char*)pass)==LEN_DESPASS) {
140 salt1 = cut_str(pass, 0, LEN_DESSALT-1);
141 salt2 = randstr(LEN_DESSALT);
142 copy_s2Buffer(salt1, User_Salt);
143 cat_s2Buffer ("\r\n", User_Salt);
144 cat_s2Buffer (salt2, User_Salt);
145 }
146
147 freeNull(pass);
148 freeNull(salt0);
149 freeNull(salt1);
150 freeNull(salt2);
151
152 if (User_Salt->buf!=NULL) {
153 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
154 if (No_isNet_Chlng==FALSE) {
157 cat_s2Buffer("\r\nEND\r\n", &buf);
160 }
161 }
162 else {
163 tcp_send_crypt_mesg(sock, (char*)"ERR 129 USERID error.\r\n", CRYPT_SharedKey, CRYPT_Type);
164 DEBUG_MODE PRINT_MESG("ERR 129 USERID error.\n");
165 return 129;
166 }
167
168 return 0;
169}
170
171
187int command_PASSWD(Buffer operand, Buffer comment, int sock)
188{
189 char* opass = NULL; // Password that from system
190 char* npass = NULL; // Password that crypted opass
191 char* salt = NULL;
192
193 UNUSED(comment);
194
195 if (User_Passwd==NULL) {
196 tcp_send_crypt_mesg(sock, (char*)"ERR 131 system has not a your password.\r\n", CRYPT_SharedKey, CRYPT_Type);
197 DEBUG_MODE PRINT_MESG("ERR 131 system has not a your password.\n");
198 return 131;
199 }
200
201#ifdef ENABLE_LDAP
202 int cc;
203 if (!strcmp((const char*)User_Passwd->buf, "*") && Use_isNet_Ldap) {
208 cc = simple_check_ldap_passwd(ld, (char*)User_ID->buf, (char*)operand.buf, LdapBind);
210 //
211 if (cc==0) {
212 cc = tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
213 return 0;
214 }
215 else {
216 cc = tcp_send_crypt_mesg(sock, (char*)"ERR 138 PASSWD error.\r\n", CRYPT_SharedKey, CRYPT_Type);
217 DEBUG_MODE PRINT_MESG("ERR 138 PASSWD error.\n");
218 return 138;
219 }
220 }
221#endif
222
223 int lsalt = 0;
224 if (User_Passwd->buf[0]=='$') {
225 if (User_Passwd->buf[1]=='1') lsalt = LEN_DOLLAR_SALT;
226 //else if (User_Passwd->buf[1]=='2') lsalt = LEN_DOLLAR2_SALT;
227 else if (User_Passwd->buf[1]=='5') lsalt = LEN_DOLLAR5_SALT;
228 else if (User_Passwd->buf[1]=='6') lsalt = LEN_DOLLAR6_SALT;
229
230 opass = (char*)&(User_Passwd->buf[lsalt]);
231 }
232 else if (strlen((const char*)User_Passwd->buf)==LEN_DESPASS) {
233 lsalt = 2;
234 opass = (char*)&(User_Passwd->buf[lsalt]);
235 }
236
237 //
238 if (No_isNet_Chlng==FALSE) { // Use Challenge Keys
239 salt = get_line((char*)User_Salt->buf, 2);
240 if (salt!=NULL && opass!=NULL) {
241 npass = (char*)crypt((const char*)opass, (const char*)salt);
242 npass = &(npass[lsalt]);
243 }
244 if (npass!=NULL) {
245 if (!strcmp((const char*)operand.buf, (const char*)npass)) {
246 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
247 return 0;
248 }
249 }
250 }
251 else { // Not Use Challenge Keys
252 salt = get_line((char*)User_Salt->buf, 1);
253 if (salt!=NULL && operand.buf!=NULL) {
254 npass = (char*)crypt((const char*)operand.buf, (const char*)salt);
255 npass = &(npass[lsalt]);
256 free(salt);
257 }
258 if (npass!=NULL && opass!=NULL) {
259 if (!strcmp((const char*)opass, (const char*)npass)) {
260 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
261 return 0;
262 }
263 }
264 }
265
266 tcp_send_crypt_mesg(sock, (char*)"ERR 139 PASSWD error.\r\n", CRYPT_SharedKey, CRYPT_Type);
267 DEBUG_MODE PRINT_MESG("ERR 139 PASSWD error.\n");
268 return 139;
269}
270
271
287int command_HELLO(Buffer operand, Buffer comment, int sock)
288{
289 int cc;
290
291 UNUSED(operand);
292 UNUSED(comment);
293 //
294 cc = tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
295 return cc;
296}
297
298
314int command_BYE(Buffer operand, Buffer comment, int sock)
315{
316 int cc;
317
318 UNUSED(operand);
319 UNUSED(comment);
320 //
321 cc = tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
322 //clear_CRYPT_parameter();
323 return cc;
324}
325
326
360int command_KEYEX(Buffer operand, Buffer comment, int sock)
361{
362 static int keyex;
363 Buffer buf;
364
365 UNUSED(comment);
366
367 if (!strcmp("DH", (const char*)operand.buf)) {
368 if (Base64_DHspki!=NULL) {
370 keyex = SSL_DH;
371 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
372
375 cat_s2Buffer("\r\nEND\r\n", &buf);
378 }
379 else {
380 tcp_send_crypt_mesg(sock, (char*)"ERR 161 KEYEX DH Error.\r\n", CRYPT_SharedKey, CRYPT_Type);
381 DEBUG_MODE PRINT_MESG("ERR 161 KEYEX DH Error.\n");
382 return 161;
383 }
384 }
385
386 else if (!strcmp("RSA", (const char*)operand.buf)) {
387 if (Base64_RSAspki!=NULL) {
389 keyex = SSL_RSA;
390
391 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
394 cat_s2Buffer("\r\nEND\r\n", &buf);
397 }
398 else {
399 tcp_send_crypt_mesg(sock, (char*)"ERR 162 KEYEX RSA Error.\r\n", CRYPT_SharedKey, CRYPT_Type);
400 DEBUG_MODE PRINT_MESG("ERR 162 KEYEX RSA Error.\n");
401 return 162;
402 }
403 }
404
405 else if (!strcmp("SPKI", (const char*)operand.buf)) {
406 Buffer dkey;
407
408 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
410 tcp_recv_Buffer_wait(sock, &buf, 20);
413 chomp_Buffer(&dkey);
415 free_Buffer(&dkey);
416
419
420 if (gen_CRYPT_SharedKey(keyex, buf, CRYPT_SharedKey, (void*)DHkey)) {
421 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
422 }
423 else {
424 tcp_send_crypt_mesg(sock, (char*)"ERR 163 cannot generate Shared KEY.\r\n", CRYPT_SharedKey, CRYPT_Type);
425 DEBUG_MODE PRINT_MESG("ERR 163 cannot generate Shared KEY.\n");
426 return 163;
427 }
429 }
430
431 else if (!strcmp("CHLNG", (const char*)operand.buf)) {
432 if (!Use_isNet_Ldap) {
434 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
435 }
436 else {
437 tcp_send_crypt_mesg(sock, (char*)"ERR 164 cannot use Challenge Key mode with LDAP mode.\r\n", CRYPT_SharedKey, CRYPT_Type);
438 DEBUG_MODE PRINT_MESG("ERR 164 cannot use Challenge Key mode with LDAP mode.\n");
439 return 164;
440 }
441 }
442
443 else if (!strcmp("NOCHLNG", (const char*)operand.buf)) {
444 if (Use_isNet_Crypt==TRUE) {
446 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
447 }
448 else {
449 tcp_send_crypt_mesg(sock, (char*)"ERR 165 both No Challenge Key mode and No Crypt mode are not used.\r\n", CRYPT_SharedKey, CRYPT_Type);
450 DEBUG_MODE PRINT_MESG("ERR 165 both No Challenge Key mode and No Crypt mode are not used.\n");
451 return 165;
452 }
453 }
454
455 else {
456 tcp_send_crypt_mesg(sock, (char*)"ERR 169 Unkown KEYEX operand.\n\r", CRYPT_SharedKey, CRYPT_Type);
457 DEBUG_MODE PRINT_MESG("ERR 169 Unkown KEYEX operand.\r");
458 return 169;
459 }
460
461 return 0;
462}
463
464
482int command_CRYPT(Buffer operand, Buffer comment, int sock)
483{
484 UNUSED(comment);
485
486 if (CRYPT_SharedKey==NULL) {
487 tcp_send_crypt_mesg(sock, (char*)"ERR 171 No Shared Key.\r\n", NULL, NULL);
488 DEBUG_MODE PRINT_MESG("ERR 171 No Shared Key.\n");
489 return 171;
490 }
491
492 if (!strcmp("AES128CBC", (const char*)operand.buf)) { // AES 128bit CBC
493 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
494 free(CRYPT_Type);
495 CRYPT_Type = init_EVPAPI_Buffer(SSL_AES128CBC);
496 }
497 else if (!strcmp("3DES3CBC", (const char*)operand.buf)) { // 3DES 3key CBC
498 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
499 CRYPT_Type = init_EVPAPI_Buffer(SSL_3DES3CBC);
500 }
501 else if (!strcmp("RESET", (const char*)operand.buf)) { // RESET: 暗号化中止
502 tcp_send_crypt_mesg(sock, (char*)"OK\r\n", CRYPT_SharedKey, CRYPT_Type);
503 //free_EVPAPI_Buffer();
504 }
505 else {
506 tcp_send_crypt_mesg(sock, (char*)"ERR 179 Unkown CRYPT operand.\r\n", CRYPT_SharedKey, CRYPT_Type);
507 DEBUG_MODE PRINT_MESG("ERR 179 Unkown CRYPT operand.\n");
508 return 179;
509 }
510
511 return 0;
512}
513
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.c:71
void chomp_Buffer(Buffer *str)
最初の改行コード以降を無視する.
Definition buffer.c:1347
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.c:128
Buffer dup_Buffer(Buffer buf)
Buffer型変数のコピーをつくる.
Definition buffer.c:211
Buffer decode_base64_Buffer(Buffer str)
strのバッファを Base64からデコードする
Definition buffer.c:850
void del_Buffer(Buffer **buf)
new_Buffer()で作り出した Buffer型変数を削除する.
Definition buffer.c:148
Buffer * new_Buffer(int sz)
空のBuffer型変数を生成する.
Definition buffer.c:23
int copy_Buffer(Buffer *src, Buffer *dst)
Buffer型変数 srcから dstへバッファをコピーする.
Definition buffer.c:315
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
Definition buffer.h:108
#define cat_s2Buffer(src, dst)
cat_b2Buffer()
Definition buffer.h:122
#define make_Buffer_bystr(str)
set_Buffer()
Definition buffer.h:57
#define UNUSED(x)
Definition common.h:264
#define LBUF
Definition common.h:146
#define TRUE
Definition common.h:226
#define FALSE
Definition common.h:223
Buffer * Base64_DHspki
Definition isnet_server.c:2
int Use_isNet_Ldap
Buffer * User_ID
Definition isnet_server.c:9
int command_USERID(Buffer operand, Buffer comment, int sock)
EVP_CIPHER * CRYPT_Type
Definition isnet_server.c:6
DH * DHkey
Definition isnet_server.c:7
int No_isNet_Chlng
int Use_isNet_Crypt
Buffer * CRYPT_SharedKey
Definition isnet_server.c:5
int command_HELLO(Buffer operand, Buffer comment, int sock)
int command_KEYEX(Buffer operand, Buffer comment, int sock)
int command_BYE(Buffer operand, Buffer comment, int sock)
Buffer * Base64_RSAspki
Definition isnet_server.c:3
JBXL_LDAP_Host * LdapHost
int command_PASSWD(Buffer operand, Buffer comment, int sock)
JBXL_LDAP_Dn * LdapBind
Buffer * User_Passwd
int command_CRYPT(Buffer operand, Buffer comment, int sock)
Buffer * User_Salt
#define JBXL_LDAP_PASSWD_ERROR
ユーザ認証失敗(ユーザは存在するが,パスワードが一致しない)
Definition jbxl_state.h:138
unsigned char ** buf
Definition jpeg_tool.h:96
void close_ldap_connection(LDAP *ld, JBXL_LDAP_Host **p_ldap_host, JBXL_LDAP_Dn **p_ldap_bind)
Definition ldap_tool.c:367
LDAP * open_ldap_connection(JBXL_LDAP_Host *ldap_host, JBXL_LDAP_Dn *ldap_bind)
Definition ldap_tool.c:129
JBXL_LDAP_Host * new_LDAP_Host(void)
Definition ldap_tool.c:418
JBXL_LDAP_Dn * new_LDAP_Dn(void)
Definition ldap_tool.c:428
int simple_check_ldap_passwd(LDAP *ld, char *userid, char *passwd, JBXL_LDAP_Dn *ldap_bind)
Definition ldap_tool.c:230
void read_ldap_config(char *fn, JBXL_LDAP_Host *ldap_host, JBXL_LDAP_Dn *ldap_bind)
Definition ldap_tool.c:37
char * get_passwd(char *user_id)
Definition password.c:25
#define LEN_DOLLAR5_SALT
Definition password.h:46
#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
int tcp_send_crypt_sBuffer(int sock, Buffer *mesg, Buffer *key, EVP_CIPHER *cipher)
Definition ssl_tool.c:351
int gen_CRYPT_SharedKey(int keyex, Buffer spki, Buffer *shdkey, void *ptr)
Definition ssl_tool.c:91
int tcp_send_crypt_mesg(int sock, char *mesg, Buffer *key, EVP_CIPHER *cipher)
Definition ssl_tool.c:298
EVP_CIPHER * init_EVPAPI_Buffer(int type)
Definition ssl_tool.c:645
Buffer get_plain_sBuffer(Buffer mesg, Buffer *key, EVP_CIPHER *cipher)
Definition ssl_tool.c:423
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
char * get_line(char *buf, int n)
文字型データbufのバッファ内の n行目を取り出す.改行コードは削除される.要 free()
Definition tools.c:484
char * randstr(int n)
ランダムに A-Za-z0-9 までの文字を n文字生成する.
Definition tools.c:1777
char * cut_str(char *buf, int ls, int le)
buf[ls]〜buf[le] を切り出す.要 free()
Definition tools.c:1338
#define freeNull(p)
Definition tools.h:201
#define PRINT_MESG
環境依存用の出力関数.print_message()
Definition tools.h:475
#define DEBUG_MODE
Definition tools.h:502
int tcp_recv_Buffer_wait(int sock, Buffer *str, int tm)
TCP経由でデータを受信する.待ち時間(タイムアウト)を指定できる.
Definition xtools.c:370