]>
Commit | Line | Data |
---|---|---|
04e4e140 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
04e4e140 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
7c16470c AJ |
9 | /* UNIX SMBlib NetBIOS implementation |
10 | ||
11 | Version 1.0 | |
12 | SMBlib File Access Routines | |
13 | ||
14 | Copyright (C) Richard Sharpe 1996 | |
7c16470c AJ |
15 | */ |
16 | ||
17 | /* | |
18 | This program is free software; you can redistribute it and/or modify | |
19 | it under the terms of the GNU General Public License as published by | |
20 | the Free Software Foundation; either version 2 of the License, or | |
21 | (at your option) any later version. | |
22 | ||
23 | This program is distributed in the hope that it will be useful, | |
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | GNU General Public License for more details. | |
27 | ||
28 | You should have received a copy of the GNU General Public License | |
29 | along with this program; if not, write to the Free Software | |
30 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
31 | */ | |
32 | ||
04e4e140 | 33 | #include "squid.h" |
0ab648c9 | 34 | #include "rfcnb/rfcnb.h" |
602d9612 | 35 | #include "smblib/smblib-priv.h" |
7c16470c | 36 | |
0ab648c9 AJ |
37 | #if HAVE_STRING_H |
38 | #include <string.h> | |
39 | #endif | |
7c16470c AJ |
40 | |
41 | /* Open a file with file_name using desired mode and search attr */ | |
42 | /* If File_Handle is null, then create and populate a file handle */ | |
43 | ||
44 | SMB_File *SMB_Open(SMB_Tree_Handle Tree_Handle, | |
45 | SMB_File *File_Handle, | |
46 | char *file_name, | |
47 | WORD mode, | |
48 | WORD search) | |
49 | ||
50 | { | |
51 | struct RFCNB_Pkt *pkt; | |
52 | int pkt_len, param_len; | |
53 | char *p; | |
54 | struct SMB_File_Def *file_tmp; | |
55 | ||
56 | /* We allocate a file object and copy some things ... */ | |
57 | ||
58 | file_tmp = File_Handle; | |
59 | ||
60 | if (File_Handle == NULL) { | |
61 | ||
62 | if ((file_tmp = (SMB_File *)malloc(sizeof(SMB_File))) == NULL) { | |
63 | ||
64 | #ifdef DEBUG | |
65 | fprintf(stderr, "Could not allocate file handle space ..."); | |
66 | #endif | |
67 | ||
68 | SMBlib_errno = SMBlibE_NoSpace; | |
69 | return(NULL); | |
70 | ||
71 | } | |
72 | ||
73 | } | |
74 | ||
75 | strncpy(file_tmp -> filename, file_name, sizeof(file_tmp -> filename) - 1); | |
76 | file_tmp -> tree = Tree_Handle; | |
77 | file_tmp -> fid = 0xFFFF; /* Is this an invalid FID? */ | |
78 | ||
79 | param_len = strlen(file_name) + 2; /* 1 for null, 1 for ASCII marker */ | |
80 | ||
81 | pkt_len = SMB_open_len + param_len; | |
82 | ||
83 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(max(pkt_len, SMB_openr_len)); | |
84 | ||
85 | if (pkt == NULL) { /* Really should do some error handling */ | |
86 | ||
87 | if (File_Handle == NULL) | |
88 | free(file_tmp); | |
89 | SMBlib_errno = SMBlibE_NoSpace; | |
90 | return(NULL); | |
91 | ||
92 | } | |
93 | ||
94 | /* Now plug in the bits we need */ | |
95 | ||
0ab648c9 | 96 | memset(SMB_Hdr(pkt), 0, SMB_open_len); |
7c16470c AJ |
97 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
98 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBopen; | |
99 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Tree_Handle -> con -> pid); | |
100 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, Tree_Handle -> tid); | |
101 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Tree_Handle -> con -> mid); | |
102 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Tree_Handle -> con -> uid); | |
103 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 2; | |
104 | ||
105 | SSVAL(SMB_Hdr(pkt), SMB_open_mod_offset, mode); | |
106 | SSVAL(SMB_Hdr(pkt), SMB_open_atr_offset, search); | |
107 | SSVAL(SMB_Hdr(pkt), SMB_open_bcc_offset, param_len); | |
108 | ||
109 | /* Now plug in the file name ... */ | |
110 | ||
111 | p = (char *)(SMB_Hdr(pkt) + SMB_open_buf_offset); | |
112 | *p = SMBasciiID; | |
113 | strcpy(p+1, file_name); | |
114 | p = p + strlen(file_name); | |
115 | *(p+1) = 0; /* plug in a null ... */ | |
116 | ||
117 | /* Now send the packet and get the response ... */ | |
118 | ||
119 | if (RFCNB_Send(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
120 | ||
121 | #ifdef DEBUG | |
122 | fprintf(stderr, "Error sending Open request\n"); | |
123 | #endif | |
124 | ||
125 | if (File_Handle == NULL) | |
126 | free(file_tmp); | |
127 | RFCNB_Free_Pkt(pkt); | |
128 | SMBlib_errno = -SMBlibE_SendFailed; | |
129 | return(NULL); | |
130 | ||
131 | } | |
132 | ||
133 | /* Now get the response ... */ | |
134 | ||
135 | #ifdef DEBUG | |
136 | fprintf(stderr, "Pkt_Len for Open resp = %i\n", pkt_len); | |
137 | #endif | |
138 | ||
139 | if (RFCNB_Recv(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
140 | ||
141 | #ifdef DEBUG | |
142 | fprintf(stderr, "Error receiving response to open request\n"); | |
143 | #endif | |
144 | ||
145 | if (File_Handle = NULL) | |
146 | free(file_tmp); | |
147 | RFCNB_Free_Pkt(pkt); | |
148 | SMBlib_errno = -SMBlibE_RecvFailed; | |
149 | return(NULL); | |
150 | ||
151 | } | |
152 | ||
153 | /* Now parse the response and pass back any error ... */ | |
154 | ||
155 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
156 | ||
157 | #ifdef DEBUG | |
158 | fprintf(stderr, "SMB_Open failed with errorclass = %i, Error Code = %i\n", | |
159 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
160 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
161 | #endif | |
162 | ||
163 | if (File_Handle = NULL) | |
164 | free(file_tmp); | |
165 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
166 | RFCNB_Free_Pkt(pkt); | |
167 | SMBlib_errno = SMBlibE_Remote; | |
168 | return(NULL); /* Should clean up ... */ | |
169 | ||
170 | } | |
171 | ||
172 | file_tmp -> fid = SVAL(SMB_Hdr(pkt), SMB_openr_fid_offset); | |
173 | file_tmp -> lastmod = IVAL(SMB_Hdr(pkt), SMB_openr_tim_offset); | |
174 | file_tmp -> size = IVAL(SMB_Hdr(pkt), SMB_openr_fsz_offset); | |
175 | file_tmp -> access = SVAL(SMB_Hdr(pkt), SMB_openr_acc_offset); | |
176 | file_tmp -> fileloc = 0; | |
177 | ||
178 | RFCNB_Free_Pkt(pkt); /* Free up this space */ | |
179 | ||
180 | #ifdef DEBUG | |
181 | fprintf(stderr, "SMB_Open succeeded, FID = %i\n", file_tmp -> fid); | |
182 | #endif | |
183 | ||
184 | RFCNB_Free_Pkt(pkt); | |
185 | ||
186 | return(file_tmp); | |
187 | ||
188 | } | |
189 | ||
190 | /* Close the file referred to in File_Handle */ | |
191 | ||
192 | int SMB_Close(SMB_File *File_Handle) | |
193 | ||
194 | { | |
195 | struct SMB_Close_Prot_Def *prot_pkt; | |
196 | struct SMB_Hdr_Def_LM12 *resp_pkt; | |
197 | struct RFCNB_Pkt *pkt; | |
198 | int pkt_len; | |
199 | ||
200 | if (File_Handle == NULL) { /* Error */ | |
201 | ||
202 | /*SMBLIB_errno = SMBLIBE_BadHandle; */ | |
203 | return(-1); | |
204 | ||
205 | } | |
206 | ||
207 | pkt_len = SMB_clos_len; | |
208 | ||
209 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
210 | ||
211 | if (pkt == NULL) { /* Really should do some error handling */ | |
212 | ||
213 | SMBlib_errno = SMBlibE_NoSpace; | |
214 | return(SMBlibE_BAD); | |
215 | ||
216 | } | |
217 | ||
218 | /* Now plug in the bits we need */ | |
219 | ||
0ab648c9 | 220 | memset(SMB_Hdr(pkt), 0, SMB_clos_len); |
7c16470c AJ |
221 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
222 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBclose; | |
223 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, File_Handle -> tree -> con -> pid); | |
224 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, File_Handle -> tree -> tid); | |
225 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, File_Handle -> tree -> con -> mid); | |
226 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, File_Handle -> tree -> con -> uid); | |
227 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 3; | |
228 | ||
229 | SSVAL(SMB_Hdr(pkt), SMB_clos_fid_offset, File_Handle -> fid); | |
230 | SIVAL(SMB_Hdr(pkt), SMB_clos_tim_offset, 0); | |
231 | SSVAL(SMB_Hdr(pkt), SMB_clos_bcc_offset, 0); | |
232 | ||
233 | /* Now send the packet and get the response ... */ | |
234 | ||
235 | if (RFCNB_Send(File_Handle -> tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
236 | ||
237 | #ifdef DEBUG | |
238 | fprintf(stderr, "Error sending Open request\n"); | |
239 | #endif | |
240 | ||
241 | RFCNB_Free_Pkt(pkt); | |
242 | SMBlib_errno = SMBlibE_SendFailed; | |
243 | return(SMBlibE_BAD); | |
244 | ||
245 | } | |
246 | ||
247 | /* Now get the response ... */ | |
248 | ||
249 | if (RFCNB_Recv(File_Handle -> tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
250 | ||
251 | #ifdef DEBUG | |
252 | fprintf(stderr, "Error receiving response to open request\n"); | |
253 | #endif | |
254 | ||
255 | RFCNB_Free_Pkt(pkt); | |
256 | SMBlib_errno = SMBlibE_RecvFailed; | |
257 | return(SMBlibE_BAD); | |
258 | ||
259 | } | |
260 | ||
261 | /* Now parse the response and pass back any error ... */ | |
262 | ||
263 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
264 | ||
265 | #ifdef DEBUG | |
266 | fprintf(stderr, "SMB_Close failed with errorclass = %i, Error Code = %i\n", | |
267 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
268 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
269 | #endif | |
270 | ||
271 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
272 | RFCNB_Free_Pkt(pkt); | |
273 | SMBlib_errno = SMBlibE_Remote; | |
274 | return(SMBlibE_BAD); /* Should clean up ... */ | |
275 | ||
276 | } | |
277 | ||
278 | #ifdef DEBUG | |
279 | fprintf(stderr, "File %s closed successfully.\n", File_Handle -> filename); | |
280 | #endif DEBUG | |
281 | ||
282 | /* We should deallocate the File_Handle now ... */ | |
283 | ||
284 | File_Handle -> tree = NULL; | |
285 | File_Handle -> filename[0] = 0; | |
286 | File_Handle -> fid = 0xFFFF; | |
287 | ||
288 | RFCNB_Free_Pkt(pkt); | |
289 | free(File_Handle); | |
290 | ||
291 | return(0); | |
292 | } | |
293 | ||
294 | /* Read numbytes into data from the file pointed to by File_Handle from */ | |
295 | /* the offset in the File_Handle. */ | |
296 | ||
297 | int SMB_Read(SMB_File *File_Handle, char *data, int numbytes) | |
298 | ||
299 | { | |
300 | int tot_read; | |
301 | struct RFCNB_Pkt *snd_pkt, *recv_pkt, *data_ptr; | |
302 | int snd_pkt_len, recv_pkt_len, this_read, bytes_left = numbytes; | |
303 | int max_read_data, bytes_read = 0; | |
304 | ||
305 | /* We loop around, reading the data, accumulating it into the buffer */ | |
306 | /* We build an SMB packet, where the data is pointed to by a fragment*/ | |
307 | /* tagged onto the end */ | |
308 | ||
309 | data_ptr = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(0); | |
310 | if (data_ptr == NULL) { | |
311 | ||
312 | /* We should handle the error here */ | |
313 | ||
314 | SMBlib_errno = SMBlibE_NoSpace; | |
315 | return(SMBlibE_BAD); | |
316 | ||
317 | } | |
318 | ||
319 | snd_pkt_len = SMB_read_len; /* size for the read SMB */ | |
320 | recv_pkt_len = SMB_readr_len + 3; /* + 3 for the datablockID and blklen */ | |
321 | ||
322 | snd_pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(snd_pkt_len); | |
323 | ||
324 | if (snd_pkt == NULL) { | |
325 | ||
326 | RFCNB_Free_Pkt(data_ptr); | |
327 | SMBlib_errno = SMBlibE_NoSpace; | |
328 | return(SMBlibE_BAD); | |
329 | ||
330 | } | |
331 | ||
332 | recv_pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(recv_pkt_len); | |
333 | ||
334 | if (recv_pkt == NULL) { | |
335 | ||
336 | RFCNB_Free_Pkt(snd_pkt); | |
337 | RFCNB_Free_Pkt(data_ptr); | |
338 | SMBlib_errno = SMBlibE_NoSpace; | |
339 | return(SMBlibE_BAD); | |
340 | ||
341 | } | |
342 | ||
343 | /* Put the recv pkt together */ | |
344 | ||
345 | recv_pkt -> next = data_ptr; | |
346 | ||
347 | /* Now build the read request and the receive packet etc ... */ | |
348 | ||
0ab648c9 | 349 | memset(SMB_Hdr(snd_pkt), 0, SMB_read_len); |
7c16470c AJ |
350 | SIVAL(SMB_Hdr(snd_pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
351 | *(SMB_Hdr(snd_pkt) + SMB_hdr_com_offset) = SMBread; | |
352 | SSVAL(SMB_Hdr(snd_pkt), SMB_hdr_pid_offset, File_Handle -> tree -> con -> pid); | |
353 | SSVAL(SMB_Hdr(snd_pkt), SMB_hdr_tid_offset, File_Handle -> tree -> tid); | |
354 | SSVAL(SMB_Hdr(snd_pkt), SMB_hdr_mid_offset, File_Handle -> tree -> con -> mid); | |
355 | SSVAL(SMB_Hdr(snd_pkt), SMB_hdr_uid_offset, File_Handle -> tree -> con -> uid); | |
356 | *(SMB_Hdr(snd_pkt) + SMB_hdr_wct_offset) = 5; | |
357 | SSVAL(SMB_Hdr(snd_pkt), SMB_read_fid_offset, File_Handle -> fid); | |
358 | ||
359 | max_read_data = (File_Handle -> tree -> mbs) - recv_pkt_len; | |
360 | ||
361 | while (bytes_left > 0) { | |
362 | ||
363 | this_read = (bytes_left > max_read_data?max_read_data: bytes_left); | |
364 | ||
365 | SSVAL(SMB_Hdr(snd_pkt), SMB_read_cnt_offset, this_read); | |
366 | SIVAL(SMB_Hdr(snd_pkt), SMB_read_ofs_offset, File_Handle -> fileloc); | |
367 | SSVAL(SMB_Hdr(snd_pkt), SMB_read_clf_offset, 0x0); | |
368 | SSVAL(SMB_Hdr(snd_pkt), SMB_read_bcc_offset, 0x0); | |
369 | ||
370 | /* Now send the packet and wait for a response */ | |
371 | ||
372 | if (RFCNB_Send(File_Handle -> tree -> con -> Trans_Connect, snd_pkt, snd_pkt_len) < 0) { | |
373 | ||
374 | #ifdef DEBUG | |
375 | fprintf(stderr, "Error sending read request\n"); | |
376 | #endif | |
377 | ||
378 | data_ptr -> data = NULL; | |
379 | data_ptr -> len = 0; | |
380 | RFCNB_Free_Pkt(recv_pkt); | |
381 | RFCNB_Free_Pkt(snd_pkt); | |
382 | SMBlib_errno = SMBlibE_SendFailed; | |
383 | return(SMBlibE_BAD); | |
384 | ||
385 | } | |
386 | ||
387 | /* Now get the response ... first point the data portion to the right */ | |
388 | /* place in the read buffer ... what we are doing is ugly */ | |
389 | ||
390 | data_ptr -> data = (data + bytes_read); | |
391 | data_ptr -> len = this_read; | |
392 | ||
393 | if (RFCNB_Recv(File_Handle -> tree -> con -> Trans_Connect, recv_pkt, recv_pkt_len + this_read) < 0) { | |
394 | ||
395 | #ifdef DEBUG | |
396 | fprintf(stderr, "Error receiving response to write\n"); | |
397 | #endif | |
398 | ||
399 | data_ptr -> len = 0; | |
400 | data_ptr -> data = NULL; | |
401 | RFCNB_Free_Pkt(recv_pkt); | |
402 | RFCNB_Free_Pkt(snd_pkt); | |
403 | SMBlib_errno = SMBlibE_RecvFailed; | |
404 | return(SMBlibE_BAD); | |
405 | ||
406 | } | |
407 | ||
408 | if (CVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
409 | ||
410 | #ifdef DEBUG | |
411 | fprintf(stderr, "SMB_Read failed with errorclass = %i, Error Code = %i\n", | |
412 | CVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset), | |
413 | SVAL(SMB_Hdr(recv_pkt), SMB_hdr_err_offset)); | |
414 | #endif | |
415 | ||
416 | SMBlib_SMB_Error = IVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset); | |
417 | data_ptr -> data = NULL; | |
418 | data_ptr -> len = 0; | |
419 | RFCNB_Free_Pkt(recv_pkt); | |
420 | RFCNB_Free_Pkt(snd_pkt); | |
421 | SMBlib_errno = SMBlibE_Remote; | |
422 | return(-1); | |
423 | ||
424 | } | |
425 | ||
426 | /* Ok, that worked, so update some things here ... */ | |
427 | ||
428 | bytes_read = bytes_read + SVAL(SMB_Hdr(recv_pkt), SMB_readr_cnt_offset); | |
429 | bytes_left = bytes_left - SVAL(SMB_Hdr(recv_pkt), SMB_readr_cnt_offset); | |
430 | ||
431 | } | |
432 | ||
433 | /* Now free those packet headers that we allocated ... */ | |
434 | ||
435 | data_ptr -> data = NULL; /* Since recv_pkt points to data_ptr */ | |
436 | data_ptr -> len = 0; /* it is freed too */ | |
437 | RFCNB_Free_Pkt(recv_pkt); | |
438 | RFCNB_Free_Pkt(snd_pkt); | |
439 | ||
440 | return(bytes_read); | |
441 | ||
442 | } | |
443 | ||
444 | /* Lseek seeks just like the UNIX version does ... */ | |
445 | ||
446 | off_t SMB_Lseek(SMB_File *File_Handle, off_t offset, int whence) | |
447 | ||
448 | { | |
449 | ||
450 | /* We should check that the file handle is kosher ... We may also blow up | |
451 | if we get a 64 bit offset ... should avoid wrap-around ... */ | |
452 | ||
453 | switch (whence) { | |
454 | case SEEK_SET: | |
455 | ||
456 | File_Handle -> fileloc = offset; | |
457 | break; | |
458 | ||
459 | case SEEK_CUR: | |
460 | ||
461 | File_Handle -> fileloc = File_Handle -> fileloc + offset; | |
462 | break; | |
463 | ||
464 | case SEEK_END: | |
465 | ||
466 | File_Handle -> fileloc = File_Handle -> size + offset; | |
467 | break; | |
468 | ||
469 | default: | |
470 | return(-1); | |
471 | ||
472 | } | |
473 | ||
474 | return(File_Handle -> fileloc); | |
475 | ||
476 | } | |
477 | ||
7c16470c AJ |
478 | /* Write numbytes from data to the file pointed to by the File_Handle at */ |
479 | /* the offset in the File_Handle. */ | |
480 | ||
481 | int SMB_Write(SMB_File *File_Handle, char *data, int numbytes) | |
482 | ||
483 | { | |
484 | int tot_written = 0; | |
485 | struct RFCNB_Pkt *pkt, *data_ptr; | |
486 | int pkt_len, i, this_write, max_write_data, bytes_left = numbytes; | |
487 | ||
488 | /* We loop around, writing the data, accumulating what was written */ | |
489 | /* We build an SMB packet, where the data is pointed to by a fragment */ | |
490 | /* tagged onto the end ... */ | |
491 | ||
492 | data_ptr = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(0); | |
493 | if (data_ptr == NULL) { | |
494 | ||
495 | SMBlib_errno = SMBlibE_NoSpace; | |
496 | return(SMBlibE_BAD); | |
497 | ||
498 | } | |
499 | ||
500 | pkt_len = SMB_write_len + 3; /* + 3 for the datablockID and blklen */ | |
501 | ||
502 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
503 | ||
504 | if (pkt == NULL) { | |
505 | ||
506 | RFCNB_Free_Pkt(data_ptr); | |
507 | SMBlib_errno = SMBlibE_NoSpace; | |
508 | return(SMBlibE_BAD); | |
509 | ||
510 | } | |
511 | ||
512 | /* Now init the things that will be the same across the possibly multiple | |
513 | packets to write this data. */ | |
514 | ||
0ab648c9 | 515 | memset(SMB_Hdr(pkt), 0, SMB_write_len); |
7c16470c AJ |
516 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
517 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBwrite; | |
518 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, File_Handle -> tree -> con -> pid); | |
519 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, File_Handle -> tree -> tid); | |
520 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, File_Handle -> tree -> con -> mid); | |
521 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, File_Handle -> tree -> con -> uid); | |
522 | SSVAL(SMB_Hdr(pkt), SMB_write_fid_offset, File_Handle -> fid); | |
523 | ||
524 | /* We will program this as send/response for the moment, but if we could | |
525 | only send the second block before getting the first, we could speed | |
526 | things up a bit ... */ | |
527 | ||
528 | max_write_data = (File_Handle -> tree -> mbs) - pkt_len; | |
529 | ||
2f8abb64 | 530 | /* the 3 is for the data block id and length that precedes the data */ |
7c16470c AJ |
531 | |
532 | while (bytes_left > 0) { | |
533 | ||
534 | /* bytes to write? */ | |
535 | ||
536 | this_write = (bytes_left > max_write_data?max_write_data:bytes_left); | |
537 | ||
538 | data_ptr -> next = NULL; | |
539 | data_ptr -> len = this_write; | |
540 | data_ptr -> data = data + tot_written; | |
541 | ||
542 | pkt -> next = data_ptr; /* link the data on the end */ | |
543 | ||
544 | SSVAL(SMB_Hdr(pkt), SMB_hdr_flg_offset, 0); | |
545 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 5; | |
546 | SSVAL(SMB_Hdr(pkt), SMB_write_fid_offset, File_Handle -> fid); | |
547 | SSVAL(SMB_Hdr(pkt), SMB_write_cnt_offset, this_write); | |
548 | SIVAL(SMB_Hdr(pkt), SMB_write_ofs_offset, File_Handle -> fileloc); | |
549 | SSVAL(SMB_Hdr(pkt), SMB_write_clf_offset, 0); | |
550 | SSVAL(SMB_Hdr(pkt), SMB_write_bcc_offset, (this_write + 3)); | |
551 | ||
552 | *(SMB_Hdr(pkt) + SMB_write_buf_offset) = SMBdatablockID; | |
553 | SSVAL(SMB_Hdr(pkt), SMB_write_buf_offset + 1, this_write); | |
554 | ||
555 | /* Now send the packet and wait for a response */ | |
556 | ||
557 | if (RFCNB_Send(File_Handle -> tree -> con -> Trans_Connect, pkt, pkt_len + this_write) < 0) { | |
558 | ||
559 | #ifdef DEBUG | |
560 | fprintf(stderr, "Error sending write request\n"); | |
561 | #endif | |
562 | ||
563 | data_ptr -> next = NULL; | |
564 | data_ptr -> len = 0; | |
565 | RFCNB_Free_Pkt(pkt); | |
566 | SMBlib_errno = -SMBlibE_SendFailed; | |
567 | return(-1); | |
568 | ||
569 | } | |
570 | ||
571 | /* Now get the response ... */ | |
572 | ||
573 | if (RFCNB_Recv(File_Handle -> tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
574 | ||
575 | #ifdef DEBUG | |
576 | fprintf(stderr, "Error receiving response to write\n"); | |
577 | #endif | |
578 | ||
579 | data_ptr -> next = NULL; | |
580 | data_ptr -> len = 0; | |
581 | RFCNB_Free_Pkt(pkt); | |
582 | SMBlib_errno = -SMBlibE_RecvFailed; | |
583 | return(-1); | |
584 | ||
585 | } | |
586 | ||
587 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
588 | ||
589 | #ifdef DEBUG | |
590 | fprintf(stderr, "SMB_Write failed with errorclass = %i, Error Code = %i\n", | |
591 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
592 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
593 | #endif | |
594 | ||
595 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
596 | data_ptr -> data = NULL; | |
597 | data_ptr -> len = 0; | |
598 | RFCNB_Free_Pkt(pkt); | |
599 | SMBlib_errno = SMBlibE_Remote; | |
600 | return(SMBlibE_BAD); | |
601 | ||
602 | } | |
603 | ||
604 | /* Ok, that worked, so update some things here ... */ | |
605 | ||
606 | tot_written = tot_written + this_write; | |
607 | bytes_left = bytes_left - this_write; | |
608 | ||
609 | /* Assume that it is ok to update this now, but what about only part */ | |
610 | /* of the write succeeding? */ | |
611 | ||
612 | File_Handle -> fileloc = File_Handle -> fileloc + this_write; | |
613 | ||
614 | #ifdef DEBUG | |
615 | fprintf(stderr, "--This_write = %i, bytes_left = %i\n", | |
616 | this_write, bytes_left); | |
617 | #endif | |
618 | ||
619 | } | |
620 | ||
621 | /* Let's get rid of those packet headers we are using ... */ | |
622 | ||
623 | data_ptr -> data = NULL; | |
624 | pkt -> next = NULL; | |
625 | ||
626 | RFCNB_Free_Pkt(pkt); | |
627 | ||
628 | return(tot_written); | |
629 | ||
630 | } | |
631 | ||
632 | /* Create file on the server with name file_name and attributes search */ | |
633 | ||
634 | SMB_File *SMB_Create(SMB_Tree_Handle Tree_Handle, | |
635 | SMB_File *File_Handle, | |
636 | char *file_name, | |
637 | WORD search) | |
638 | ||
639 | { | |
640 | struct RFCNB_Pkt *pkt; | |
641 | int pkt_len, param_len; | |
642 | char *p; | |
643 | struct SMB_File_Def *file_tmp; | |
644 | ||
645 | /* We allocate a file object and copy some things ... */ | |
646 | ||
647 | file_tmp = File_Handle; | |
648 | ||
649 | if (File_Handle == NULL) { | |
650 | ||
651 | if ((file_tmp = (SMB_File *)malloc(sizeof(SMB_File))) == NULL) { | |
652 | ||
653 | #ifdef DEBUG | |
654 | fprintf(stderr, "Could not allocate file handle space ..."); | |
655 | #endif | |
656 | ||
657 | SMBlib_errno = SMBlibE_NoSpace; | |
658 | return(NULL); | |
659 | ||
660 | } | |
661 | ||
662 | } | |
663 | ||
664 | strncpy(file_tmp -> filename, file_name, sizeof(file_tmp -> filename)); | |
665 | file_tmp -> tree = Tree_Handle; | |
666 | file_tmp -> fid = 0xFFFF; /* Is this an invalid FID? */ | |
667 | ||
668 | param_len = strlen(file_name) + 2; /* 1 for null, 1 for ASCII marker */ | |
669 | ||
670 | pkt_len = SMB_creat_len + param_len; | |
671 | ||
672 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
673 | ||
674 | if (pkt == NULL) { /* Really should do some error handling */ | |
675 | ||
676 | if (File_Handle == NULL) | |
677 | free(file_tmp); | |
678 | SMBlib_errno = SMBlibE_NoSpace; | |
679 | return(NULL); | |
680 | ||
681 | } | |
682 | ||
683 | /* Now plug in the bits we need */ | |
684 | ||
0ab648c9 | 685 | memset(SMB_Hdr(pkt), 0, SMB_creat_len); |
7c16470c AJ |
686 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
687 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBcreate; | |
688 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Tree_Handle -> con -> pid); | |
689 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, Tree_Handle -> tid); | |
690 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Tree_Handle -> con -> mid); | |
691 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Tree_Handle -> con -> uid); | |
692 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 3; | |
693 | ||
694 | SSVAL(SMB_Hdr(pkt), SMB_creat_atr_offset, search); | |
695 | SSVAL(SMB_Hdr(pkt), SMB_creat_tim_offset, 0); | |
696 | SSVAL(SMB_Hdr(pkt), SMB_creat_dat_offset, 0); | |
697 | SSVAL(SMB_Hdr(pkt), SMB_creat_bcc_offset, param_len); | |
698 | ||
699 | /* Now plug in the file name ... */ | |
700 | ||
701 | p = (char *)(SMB_Hdr(pkt) + SMB_creat_buf_offset); | |
702 | *p = SMBasciiID; | |
703 | strcpy(p+1, file_name); | |
704 | p = p + strlen(file_name); | |
705 | *(p+1) = 0; /* plug in a null ... */ | |
706 | ||
707 | /* Now send the packet and get the response ... */ | |
708 | ||
709 | if (RFCNB_Send(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
710 | ||
711 | #ifdef DEBUG | |
712 | fprintf(stderr, "Error sending Open request\n"); | |
713 | #endif | |
714 | ||
715 | if (File_Handle == NULL) | |
716 | free(file_tmp); | |
717 | RFCNB_Free_Pkt(pkt); | |
718 | SMBlib_errno = -SMBlibE_SendFailed; | |
719 | return(NULL); | |
720 | ||
721 | } | |
722 | ||
723 | /* Now get the response ... */ | |
724 | ||
725 | #ifdef DEBUG | |
726 | fprintf(stderr, "Pkt_Len for Create resp = %i\n", pkt_len); | |
727 | #endif | |
728 | ||
729 | if (RFCNB_Recv(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
730 | ||
731 | #ifdef DEBUG | |
732 | fprintf(stderr, "Error receiving response to create request\n"); | |
733 | #endif | |
734 | ||
735 | if (File_Handle == NULL) | |
736 | free(file_tmp); | |
737 | RFCNB_Free_Pkt(pkt); | |
738 | SMBlib_errno = -SMBlibE_RecvFailed; | |
739 | return(NULL); | |
740 | ||
741 | } | |
742 | ||
743 | /* Now parse the response and pass back any error ... */ | |
744 | ||
745 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
746 | ||
747 | #ifdef DEBUG | |
748 | fprintf(stderr, "SMB_Create failed with errorclass = %i, Error Code = %i\n", | |
749 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
750 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
751 | #endif | |
752 | ||
753 | if (File_Handle == NULL) | |
754 | free(file_tmp); | |
755 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
756 | RFCNB_Free_Pkt(pkt); | |
757 | SMBlib_errno = SMBlibE_Remote; | |
758 | return(NULL); /* Should clean up ... */ | |
759 | ||
760 | } | |
761 | ||
762 | file_tmp -> fid = SVAL(SMB_Hdr(pkt), SMB_creatr_fid_offset); | |
763 | file_tmp -> lastmod = 0; | |
764 | file_tmp -> size = 0; | |
765 | file_tmp -> access = SMB_AMODE_OPENRW; | |
766 | file_tmp -> fileloc = 0; | |
767 | ||
768 | RFCNB_Free_Pkt(pkt); /* Free up this space */ | |
769 | ||
770 | #ifdef DEBUG | |
771 | fprintf(stderr, "SMB_Create succeeded, FID = %i\n", file_tmp -> fid); | |
772 | #endif | |
773 | ||
774 | return(file_tmp); | |
775 | ||
776 | } | |
777 | ||
778 | /* Delete the file passed in as file_name. */ | |
779 | ||
780 | int SMB_Delete(SMB_Tree_Handle tree, char *file_name, WORD search) | |
781 | ||
782 | { | |
783 | struct RFCNB_Pkt *pkt; | |
784 | int pkt_len, param_len; | |
785 | char *p; | |
786 | ||
787 | param_len = strlen(file_name) + 2; | |
788 | pkt_len = SMB_delet_len + param_len; | |
789 | ||
790 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
791 | ||
792 | if (pkt == NULL) { /* Really should do some error handling */ | |
793 | ||
794 | SMBlib_errno = SMBlibE_NoSpace; | |
795 | return(SMBlibE_BAD); | |
796 | ||
797 | } | |
798 | ||
799 | /* Now plug in the bits we need */ | |
800 | ||
0ab648c9 | 801 | memset(SMB_Hdr(pkt), 0, SMB_delet_len); |
7c16470c AJ |
802 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
803 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBunlink; | |
804 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, tree -> con -> pid); | |
805 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, tree -> tid); | |
806 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, tree -> con -> mid); | |
807 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, tree -> con -> uid); | |
808 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 1; | |
809 | ||
810 | SIVAL(SMB_Hdr(pkt), SMB_delet_sat_offset, search); | |
811 | SSVAL(SMB_Hdr(pkt), SMB_delet_bcc_offset, param_len); | |
812 | ||
813 | /* Now plug in the file name ... */ | |
814 | ||
815 | p = (char *)(SMB_Hdr(pkt) + SMB_delet_buf_offset); | |
816 | *p = SMBasciiID; | |
817 | strcpy(p+1, file_name); | |
818 | p = p + strlen(file_name); | |
819 | *(p+1) = 0; /* plug in a null ... */ | |
820 | ||
821 | /* Now send the packet and get the response ... */ | |
822 | ||
823 | if (RFCNB_Send(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
824 | ||
825 | #ifdef DEBUG | |
826 | fprintf(stderr, "Error sending Delete request\n"); | |
827 | #endif | |
828 | ||
829 | RFCNB_Free_Pkt(pkt); | |
830 | SMBlib_errno = -SMBlibE_SendFailed; | |
831 | return(SMBlibE_BAD); | |
832 | ||
833 | } | |
834 | ||
835 | /* Now get the response ... */ | |
836 | ||
837 | if (RFCNB_Recv(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
838 | ||
839 | #ifdef DEBUG | |
840 | fprintf(stderr, "Error receiving response to delete request\n"); | |
841 | #endif | |
842 | ||
843 | RFCNB_Free_Pkt(pkt); | |
844 | SMBlib_errno = -SMBlibE_RecvFailed; | |
845 | return(SMBlibE_BAD); | |
846 | ||
847 | } | |
848 | ||
849 | /* Now parse the response and pass back any error ... */ | |
850 | ||
851 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
852 | ||
853 | #ifdef DEBUG | |
854 | fprintf(stderr, "SMB_Delete failed with errorclass = %i, Error Code = %i\n", | |
855 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
856 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
857 | #endif | |
858 | ||
859 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
860 | RFCNB_Free_Pkt(pkt); | |
861 | SMBlib_errno = SMBlibE_Remote; | |
862 | return(SMBlibE_BAD); /* Should clean up ... */ | |
863 | ||
864 | } | |
865 | ||
866 | #ifdef DEBUG | |
867 | fprintf(stderr, "File %s deleted successfully.\n", file_name); | |
868 | #endif DEBUG | |
869 | ||
870 | RFCNB_Free_Pkt(pkt); | |
871 | ||
872 | return(0); | |
873 | } | |
874 | ||
875 | /* Create the directory passed in as dir_name */ | |
876 | ||
877 | int SMB_Create_Dir(SMB_Tree_Handle tree, char *dir_name) | |
878 | ||
879 | { | |
880 | struct RFCNB_Pkt *pkt; | |
881 | int pkt_len, param_len; | |
882 | char *p; | |
883 | ||
884 | param_len = strlen(dir_name) + 2; /* + null and + asciiID */ | |
885 | pkt_len = SMB_creatdir_len + param_len; | |
886 | ||
887 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
888 | ||
889 | if (pkt == NULL) { /* Really should do some error handling */ | |
890 | ||
891 | SMBlib_errno = SMBlibE_NoSpace; | |
892 | return(SMBlibE_BAD); | |
893 | ||
894 | } | |
895 | ||
896 | /* Now plug in the bits we need */ | |
897 | ||
0ab648c9 | 898 | memset(SMB_Hdr(pkt), 0, SMB_creatdir_len); |
7c16470c AJ |
899 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
900 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBmkdir; | |
901 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, tree -> con -> pid); | |
902 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, tree -> tid); | |
903 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, tree -> con -> mid); | |
904 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, tree -> con -> uid); | |
905 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0; | |
906 | ||
907 | SSVAL(SMB_Hdr(pkt), SMB_creatdir_bcc_offset, param_len); | |
908 | ||
909 | /* Now plug in the file name ... */ | |
910 | ||
911 | p = (char *)(SMB_Hdr(pkt) + SMB_creatdir_buf_offset); | |
912 | *p = SMBasciiID; | |
913 | strcpy(p+1, dir_name); | |
914 | p = p + strlen(dir_name); | |
915 | *(p+1) = 0; /* plug in a null ... */ | |
916 | ||
917 | /* Now send the packet and get the response ... */ | |
918 | ||
919 | if (RFCNB_Send(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
920 | ||
921 | #ifdef DEBUG | |
922 | fprintf(stderr, "Error sending Create Dir request\n"); | |
923 | #endif | |
924 | ||
925 | RFCNB_Free_Pkt(pkt); | |
926 | SMBlib_errno = -SMBlibE_SendFailed; | |
927 | return(SMBlibE_BAD); | |
928 | ||
929 | } | |
930 | ||
931 | /* Now get the response ... */ | |
932 | ||
933 | if (RFCNB_Recv(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
934 | ||
935 | #ifdef DEBUG | |
936 | fprintf(stderr, "Error receiving response to Create Dir request\n"); | |
937 | #endif | |
938 | ||
939 | RFCNB_Free_Pkt(pkt); | |
940 | SMBlib_errno = -SMBlibE_RecvFailed; | |
941 | return(SMBlibE_BAD); | |
942 | ||
943 | } | |
944 | ||
945 | /* Now parse the response and pass back any error ... */ | |
946 | ||
947 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
948 | ||
949 | #ifdef DEBUG | |
950 | fprintf(stderr, "SMB_Create_Dir failed with errorclass = %i, Error Code = %i\n", | |
951 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
952 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
953 | #endif | |
954 | ||
955 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
956 | RFCNB_Free_Pkt(pkt); | |
957 | SMBlib_errno = SMBlibE_Remote; | |
958 | return(SMBlibE_BAD); /* Should clean up ... */ | |
959 | ||
960 | } | |
961 | ||
962 | #ifdef DEBUG | |
963 | fprintf(stderr, "Directory %s created successfully.\n", dir_name); | |
964 | #endif DEBUG | |
965 | ||
966 | RFCNB_Free_Pkt(pkt); | |
967 | ||
968 | return(0); | |
969 | } | |
970 | ||
971 | /* Delete the directory passed as dir_name, as long as it is empty ... */ | |
972 | ||
973 | int SMB_Delete_Dir(SMB_Tree_Handle tree, char *dir_name) | |
974 | ||
975 | { | |
976 | struct RFCNB_Pkt *pkt; | |
977 | int pkt_len, param_len; | |
978 | char *p; | |
979 | ||
980 | param_len = strlen(dir_name) + 2; /* + null and + asciiID */ | |
981 | pkt_len = SMB_deletdir_len + param_len; | |
982 | ||
983 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
984 | ||
985 | if (pkt == NULL) { /* Really should do some error handling */ | |
986 | ||
987 | SMBlib_errno = SMBlibE_NoSpace; | |
988 | return(SMBlibE_BAD); | |
989 | ||
990 | } | |
991 | ||
992 | /* Now plug in the bits we need */ | |
993 | ||
0ab648c9 | 994 | memset(SMB_Hdr(pkt), 0, SMB_deletdir_len); |
7c16470c AJ |
995 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
996 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBrmdir; | |
997 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, tree -> con -> pid); | |
998 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, tree -> tid); | |
999 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, tree -> con -> mid); | |
1000 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, tree -> con -> uid); | |
1001 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0; | |
1002 | ||
1003 | SSVAL(SMB_Hdr(pkt), SMB_deletdir_bcc_offset, param_len); | |
1004 | ||
1005 | /* Now plug in the file name ... */ | |
1006 | ||
1007 | p = (char *)(SMB_Hdr(pkt) + SMB_deletdir_buf_offset); | |
1008 | *p = SMBasciiID; | |
1009 | strcpy(p+1, dir_name); | |
1010 | p = p + strlen(dir_name); | |
1011 | *(p+1) = 0; /* plug in a null ... */ | |
1012 | ||
1013 | /* Now send the packet and get the response ... */ | |
1014 | ||
1015 | if (RFCNB_Send(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
1016 | ||
1017 | #ifdef DEBUG | |
1018 | fprintf(stderr, "Error sending Delete Dir request\n"); | |
1019 | #endif | |
1020 | ||
1021 | RFCNB_Free_Pkt(pkt); | |
1022 | SMBlib_errno = -SMBlibE_SendFailed; | |
1023 | return(SMBlibE_BAD); | |
1024 | ||
1025 | } | |
1026 | ||
1027 | /* Now get the response ... */ | |
1028 | ||
1029 | if (RFCNB_Recv(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
1030 | ||
1031 | #ifdef DEBUG | |
1032 | fprintf(stderr, "Error receiving response to Delete Dir request\n"); | |
1033 | #endif | |
1034 | ||
1035 | RFCNB_Free_Pkt(pkt); | |
1036 | SMBlib_errno = -SMBlibE_RecvFailed; | |
1037 | return(SMBlibE_BAD); | |
1038 | ||
1039 | } | |
1040 | ||
1041 | /* Now parse the response and pass back any error ... */ | |
1042 | ||
1043 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
1044 | ||
1045 | #ifdef DEBUG | |
1046 | fprintf(stderr, "SMB_Delete_Dir failed with errorclass = %i, Error Code = %i\n", | |
1047 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
1048 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
1049 | #endif | |
1050 | ||
1051 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
1052 | RFCNB_Free_Pkt(pkt); | |
1053 | SMBlib_errno = SMBlibE_Remote; | |
1054 | return(SMBlibE_BAD); /* Should clean up ... */ | |
1055 | ||
1056 | } | |
1057 | ||
1058 | #ifdef DEBUG | |
1059 | fprintf(stderr, "Directory %s deleted successfully.\n", dir_name); | |
1060 | #endif DEBUG | |
1061 | ||
1062 | RFCNB_Free_Pkt(pkt); | |
1063 | ||
1064 | return(0); | |
1065 | } | |
1066 | ||
1067 | /* Check for the existence of the directory in dir_name */ | |
1068 | ||
1069 | int SMB_Check_Dir(SMB_Tree_Handle tree, char *dir_name) | |
1070 | ||
1071 | { | |
1072 | struct RFCNB_Pkt *pkt; | |
1073 | int pkt_len, param_len; | |
1074 | char *p; | |
1075 | ||
1076 | param_len = strlen(dir_name) + 2; /* + null and + asciiID */ | |
1077 | pkt_len = SMB_checkdir_len + param_len; | |
1078 | ||
1079 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
1080 | ||
1081 | if (pkt == NULL) { /* Really should do some error handling */ | |
1082 | ||
1083 | SMBlib_errno = SMBlibE_NoSpace; | |
1084 | return(SMBlibE_BAD); | |
1085 | ||
1086 | } | |
1087 | ||
1088 | /* Now plug in the bits we need */ | |
1089 | ||
0ab648c9 | 1090 | memset(SMB_Hdr(pkt), 0, SMB_checkdir_len); |
7c16470c AJ |
1091 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
1092 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBchkpth; | |
1093 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, tree -> con -> pid); | |
1094 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, tree -> tid); | |
1095 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, tree -> con -> mid); | |
1096 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, tree -> con -> uid); | |
1097 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0; | |
1098 | ||
1099 | SSVAL(SMB_Hdr(pkt), SMB_checkdir_bcc_offset, param_len); | |
1100 | ||
1101 | /* Now plug in the file name ... */ | |
1102 | ||
1103 | p = (char *)(SMB_Hdr(pkt) + SMB_checkdir_buf_offset); | |
1104 | *p = SMBasciiID; | |
1105 | strcpy(p+1, dir_name); | |
1106 | p = p + strlen(dir_name); | |
1107 | *(p+1) = 0; /* plug in a null ... */ | |
1108 | ||
1109 | /* Now send the packet and get the response ... */ | |
1110 | ||
1111 | if (RFCNB_Send(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
1112 | ||
1113 | #ifdef DEBUG | |
1114 | fprintf(stderr, "Error sending Check Dir Path request\n"); | |
1115 | #endif | |
1116 | ||
1117 | RFCNB_Free_Pkt(pkt); | |
1118 | SMBlib_errno = -SMBlibE_SendFailed; | |
1119 | return(SMBlibE_BAD); | |
1120 | ||
1121 | } | |
1122 | ||
1123 | /* Now get the response ... */ | |
1124 | ||
1125 | if (RFCNB_Recv(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
1126 | ||
1127 | #ifdef DEBUG | |
1128 | fprintf(stderr, "Error receiving response to Check Dir request\n"); | |
1129 | #endif | |
1130 | ||
1131 | RFCNB_Free_Pkt(pkt); | |
1132 | SMBlib_errno = -SMBlibE_RecvFailed; | |
1133 | return(SMBlibE_BAD); | |
1134 | ||
1135 | } | |
1136 | ||
1137 | /* Now parse the response and pass back any error ... */ | |
1138 | ||
1139 | if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
1140 | ||
1141 | #ifdef DEBUG | |
1142 | fprintf(stderr, "SMB_Check_Dir failed with errorclass = %i, Error Code = %i\n", | |
1143 | CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset), | |
1144 | SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset)); | |
1145 | #endif | |
1146 | ||
1147 | SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset); | |
1148 | RFCNB_Free_Pkt(pkt); | |
1149 | SMBlib_errno = SMBlibE_Remote; | |
1150 | return(SMBlibE_BAD); /* Should clean up ... */ | |
1151 | ||
1152 | } | |
1153 | ||
1154 | #ifdef DEBUG | |
1155 | fprintf(stderr, "Directory %s checked successfully.\n", dir_name); | |
1156 | #endif DEBUG | |
1157 | ||
1158 | RFCNB_Free_Pkt(pkt); | |
1159 | ||
1160 | return(0); | |
1161 | } | |
1162 | ||
1163 | /* Search directory for the files listed ... Relative to the TID in the */ | |
1164 | /* Con Handle. Return number of Dir Ents returned as the result. */ | |
1165 | ||
1166 | int SMB_Search(SMB_Tree_Handle tree, | |
1167 | char *dir_name, | |
1168 | WORD search, | |
1169 | SMB_CP_dirent *dirents, | |
1170 | int direntc, | |
1171 | char *resumekey, | |
1172 | int resumekey_len) | |
1173 | ||
1174 | { | |
1175 | struct RFCNB_Pkt *pkt, *recv_pkt; | |
1176 | int pkt_len, param_len, recv_param_len, recv_pkt_len, ret_count, i; | |
1177 | char *p; | |
1178 | ||
1179 | param_len = strlen(dir_name) + 2 + resumekey_len + 3; /* You have to know */ | |
1180 | pkt_len = SMB_search_len + param_len; | |
1181 | ||
1182 | recv_param_len = direntc * SMB_searchr_dirent_len + 3; | |
1183 | recv_pkt_len = SMB_searchr_len + recv_param_len; | |
1184 | ||
1185 | pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len); | |
1186 | ||
1187 | if (pkt == NULL) { /* Really should do some error handling */ | |
1188 | ||
1189 | SMBlib_errno = SMBlibE_NoSpace; | |
1190 | return(SMBlibE_BAD); | |
1191 | ||
1192 | } | |
1193 | ||
1194 | recv_pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(recv_pkt_len); | |
1195 | ||
1196 | if (recv_pkt == NULL) { /* Really should do some error handling */ | |
1197 | ||
1198 | RFCNB_Free_Pkt(pkt); | |
1199 | SMBlib_errno = SMBlibE_NoSpace; | |
1200 | return(SMBlibE_BAD); | |
1201 | ||
1202 | } | |
1203 | ||
7c16470c AJ |
1204 | /* Now plug in the bits we need */ |
1205 | ||
0ab648c9 | 1206 | memset(SMB_Hdr(pkt), 0, SMB_search_len); |
7c16470c AJ |
1207 | SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */ |
1208 | *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBsearch; | |
1209 | SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, tree -> con -> pid); | |
1210 | SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, tree -> tid); | |
1211 | SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, tree -> con -> mid); | |
1212 | SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, tree -> con -> uid); | |
1213 | ||
2f8abb64 | 1214 | /* Tell server we known about non-dos names and extended attributes */ |
7c16470c AJ |
1215 | |
1216 | SSVAL(SMB_Hdr(pkt), SMB_hdr_flg2_offset, | |
1217 | (SMB_FLG2_NON_DOS | SMB_FLG2_EXT_ATR)); | |
1218 | ||
1219 | *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 2; | |
1220 | ||
1221 | SSVAL(SMB_Hdr(pkt), SMB_search_mdc_offset, direntc); /* How many we want */ | |
1222 | SSVAL(SMB_Hdr(pkt), SMB_search_atr_offset, search); | |
1223 | SSVAL(SMB_Hdr(pkt), SMB_search_bcc_offset, param_len); | |
1224 | ||
1225 | /* Now plug in the file name ... */ | |
1226 | ||
1227 | p = (char *)(SMB_Hdr(pkt) + SMB_search_buf_offset); | |
1228 | *p = SMBasciiID; | |
1229 | strcpy(p+1, dir_name); | |
1230 | p = p + strlen(dir_name) + 2; /* Skip the null */ | |
1231 | ||
7c16470c AJ |
1232 | *p = SMBvariableblockID; |
1233 | p = p + 1; | |
1234 | ||
1235 | /* And now the resume key */ | |
1236 | ||
1237 | SSVAL(p, 0, resumekey_len); | |
1238 | ||
1239 | p = p + 2; | |
1240 | ||
1241 | bcopy(resumekey, p, resumekey_len); | |
1242 | ||
1243 | /* Now send the packet and get the response ... */ | |
1244 | ||
1245 | if (RFCNB_Send(tree -> con -> Trans_Connect, pkt, pkt_len) < 0) { | |
1246 | ||
1247 | #ifdef DEBUG | |
1248 | fprintf(stderr, "Error sending search request\n"); | |
1249 | #endif | |
1250 | ||
1251 | RFCNB_Free_Pkt(pkt); | |
1252 | RFCNB_Free_Pkt(recv_pkt); | |
1253 | SMBlib_errno = -SMBlibE_SendFailed; | |
1254 | return(SMBlibE_BAD); | |
1255 | ||
1256 | } | |
1257 | ||
1258 | /* Now get the response ... */ | |
1259 | ||
1260 | if (RFCNB_Recv(tree -> con -> Trans_Connect, recv_pkt, recv_pkt_len) < 0) { | |
1261 | ||
1262 | #ifdef DEBUG | |
1263 | fprintf(stderr, "Error receiving response to Check Dir request\n"); | |
1264 | #endif | |
1265 | ||
1266 | RFCNB_Free_Pkt(pkt); | |
1267 | RFCNB_Free_Pkt(recv_pkt); | |
1268 | SMBlib_errno = -SMBlibE_RecvFailed; | |
1269 | return(SMBlibE_BAD); | |
1270 | ||
1271 | } | |
1272 | ||
1273 | /* Now parse the response and pass back any error ... */ | |
1274 | ||
1275 | if (CVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */ | |
1276 | ||
1277 | #ifdef DEBUG | |
1278 | fprintf(stderr, "SMB_Check_Dir failed with errorclass = %i, Error Code = %i\n", | |
1279 | CVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset), | |
1280 | SVAL(SMB_Hdr(recv_pkt), SMB_hdr_err_offset)); | |
1281 | #endif | |
1282 | ||
1283 | SMBlib_SMB_Error = IVAL(SMB_Hdr(recv_pkt), SMB_hdr_rcls_offset); | |
1284 | RFCNB_Free_Pkt(pkt); | |
1285 | RFCNB_Free_Pkt(recv_pkt); | |
1286 | SMBlib_errno = SMBlibE_Remote; | |
1287 | return(SMBlibE_BAD); /* Should clean up ... */ | |
1288 | ||
1289 | } | |
1290 | ||
1291 | /* Now copy the results into the user's structure */ | |
1292 | ||
1293 | ret_count = SVAL(SMB_Hdr(recv_pkt), SMB_searchr_dec_offset); | |
1294 | ||
1295 | p = SMB_Hdr(recv_pkt) + SMB_searchr_buf_offset + 3; | |
1296 | ||
1297 | /* Hmmm, should check that we have the right number of bytes ... */ | |
1298 | ||
1299 | for (i = 0; i < ret_count; i++) { | |
1300 | ||
1301 | bcopy(p, dirents[i].resume_key, 21); | |
1302 | ||
1303 | p = p + 21; | |
1304 | ||
1305 | dirents[i].file_attributes = (unsigned char)*p; | |
1306 | ||
1307 | p = p + 1; | |
1308 | ||
1309 | dirents[i].date_time = IVAL(p, 0); /* Should this be IVAL? */ | |
1310 | ||
1311 | p = p + 4; | |
1312 | ||
1313 | dirents[i].size = IVAL(p, 0); | |
1314 | ||
1315 | p = p + 4; | |
1316 | ||
1317 | bcopy(p, dirents[i].filename, 13); /* Copy in file name */ | |
1318 | ||
1319 | p = p + 13; | |
1320 | ||
1321 | } | |
1322 | ||
1323 | return(ret_count); | |
1324 | ||
1325 | } | |
f53969cc | 1326 |