-
Notifications
You must be signed in to change notification settings - Fork 256
/
smblab.c
650 lines (526 loc) · 18.7 KB
/
smblab.c
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <fileio.h>
#include <fileXio_rpc.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <sbv_patches.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <debug.h>
#include <sys/time.h>
#include <time.h>
#include <ps2smb.h>
#define DPRINTF(args...) \
printf(args); \
scr_printf(args);
#define IP_ADDR "192.168.0.10"
#define NETMASK "255.255.255.0"
#define GATEWAY "192.168.0.1"
extern void poweroff_irx;
extern int size_poweroff_irx;
extern void ps2dev9_irx;
extern int size_ps2dev9_irx;
extern void smsutils_irx;
extern int size_smsutils_irx;
extern void smstcpip_irx;
extern int size_smstcpip_irx;
extern void smsmap_irx;
extern int size_smsmap_irx;
extern void smbman_irx;
extern int size_smbman_irx;
extern void udptty_irx;
extern int size_udptty_irx;
extern void ioptrap_irx;
extern int size_ioptrap_irx;
extern void ps2link_irx;
extern int size_ps2link_irx;
extern void iomanx_irx;
extern int size_iomanx_irx;
extern void filexio_irx;
extern int size_filexio_irx;
// for IP config
#define IPCONFIG_MAX_LEN 64
static char g_ipconfig[IPCONFIG_MAX_LEN] __attribute__((aligned(64)));
static int g_ipconfig_len;
static ShareEntry_t sharelist[128] __attribute__((aligned(64))); // Keep this aligned for DMA!
typedef struct
{
u8 unused;
u8 sec;
u8 min;
u8 hour;
u8 day;
u8 month;
u16 year;
} ps2time_t;
#define isYearLeap(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
//--------------------------------------------------------------
// I wanted this to be done on IOP, but unfortunately, the compiler
// can't handle div ops on 64 bit numbers.
static ps2time_t *smbtime2ps2time(s64 smbtime, ps2time_t *ps2time)
{
const int mdtab[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} // leap year
};
register u32 dayclock, dayno;
s64 UnixSystemTime;
register int year = 1970; // year of the Epoch
memset((void *)ps2time, 0, sizeof(ps2time_t));
// we add 5x10^6 to the number before scaling it to seconds and subtracting
// the constant (seconds between 01/01/1601 and the Epoch: 01/01/1970),
// so that we round in the same way windows does.
UnixSystemTime = (s64)(((smbtime + 5000000) / 10000000) - 11644473600);
dayclock = UnixSystemTime % 86400;
dayno = UnixSystemTime / 86400;
ps2time->sec = dayclock % 60;
ps2time->min = (dayclock % 3600) / 60;
ps2time->hour = dayclock / 3600;
while (dayno >= (isYearLeap(year) ? 366 : 365)) {
dayno -= (isYearLeap(year) ? 366 : 365);
year++;
}
ps2time->year = year;
ps2time->month = 0;
while (dayno >= mdtab[isYearLeap(year)][ps2time->month]) {
dayno -= mdtab[isYearLeap(year)][ps2time->month];
ps2time->month++;
}
ps2time->day = dayno + 1;
ps2time->month++;
return (ps2time_t *)ps2time;
}
//--------------------------------------------------------------
void set_ipconfig(void)
{
memset(g_ipconfig, 0, IPCONFIG_MAX_LEN);
g_ipconfig_len = 0;
strncpy(&g_ipconfig[g_ipconfig_len], IP_ADDR, 15);
g_ipconfig_len += strlen(IP_ADDR) + 1;
strncpy(&g_ipconfig[g_ipconfig_len], NETMASK, 15);
g_ipconfig_len += strlen(NETMASK) + 1;
strncpy(&g_ipconfig[g_ipconfig_len], GATEWAY, 15);
g_ipconfig_len += strlen(GATEWAY) + 1;
}
//--------------------------------------------------------------
int main(int argc, char *argv[2])
{
int i, ret;
init_scr();
scr_clear();
DPRINTF("smblab\n");
SifInitRpc(0);
DPRINTF("IOP Reset... ");
while (!SifIopReset("", 0))
;
while (!SifIopSync())
;
;
fioExit();
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
SifExitCmd();
SifInitRpc(0);
FlushCache(0);
FlushCache(2);
SifLoadFileInit();
SifInitIopHeap();
sbv_patch_enable_lmb();
sbv_patch_disable_prefix_check();
SifLoadModule("rom0:SIO2MAN", 0, 0);
SifLoadModule("rom0:MCMAN", 0, 0);
SifLoadModule("rom0:MCSERV", 0, 0);
DPRINTF("OK\n");
set_ipconfig();
DPRINTF("loading iomanX... ");
SifExecModuleBuffer(&iomanx_irx, size_iomanx_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading fileXio... ");
SifExecModuleBuffer(&filexio_irx, size_filexio_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading poweroff... ");
SifExecModuleBuffer(&poweroff_irx, size_poweroff_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading ps2dev9... ");
SifExecModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading smsutils... ");
SifExecModuleBuffer(&smsutils_irx, size_smsutils_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading smstcpip... ");
SifExecModuleBuffer(&smstcpip_irx, size_smstcpip_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading smsmap... ");
SifExecModuleBuffer(&smsmap_irx, size_smsmap_irx, g_ipconfig_len, g_ipconfig, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading udptty... ");
SifExecModuleBuffer(&udptty_irx, size_udptty_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading ioptrap... ");
SifExecModuleBuffer(&ioptrap_irx, size_ioptrap_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading ps2link... ");
SifExecModuleBuffer(&ps2link_irx, size_ps2link_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("loading smbman... ");
SifExecModuleBuffer(&smbman_irx, size_smbman_irx, 0, NULL, &ret);
DPRINTF("ret=%d\n", ret);
DPRINTF("modules load OK\n");
fileXioInit();
// ----------------------------------------------------------------
// how to get password hashes:
// ----------------------------------------------------------------
smbGetPasswordHashes_in_t passwd;
smbGetPasswordHashes_out_t passwdhashes;
strcpy(passwd.password, "mypassw");
DPRINTF("GETPASSWORDHASHES... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_GETPASSWORDHASHES, (void *)&passwd, sizeof(passwd), (void *)&passwdhashes, sizeof(passwdhashes));
if (ret == 0) {
DPRINTF("OK\n");
DPRINTF("LMhash = 0x");
for (i = 0; i < 16; i++)
DPRINTF("%02X", passwdhashes.LMhash[i]);
DPRINTF("\n");
DPRINTF("NTLMhash = 0x");
for (i = 0; i < 16; i++)
DPRINTF("%02X", passwdhashes.NTLMhash[i]);
DPRINTF("\n");
} else
DPRINTF("Error %d\n", ret);
// we now have 32 bytes of hash (16 bytes LM hash + 16 bytes NTLM hash)
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// how to LOGON to SMB server:
// ----------------------------------------------------------------
smbLogOn_in_t logon;
strcpy(logon.serverIP, "192.168.0.2");
logon.serverPort = 445;
strcpy(logon.User, "GUEST");
/*
strcpy(logon.User, "jimmikaelkael");
// we could reuse the generated hash
memcpy((void *)logon.Password, (void *)&passwdhashes, sizeof(passwdhashes));
logon.PasswordType = HASHED_PASSWORD;
// or log sending the plaintext password
strcpy(logon.Password, "mypassw");
logon.PasswordType = PLAINTEXT_PASSWORD;
// or simply tell we're not sending password
logon.PasswordType = NO_PASSWORD;
*/
DPRINTF("LOGON... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_LOGON, (void *)&logon, sizeof(logon), NULL, 0);
if (ret == 0) {
DPRINTF("OK ");
} else {
DPRINTF("Error %d ", ret);
}
// ----------------------------------------------------------------
// how to send an Echo to SMB server to test if it's alive:
// ----------------------------------------------------------------
smbEcho_in_t echo;
strcpy(echo.echo, "ALIVE ECHO TEST");
echo.len = strlen("ALIVE ECHO TEST");
DPRINTF(" ECHO... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_ECHO, (void *)&echo, sizeof(echo), NULL, 0);
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// how to get the available share list:
// ----------------------------------------------------------------
smbGetShareList_in_t getsharelist;
getsharelist.EE_addr = (void *)&sharelist[0];
getsharelist.maxent = 128;
DPRINTF("GETSHARELIST... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_GETSHARELIST, (void *)&getsharelist, sizeof(getsharelist), NULL, 0);
if (ret >= 0) {
DPRINTF("OK count = %d\n", ret);
for (i = 0; i < ret; i++) {
DPRINTF("\t\t - %s: %s\n", sharelist[i].ShareName, sharelist[i].ShareComment);
}
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// how to open a share:
// ----------------------------------------------------------------
smbOpenShare_in_t openshare;
strcpy(openshare.ShareName, "PS2SMB");
// we could reuse the generated hash
// memcpy((void *)logon.Password, (void *)&passwdhashes, sizeof(passwdhashes));
// logon.PasswordType = HASHED_PASSWORD;
// or log sending the plaintext password
// strcpy(logon.Password, "mypassw");
// logon.PasswordType = PLAINTEXT_PASSWORD;
// or simply tell we're not sending password
// logon.PasswordType = NO_PASSWORD;
DPRINTF("OPENSHARE... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_OPENSHARE, (void *)&openshare, sizeof(openshare), NULL, 0);
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// how to query disk informations: (you must be connected to a share)
// ----------------------------------------------------------------
smbQueryDiskInfo_out_t querydiskinfo;
DPRINTF("QUERYDISKINFO... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_QUERYDISKINFO, NULL, 0, (void *)&querydiskinfo, sizeof(querydiskinfo));
if (ret == 0) {
DPRINTF("OK\n");
DPRINTF("Total Units = %d, BlocksPerUnit = %d\n", querydiskinfo.TotalUnits, querydiskinfo.BlocksPerUnit);
DPRINTF("BlockSize = %d, FreeUnits = %d\n", querydiskinfo.BlockSize, querydiskinfo.FreeUnits);
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// getstat test:
// ----------------------------------------------------------------
DPRINTF("IO getstat... ");
iox_stat_t stats;
ret = fileXioGetStat("smb:\\", &stats);
if (ret == 0) {
DPRINTF("OK\n");
s64 smb_ctime, smb_atime, smb_mtime;
ps2time_t ctime, atime, mtime;
memcpy((void *)&smb_ctime, stats.ctime, 8);
memcpy((void *)&smb_atime, stats.atime, 8);
memcpy((void *)&smb_mtime, stats.mtime, 8);
smbtime2ps2time(smb_ctime, (ps2time_t *)&ctime);
smbtime2ps2time(smb_atime, (ps2time_t *)&atime);
smbtime2ps2time(smb_mtime, (ps2time_t *)&mtime);
s64 hisize = stats.hisize;
hisize = hisize << 32;
s64 size = hisize | stats.size;
DPRINTF("size = %ld, mode = %04x\n", size, stats.mode);
DPRINTF("ctime = %04d.%02d.%02d %02d:%02d:%02d.%02d\n",
ctime.year, ctime.month, ctime.day,
ctime.hour, ctime.min, ctime.sec, ctime.unused);
DPRINTF("atime = %04d.%02d.%02d %02d:%02d:%02d.%02d\n",
atime.year, atime.month, atime.day,
atime.hour, atime.min, atime.sec, atime.unused);
DPRINTF("mtime = %04d.%02d.%02d %02d:%02d:%02d.%02d\n",
mtime.year, mtime.month, mtime.day,
mtime.hour, mtime.min, mtime.sec, mtime.unused);
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// create directory test:
// ----------------------------------------------------------------
DPRINTF("IO mkdir... ");
ret = mkdir("smb:\\created");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// rename file test:
// ----------------------------------------------------------------
/*
DPRINTF("IO rename... ");
ret = fileXioRename("smb:\\rename_me\\rename_me.txt", "smb:\\rename_me\\renamed.txt");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
*/
// ----------------------------------------------------------------
// rename directory test:
// ----------------------------------------------------------------
DPRINTF("IO rename... ");
ret = fileXioRename("smb:\\created", "smb:\\renamed");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// delete file test:
// ----------------------------------------------------------------
/*
DPRINTF("IO remove... ");
ret = remove("smb:\\delete_me\\delete_me.txt");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
*/
// ----------------------------------------------------------------
// delete directory test:
// ----------------------------------------------------------------
DPRINTF("IO rmdir... ");
ret = rmdir("smb:\\renamed");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// open file test:
// ----------------------------------------------------------------
int fd = fileXioOpen("smb:\\BFTP.iso", O_RDONLY, 0666);
if (fd >= 0) {
// 64bit filesize test
s64 filesize = fileXioLseek64(fd, 0, SEEK_END);
u8 *p = (u8 *)&filesize;
DPRINTF("filesize = ");
for (i = 0; i < 8; i++) {
DPRINTF("%02X ", p[i]);
}
DPRINTF("\n");
// 64bit offset read test
fileXioLseek64(fd, filesize - 2041, SEEK_SET);
u8 buf[16];
fileXioRead(fd, buf, 16);
p = (u8 *)buf;
DPRINTF("read = ");
for (i = 0; i < 16; i++) {
DPRINTF("%02X", p[i]);
}
DPRINTF("\n");
// 64bit write test
/*
fileXioLseek64(fd, filesize - 16, SEEK_SET);
fileXioWrite(fd, "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC", 16);
fileXioLseek64(fd, filesize - 16, SEEK_SET);
fileXioRead(fd, buf, 16);
p = (u8 *)buf;
DPRINTF("read = ");
for (i = 0; i < 16; i++) {
DPRINTF("%02X", p[i]);
}
DPRINTF("\n");
*/
fileXioClose(fd);
}
// ----------------------------------------------------------------
// create file test:
// ----------------------------------------------------------------
fd = open("smb:\\testfile", O_RDWR | O_CREAT | O_TRUNC);
if (fd >= 0) {
write(fd, "test", 4);
close(fd);
}
// while(1);
// ----------------------------------------------------------------
// chdir test
// ----------------------------------------------------------------
DPRINTF("IO chdir... ");
ret = fileXioChdir("smb:\\dossier");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// dopen/dread/dclose test
// ----------------------------------------------------------------
iox_dirent_t dirent;
DPRINTF("IO dopen... ");
fd = fileXioDopen("smb:\\");
if (fd >= 0) {
DPRINTF("OK\n\t ");
ret = 1;
while (ret == 1) {
ret = fileXioDread(fd, &dirent);
if (ret == 1)
DPRINTF("%s ", dirent.name);
}
fileXioDclose(fd);
DPRINTF("\n");
} else {
DPRINTF("Error %d\n", ret);
}
/*
// ----------------------------------------------------------------
// chdir test
// ----------------------------------------------------------------
DPRINTF("IO chdir... ");
ret = fileXioChdir("smb:\\dossier2");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// dopen/dread/dclose test
// ----------------------------------------------------------------
DPRINTF("IO dopen... ");
fd = fileXioDopen("smb:\\");
if (fd >= 0) {
DPRINTF("OK\n\t ");
ret = 1;
while (ret == 1) {
ret = fileXioDread(fd, &dirent);
if (ret == 1)
DPRINTF("%s ", dirent.name);
}
fileXioDclose(fd);
DPRINTF("\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// chdir test
// ----------------------------------------------------------------
DPRINTF("IO chdir... ");
ret = fileXioChdir("smb:\\..");
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// dopen/dread/dclose test
// ----------------------------------------------------------------
DPRINTF("IO dopen... ");
fd = fileXioDopen("smb:\\");
if (fd >= 0) {
DPRINTF("OK\n\t ");
ret = 1;
while (ret == 1) {
ret = fileXioDread(fd, &dirent);
if (ret == 1)
DPRINTF("%s ", dirent.name);
}
fileXioDclose(fd);
DPRINTF("\n");
} else {
DPRINTF("Error %d\n", ret);
}
*/
// ----------------------------------------------------------------
// how to close a share:
// ----------------------------------------------------------------
DPRINTF("CLOSESHARE... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_CLOSESHARE, NULL, 0, NULL, 0);
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
// ----------------------------------------------------------------
// how to LOGOFF from SMB server:
// ----------------------------------------------------------------
DPRINTF("LOGOFF... ");
ret = fileXioDevctl("smb:", SMB_DEVCTL_LOGOFF, NULL, 0, NULL, 0);
if (ret == 0) {
DPRINTF("OK\n");
} else {
DPRINTF("Error %d\n", ret);
}
SleepThread();
return 0;
}