summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aquarela/smbconnect.c
blob: 6e6ce35502625715d350a5d66154684379c37973 (plain)
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
#include "headers.h"

SmbClient *
smbconnect(char *to, char *share, char **errmsgp)
{
	NbSession *nbs;
	SmbBuffer *b;
	SmbHeader h, rh;
	long n;
	ushort bytecountfixupoffset;
	ushort andxfixupoffset;
	uchar *pdata;
	SmbPeerInfo peerinfo;
	ushort index;
	vlong utcintenthsofaus;
	ulong secssince1970;
	ushort bytecount;
	int x;
	MSchapreply mschapreply;
	NbName nbto;
	SmbClient *c;
	char namebuf[100];
	ushort ipctid, sharetid;

	nbmknamefromstringandtype(nbto, to, 0x20);

	peerinfo.encryptionkey = nil;
	peerinfo.oemdomainname = nil;
	assert(smbglobals.nbname[0] != 0);
	nbs = nbssconnect(nbto, smbglobals.nbname);
	if (nbs == nil)
		return nil;
print("netbios session established\n");
	b = smbbuffernew(65535);
	memset(&h, 0, sizeof(h));
	h.command = SMB_COM_NEGOTIATE;
	h.flags2 = SMB_FLAGS2_KNOWS_LONG_NAMES | SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_UNICODE;
	h.wordcount = 0;
	h.pid = 42;
	smbbufferputheader(b, &h, &peerinfo);
	bytecountfixupoffset = smbbufferwriteoffset(b);
	smbbufferputbytes(b, nil, 2);
	smbbufferputb(b, 2);
	smbbufferputstring(b, nil, SMB_STRING_ASCII, "NT LM 0.12");
	smbbufferoffsetputs(b, bytecountfixupoffset, smbbufferwriteoffset(b) - bytecountfixupoffset - 2);
	nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
	nbsswrite(nbs, smbbufferreadpointer(b), smbbufferwriteoffset(b));
	/*
	 * now receive a reply
	 */
	smbbufferreset(b);
	n = nbssread(nbs, smbbufferwritepointer(b), smbbufferwritespace(b));
	if (n < 0) {
		smbstringprint(errmsgp, "smbconnect: read error: %r");
		goto fail;
	}
	smbbuffersetreadlen(b, n);
	nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
	if (!smbbuffergetandcheckheader(b, &rh, h.command, 1, &pdata, &bytecount, errmsgp))
		goto fail;
	if (!smbsuccess(&rh, errmsgp))
		goto fail;
	if (rh.wordcount == 0) {
		smbstringprint(errmsgp, "no parameters in negotiate response");
		goto fail;
	}
	index = smbnhgets(pdata); pdata += 2;
	if (index != 0) {
		smbstringprint(errmsgp, "no agreement on protocol");
		goto fail;
	}
	if (rh.wordcount != 17) {
		smbstringprint(errmsgp, "wrong number of parameters for negotiate response");
		goto fail;
	}
	peerinfo.securitymode = *pdata++;
	peerinfo.maxmpxcount = smbnhgets(pdata); pdata += 2;
	peerinfo.maxnumbervcs = smbnhgets(pdata); pdata += 2;
	peerinfo.maxbuffersize = smbnhgetl(pdata); pdata += 4;
	peerinfo.maxrawsize = smbnhgetl(pdata); pdata += 4;
	peerinfo.sessionkey = smbnhgets(pdata); pdata += 4;
	peerinfo.capabilities = smbnhgets(pdata); pdata += 4;
	utcintenthsofaus = smbnhgetv(pdata); pdata += 8;
	secssince1970 = utcintenthsofaus / 10000000 - 11644473600LL;
	peerinfo.utc =  (vlong)secssince1970 * (vlong)1000000000 + (utcintenthsofaus % 10000000) * 100;
	peerinfo.tzoff = -smbnhgets(pdata) * 60; pdata += 2;
	peerinfo.encryptionkeylength = *pdata++;
	print("securitymode: 0x%.2ux\n", peerinfo.securitymode);
	print("maxmpxcount: 0x%.4ux\n", peerinfo.maxmpxcount);
	print("maxnumbervcs: 0x%.4ux\n", peerinfo.maxnumbervcs);
	print("maxbuffersize: 0x%.8lux\n", peerinfo.maxbuffersize);
	print("maxrawsize: 0x%.8lux\n", peerinfo.maxrawsize);
	print("sessionkey: 0x%.8lux\n", peerinfo.sessionkey);
	print("capabilities: 0x%.8lux\n", peerinfo.capabilities);
	print("utc: %s(and %lld μs)\n", asctime(gmtime(peerinfo.utc / 1000000000)), peerinfo.utc % 1000000000);
	print("tzoff: %d\n", peerinfo.tzoff);
	print("encryptionkeylength: %d\n", peerinfo.encryptionkeylength);
	smberealloc(&peerinfo.encryptionkey, peerinfo.encryptionkeylength);
	if (!smbbuffergetbytes(b, peerinfo.encryptionkey, peerinfo.encryptionkeylength)) {
		smbstringprint(errmsgp, "not enough data for encryption key");
		goto fail;
	}
	print("encryptionkey: ");
	for (x = 0; x < peerinfo.encryptionkeylength; x++)
		print("%.2ux", peerinfo.encryptionkey[x]);
	print("\n");
	if (!smbbuffergetucs2(b, 0, &peerinfo.oemdomainname)) {
		smbstringprint(errmsgp, "not enough data for oemdomainname");
		goto fail;
	}
	print("oemdomainname: %s\n", peerinfo.oemdomainname);
	if (peerinfo.capabilities & CAP_EXTENDED_SECURITY) {
		smbstringprint(errmsgp, "server wants extended security");
		goto fail;
	}
	/*
	 * ok - now send SMB_COM_SESSION_SETUP_ANDX
	 * fix the flags to reflect what the peer can do
	 */
	smbbufferreset(b);
	h.command = SMB_COM_SESSION_SETUP_ANDX;
	h.wordcount = 13;
	h.flags2 &= ~SMB_FLAGS2_UNICODE;
	if (smbsendunicode(&peerinfo))
		h.flags2 |= SMB_FLAGS2_UNICODE;
	smbbufferputheader(b, &h, &peerinfo);
	smbbufferputb(b, SMB_COM_TREE_CONNECT_ANDX);
	smbbufferputb(b, 0);
	andxfixupoffset = smbbufferwriteoffset(b);
	smbbufferputs(b, 0);
	smbbufferputs(b, 0xffff);
	smbbufferputs(b, 1);
	smbbufferputs(b, 0);
	smbbufferputl(b, peerinfo.sessionkey);
	smbbufferputs(b, sizeof(mschapreply.LMresp));
	smbbufferputs(b, sizeof(mschapreply.NTresp));
	smbbufferputl(b, 0);
	smbbufferputl(b, CAP_UNICODE | CAP_LARGE_FILES);
	bytecountfixupoffset = smbbufferwriteoffset(b);
	smbbufferputs(b, 0);
	if (auth_respond(peerinfo.encryptionkey, peerinfo.encryptionkeylength,
		nil, 0,
		&mschapreply, sizeof(mschapreply), auth_getkey,
		"proto=mschap role=client server=%s", "cher") != sizeof(mschapreply)) {
		print("auth_respond failed: %r\n");
		goto fail;
	}
	smbbufferputbytes(b, &mschapreply, sizeof(mschapreply));
	smbbufferputstring(b, &peerinfo, 0, smbglobals.accountname);
	smbbufferputstring(b, &peerinfo, 0, smbglobals.primarydomain);
	smbbufferputstring(b, &peerinfo, 0, smbglobals.nativeos);
	smbbufferputstring(b, &peerinfo, 0, "");
	smbbufferoffsetputs(b, bytecountfixupoffset, smbbufferwriteoffset(b) - bytecountfixupoffset - 2);
	smbbufferalignl2(b, 2);
	smbbufferoffsetputs(b, andxfixupoffset, smbbufferwriteoffset(b));
	smbbufferputb(b, 4);
	smbbufferputb(b, SMB_COM_NO_ANDX_COMMAND);
	smbbufferputb(b, 0);
	smbbufferputs(b, 0);
	smbbufferputs(b, 0);
	smbbufferputs(b, 0);
	bytecountfixupoffset = smbbufferwriteoffset(b);
	smbbufferputs(b, 0);
	strcpy(namebuf, "\\\\");
	strcat(namebuf, to);
	strcat(namebuf, "\\IPC$");
	smbbufferputstring(b, &peerinfo, SMB_STRING_UPCASE, namebuf);
	smbbufferputstring(b, nil, SMB_STRING_ASCII, "?????");
	smbbufferoffsetputs(b, bytecountfixupoffset, smbbufferwriteoffset(b) - bytecountfixupoffset - 2);
	nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
	nbsswrite(nbs, smbbufferreadpointer(b), smbbufferwriteoffset(b));
	smbbufferreset(b);
	n = nbssread(nbs, smbbufferwritepointer(b), smbbufferwritespace(b));
	if (n < 0) {
		smbstringprint(errmsgp, "read error: %r");
		goto fail;
	}
	smbbuffersetreadlen(b, n);
	nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
	if (!smbbuffergetandcheckheader(b, &rh, h.command, 1, &pdata, &bytecount, errmsgp))
		goto fail;
	if (!smbsuccess(&rh, errmsgp))
		goto fail;
	h.uid = rh.uid;
	ipctid = rh.tid;
	/*
	 * now do another TREE_CONNECT if needed
	 */
	if (share) {
		smbbufferreset(b);
		h.command = SMB_COM_TREE_CONNECT_ANDX;
		h.wordcount = 4;
		h.tid = 0;
		smbbufferputheader(b, &h, &peerinfo);
		smbbufferputb(b, SMB_COM_NO_ANDX_COMMAND);
		smbbufferputb(b, 0);
		smbbufferputs(b, 0);
		smbbufferputs(b, 0);
		smbbufferputs(b, 0);
		bytecountfixupoffset = smbbufferwriteoffset(b);
		smbbufferputs(b, 0);
		strcpy(namebuf, "\\\\");
		strcat(namebuf, to);
		strcat(namebuf, "\\");
		strcat(namebuf, share);
		smbbufferputstring(b, &peerinfo, SMB_STRING_UPCASE, namebuf);
		smbbufferputstring(b, nil, SMB_STRING_ASCII, "A:");
		smbbufferoffsetputs(b, bytecountfixupoffset, smbbufferwriteoffset(b) - bytecountfixupoffset - 2);
		nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
		nbsswrite(nbs, smbbufferreadpointer(b), smbbufferwriteoffset(b));
		smbbufferreset(b);
		n = nbssread(nbs, smbbufferwritepointer(b), smbbufferwritespace(b));
		if (n < 0) {
			smbstringprint(errmsgp, "read error: %r");
			goto fail;
		}
		smbbuffersetreadlen(b, n);
		nbdumpdata(smbbufferreadpointer(b), smbbufferwriteoffset(b));
		if (!smbbuffergetandcheckheader(b, &rh, h.command, 3, &pdata, &bytecount, errmsgp))
			goto fail;
		if (!smbsuccess(&rh, errmsgp))
			goto fail;
		sharetid = rh.tid;
	}
	else
		sharetid = -2;
	c = smbemalloc(sizeof(*c));
	c->peerinfo = peerinfo;
	c->ipctid = ipctid;
	c->sharetid = sharetid;
	c->b = b;
	c->protoh = h;
	c->nbss = nbs;
	return c;
fail:
	smbbufferfree(&b);
	free(peerinfo.encryptionkey);
	free(peerinfo.oemdomainname);
	return nil;
}

void
smbclientfree(SmbClient *c)
{
	if (c) {
		free(c->peerinfo.encryptionkey);
		free(c->peerinfo.oemdomainname);
		free(c);
		smbbufferfree(&c->b);
	}
}

int
smbtransactionclientsend(void *magic, SmbBuffer *ob, char **)
{
	SmbClient *c = magic;
smblogprint(-1, "sending:\n");
smblogdata(-1, smblogprint, smbbufferreadpointer(ob), smbbufferwriteoffset(ob), 256);
	return nbsswrite(c->nbss, smbbufferreadpointer(ob), smbbufferwriteoffset(ob)) == 0;
}

int
smbtransactionclientreceive(void *magic, SmbBuffer *ib, char **)
{
	long n; 
	SmbClient *c = magic;
	smbbufferreset(ib);
	n = nbssread(c->nbss, smbbufferwritepointer(ib), smbbufferwritespace(ib));
	if (n >= 0) {
		assert(smbbufferputbytes(ib, nil, n));
		return 1;
	}
	return 0;
}