1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*-
* Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
| $Id: login.c,v 1.4 2007/04/27 07:40:40 danny Exp danny $
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#if __FreeBSD_version < 500000
#include <sys/time.h>
#endif
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iscsi.h"
#include "iscontrol.h"
static char *status_class1[] = {
"Initiator error",
"Authentication failure",
"Authorization failure",
"Not found",
"Target removed",
"Unsupported version",
"Too many connections",
"Missing parameter",
"Can't include in session",
"Session type not suported",
"Session does not exist",
"Invalid during login",
};
#define CLASS1_ERRS ((sizeof status_class1) / sizeof(char *))
static char *status_class3[] = {
"Target error",
"Service unavailable",
"Out of resources"
};
#define CLASS3_ERRS ((sizeof status_class3) / sizeof(char *))
static char *
selectFrom(char *str, token_t *list)
{
char *sep, *sp;
token_t *lp;
int n;
sp = str;
do {
sep = strchr(sp, ',');
if(sep != NULL)
n = sep - sp;
else
n = strlen(sp);
for(lp = list; lp->name != NULL; lp++) {
if(strncasecmp(lp->name, sp, n) == 0)
return strdup(lp->name);
}
sp = sep + 1;
} while(sep != NULL);
return NULL;
}
static char *
getkeyval(char *key, pdu_t *pp)
{
char *ptr;
int klen, len, n;
debug_called(3);
len = pp->ds_len;
ptr = (char *)pp->ds;
klen = strlen(key);
while(len > klen) {
if(strncmp(key, ptr, klen) == 0)
return ptr+klen;
n = strlen(ptr) + 1;
len -= n;
ptr += n;
}
return 0;
}
static int
handleTgtResp(isess_t *sess, pdu_t *pp)
{
isc_opt_t *op = sess->op;
char *np, *rp, *d1, *d2;
int res, l1, l2;
res = -1;
if(((np = getkeyval("CHAP_N=", pp)) == NULL) ||
((rp = getkeyval("CHAP_R=", pp)) == NULL))
goto out;
if(strcmp(np, op->tgtChapName? op->tgtChapName: op->initiatorName) != 0) {
fprintf(stderr, "%s does not match\n", np);
goto out;
}
l1 = str2bin(op->tgtChapDigest, &d1);
l2 = str2bin(rp, &d2);
debug(3, "l1=%d '%s' l2=%d '%s'", l1, op->tgtChapDigest, l2, rp);
if(l1 == l2 && memcmp(d1, d2, l1) == 0)
res = 0;
if(l1)
free(d1);
if(l2)
free(d2);
out:
free(op->tgtChapDigest);
op->tgtChapDigest = NULL;
debug(3, "res=%d", res);
return res;
}
static void
processParams(isess_t *sess, pdu_t *pp)
{
isc_opt_t *op = sess->op;
int len, klen, n;
char *eq, *ptr;
debug_called(3);
len = pp->ds_len;
ptr = (char *)pp->ds;
while(len > 0) {
if(vflag > 1)
printf("got: len=%d %s\n", len, ptr);
klen = 0;
if((eq = strchr(ptr, '=')) != NULL)
klen = eq - ptr;
if(klen > 0) {
if(strncmp(ptr, "TargetAddress", klen) == 0) {
char *p, *q, *ta = NULL;
// TargetAddress=domainname[:port][,portal-group-tag]
// XXX: if(op->targetAddress) free(op->targetAddress);
q = op->targetAddress = strdup(eq+1);
if(*q == '[') {
// bracketed IPv6
if((q = strchr(q, ']')) != NULL) {
*q++ = '\0';
ta = op->targetAddress;
op->targetAddress = strdup(ta+1);
} else
q = op->targetAddress;
}
if((p = strchr(q, ',')) != NULL) {
*p++ = 0;
op->targetPortalGroupTag = atoi(p);
}
if((p = strchr(q, ':')) != NULL) {
*p++ = 0;
op->port = atoi(p);
}
if(ta)
free(ta);
} else if(strncmp(ptr, "MaxRecvDataSegmentLength", klen) == 0) {
// danny's RFC
op->maxXmitDataSegmentLength = strtol(eq+1, (char **)NULL, 0);
} else if(strncmp(ptr, "TargetPortalGroupTag", klen) == 0) {
op->targetPortalGroupTag = strtol(eq+1, (char **)NULL, 0);
} else if(strncmp(ptr, "HeaderDigest", klen) == 0) {
op->headerDigest = selectFrom(eq+1, DigestMethods);
} else if(strncmp(ptr, "DataDigest", klen) == 0) {
op->dataDigest = selectFrom(eq+1, DigestMethods);
} else if(strncmp(ptr, "MaxOutstandingR2T", klen) == 0)
op->maxOutstandingR2T = strtol(eq+1, (char **)NULL, 0);
#if 0
else
for(kp = keyMap; kp->name; kp++) {
if(strncmp(ptr, kp->name, kp->len) == 0 && ptr[kp->len] == '=')
mp->func(sess, ptr+kp->len+1, GET);
}
#endif
}
n = strlen(ptr) + 1;
len -= n;
ptr += n;
}
}
static int
handleLoginResp(isess_t *sess, pdu_t *pp)
{
login_rsp_t *lp = (login_rsp_t *)pp;
uint st_class, status = ntohs(lp->status);
debug_called(3);
debug(4, "Tbit=%d csg=%d nsg=%d status=%x", lp->T, lp->CSG, lp->NSG, status);
st_class = status >> 8;
if(status) {
int st_detail = status & 0xff;
switch(st_class) {
case 1: // Redirect
switch(st_detail) {
// the ITN (iSCSI target Name) requests a:
case 1: // temporary address change
case 2: // permanent address change
status = 0;
}
break;
case 2: // Initiator Error
if(st_detail < CLASS1_ERRS)
printf("0x%04x: %s\n", status, status_class1[st_detail]);
break;
case 3:
if(st_detail < CLASS3_ERRS)
printf("0x%04x: %s\n", status, status_class3[st_detail]);
break;
}
}
if(status == 0) {
processParams(sess, pp);
setOptions(sess, 0); // XXX: just in case ...
if(lp->T) {
isc_opt_t *op = sess->op;
if(sess->csg == SN_PHASE && (op->tgtChapDigest != NULL))
if(handleTgtResp(sess, pp) != 0)
return 1; // XXX: Authentication failure ...
sess->csg = lp->NSG;
if(sess->csg == FF_PHASE) {
// XXX: will need this when implementing reconnect.
sess->tsih = lp->tsih;
debug(2, "TSIH=%x", sess->tsih);
}
}
}
return st_class;
}
static int
handleChap(isess_t *sess, pdu_t *pp)
{
pdu_t spp;
login_req_t *lp;
isc_opt_t *op = sess->op;
char *ap, *ip, *cp, *digest; // MD5 is 128bits, SHA1 160bits
debug_called(3);
bzero(&spp, sizeof(pdu_t));
lp = (login_req_t *)&spp.ipdu.bhs;
lp->cmd = ISCSI_LOGIN_CMD | 0x40; // login request + Inmediate
memcpy(lp->isid, sess->isid, 6);
lp->tsih = sess->tsih; // MUST be zero the first time!
lp->CID = htons(1);
lp->CSG = SN_PHASE; // Security Negotiation
lp->NSG = LON_PHASE;
lp->T = 1;
if(((ap = getkeyval("CHAP_A=", pp)) == NULL) ||
((ip = getkeyval("CHAP_I=", pp)) == NULL) ||
((cp = getkeyval("CHAP_C=", pp)) == NULL))
return -1;
if((digest = chapDigest(ap, (char)strtol(ip, (char **)NULL, 0), cp, op->chapSecret)) == NULL)
return -1;
addText(&spp, "CHAP_N=%s", op->chapIName? op->chapIName: op->initiatorName);
addText(&spp, "CHAP_R=%s", digest);
free(digest);
if(op->tgtChapSecret != NULL) {
op->tgtChapID = (random() >> 24) % 255; // should be random enough ...
addText(&spp, "CHAP_I=%d", op->tgtChapID);
cp = genChapChallenge(cp, op->tgtChallengeLen? op->tgtChallengeLen: 8);
addText(&spp, "CHAP_C=%s", cp);
op->tgtChapDigest = chapDigest(ap, op->tgtChapID, cp, op->tgtChapSecret);
}
return sendPDU(sess, &spp, handleLoginResp);
}
static int
authenticate(isess_t *sess)
{
pdu_t spp;
login_req_t *lp;
isc_opt_t *op = sess->op;
bzero(&spp, sizeof(pdu_t));
lp = (login_req_t *)&spp.ipdu.bhs;
lp->cmd = ISCSI_LOGIN_CMD | 0x40; // login request + Inmediate
memcpy(lp->isid, sess->isid, 6);
lp->tsih = sess->tsih; // MUST be zero the first time!
lp->CID = htons(1);
lp->CSG = SN_PHASE; // Security Negotiation
lp->NSG = SN_PHASE;
lp->T = 0;
switch((authm_t)lookup(AuthMethods, op->authMethod)) {
case NONE:
return 0;
case KRB5:
case SPKM1:
case SPKM2:
case SRP:
return 2;
case CHAP:
if(op->chapDigest == 0)
addText(&spp, "CHAP_A=5");
else
if(strcmp(op->chapDigest, "MD5") == 0)
addText(&spp, "CHAP_A=5");
else
if(strcmp(op->chapDigest, "SHA1") == 0)
addText(&spp, "CHAP_A=7");
else
addText(&spp, "CHAP_A=5,7");
return sendPDU(sess, &spp, handleChap);
}
return 1;
}
int
loginPhase(isess_t *sess)
{
pdu_t spp, *sp = &spp;
isc_opt_t *op = sess->op;
login_req_t *lp;
int status = 1;
debug_called(3);
bzero(sp, sizeof(pdu_t));
lp = (login_req_t *)&spp.ipdu.bhs;
lp->cmd = ISCSI_LOGIN_CMD | 0x40; // login request + Inmediate
memcpy(lp->isid, sess->isid, 6);
lp->tsih = sess->tsih; // MUST be zero the first time!
lp->CID = htons(1); // sess->cid?
if((lp->CSG = sess->csg) == LON_PHASE)
lp->NSG = FF_PHASE; // lets try and go full feature ...
else
lp->NSG = LON_PHASE;
lp->T = 1; // transit to next login stage
if(sess->flags & SESS_INITIALLOGIN1) {
sess->flags &= ~SESS_INITIALLOGIN1;
addText(sp, "SessionType=%s", op->sessionType);
addText(sp, "InitiatorName=%s", op->initiatorName);
if(strcmp(op->sessionType, "Discovery") != 0) {
addText(sp, "TargetName=%s", op->targetName);
}
}
switch(sess->csg) {
case SN_PHASE: // Security Negotiation
addText(sp, "AuthMethod=%s", op->authMethod);
break;
case LON_PHASE: // Login Operational Negotiation
if((sess->flags & SESS_NEGODONE) == 0) {
sess->flags |= SESS_NEGODONE;
addText(sp, "MaxBurstLength=%d", op->maxBurstLength);
addText(sp, "HeaderDigest=%s", op->headerDigest);
addText(sp, "DataDigest=%s", op->dataDigest);
addText(sp, "MaxRecvDataSegmentLength=%d", op->maxRecvDataSegmentLength);
addText(sp, "ErrorRecoveryLevel=%d", op->errorRecoveryLevel);
addText(sp, "DefaultTime2Wait=%d", op->defaultTime2Wait);
addText(sp, "DefaultTime2Retain=%d", op->defaultTime2Retain);
addText(sp, "DataPDUInOrder=%s", op->dataPDUInOrder? "Yes": "No");
addText(sp, "DataSequenceInOrder=%s", op->dataSequenceInOrder? "Yes": "No");
addText(sp, "MaxOutstandingR2T=%d", op->maxOutstandingR2T);
if(strcmp(op->sessionType, "Discovery") != 0) {
addText(sp, "MaxConnections=%d", op->maxConnections);
addText(sp, "FirstBurstLength=%d", op->firstBurstLength);
addText(sp, "InitialR2T=%s", op->initialR2T? "Yes": "No");
addText(sp, "ImmediateData=%s", op->immediateData? "Yes": "No");
}
}
break;
}
status = sendPDU(sess, &spp, handleLoginResp);
switch(status) {
case 0: // all is ok ...
if(sess->csg == SN_PHASE)
/*
| if we are still here, then we need
| to exchange some secrets ...
*/
status = authenticate(sess);
}
return status;
}
|