Convert receive_smb_raw to NTSTATUS
[tprouty/samba.git] / source3 / libsmb / clientgen.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client generic functions
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2007.
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 /*******************************************************************
24  Setup the word count and byte count for a client smb message.
25 ********************************************************************/
26
27 int cli_set_message(char *buf,int num_words,int num_bytes,bool zero)
28 {
29         if (zero && (num_words || num_bytes)) {
30                 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
31         }
32         SCVAL(buf,smb_wct,num_words);
33         SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
34         smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
35         return (smb_size + num_words*2 + num_bytes);
36 }
37
38 /****************************************************************************
39  Change the timeout (in milliseconds).
40 ****************************************************************************/
41
42 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
43 {
44         unsigned int old_timeout = cli->timeout;
45         cli->timeout = timeout;
46         return old_timeout;
47 }
48
49 /****************************************************************************
50  Change the port number used to call on.
51 ****************************************************************************/
52
53 int cli_set_port(struct cli_state *cli, int port)
54 {
55         cli->port = port;
56         return port;
57 }
58
59 /****************************************************************************
60  Read an smb from a fd ignoring all keepalive packets. Note that the buffer 
61  *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
62  The timeout is in milliseconds
63
64  This is exactly the same as receive_smb except that it never returns
65  a session keepalive packet (just as receive_smb used to do).
66  receive_smb was changed to return keepalives as the oplock processing means this call
67  should never go into a blocking read.
68 ****************************************************************************/
69
70 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
71 {
72         size_t len;
73
74         for(;;) {
75                 NTSTATUS status;
76
77                 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
78
79                 status = receive_smb_raw(cli->fd, cli->inbuf, cli->timeout,
80                                          maxlen, &len);
81                 if (!NT_STATUS_IS_OK(status)) {
82                         DEBUG(10,("client_receive_smb failed\n"));
83                         show_msg(cli->inbuf);
84
85                         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
86                                 set_smb_read_error(&cli->smb_rw_error,
87                                                    SMB_READ_EOF);
88                                 return -1;
89                         }
90
91                         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
92                                 set_smb_read_error(&cli->smb_rw_error,
93                                                    SMB_READ_TIMEOUT);
94                                 return -1;
95                         }
96
97                         set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
98                         return -1;
99                 }
100
101                 if (len < 0) {
102                         return len;
103                 }
104
105                 /* Ignore session keepalive packets. */
106                 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
107                         break;
108                 }
109         }
110
111         if (cli_encryption_on(cli)) {
112                 NTSTATUS status = cli_decrypt_message(cli);
113                 if (!NT_STATUS_IS_OK(status)) {
114                         DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
115                                 nt_errstr(status)));
116                         cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
117                         return -1;
118                 }
119         }
120
121         show_msg(cli->inbuf);
122         return len;
123 }
124
125 /****************************************************************************
126  Recv an smb.
127 ****************************************************************************/
128
129 bool cli_receive_smb(struct cli_state *cli)
130 {
131         ssize_t len;
132
133         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
134         if (cli->fd == -1)
135                 return false; 
136
137  again:
138         len = client_receive_smb(cli, 0);
139         
140         if (len > 0) {
141                 /* it might be an oplock break request */
142                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
143                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
144                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
145                     SVAL(cli->inbuf,smb_vwv7) == 0) {
146                         if (cli->oplock_handler) {
147                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
148                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
149                                 if (!cli->oplock_handler(cli, fnum, level)) {
150                                         return false;
151                                 }
152                         }
153                         /* try to prevent loops */
154                         SCVAL(cli->inbuf,smb_com,0xFF);
155                         goto again;
156                 }
157         }
158
159         /* If the server is not responding, note that now */
160         if (len < 0) {
161                 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
162                 close(cli->fd);
163                 cli->fd = -1;
164                 return false;
165         }
166
167         if (!cli_check_sign_mac(cli)) {
168                 /*
169                  * If we get a signature failure in sessionsetup, then
170                  * the server sometimes just reflects the sent signature
171                  * back to us. Detect this and allow the upper layer to
172                  * retrieve the correct Windows error message.
173                  */
174                 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
175                         (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
176                         (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
177                         memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
178                         cli_is_error(cli)) {
179
180                         /*
181                          * Reflected signature on login error. 
182                          * Set bad sig but don't close fd.
183                          */
184                         cli->smb_rw_error = SMB_READ_BAD_SIG;
185                         return true;
186                 }
187
188                 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
189                 cli->smb_rw_error = SMB_READ_BAD_SIG;
190                 close(cli->fd);
191                 cli->fd = -1;
192                 return false;
193         };
194         return true;
195 }
196
197 /****************************************************************************
198  Read the data portion of a readX smb.
199  The timeout is in milliseconds
200 ****************************************************************************/
201
202 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
203 {
204         NTSTATUS status;
205
206         set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
207
208         status = read_socket_with_timeout(
209                 cli->fd, buffer, len, len, cli->timeout, NULL);
210         if (NT_STATUS_IS_OK(status)) {
211                 return len;
212         }
213
214         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
215                 set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
216                 return -1;
217         }
218
219         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
220                 set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
221                 return -1;
222         }
223
224         set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
225         return -1;
226 }
227
228 /****************************************************************************
229  Read a smb readX header.
230  We can only use this if encryption and signing are off.
231 ****************************************************************************/
232
233 bool cli_receive_smb_readX_header(struct cli_state *cli)
234 {
235         ssize_t len, offset;
236
237         if (cli->fd == -1)
238                 return false; 
239
240  again:
241
242         /* Read up to the size of a readX header reply. */
243         len = client_receive_smb(cli, (smb_size - 4) + 24);
244         
245         if (len > 0) {
246                 /* it might be an oplock break request */
247                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
248                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
249                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
250                     SVAL(cli->inbuf,smb_vwv7) == 0) {
251                         ssize_t total_len = smb_len(cli->inbuf);
252
253                         if (total_len > CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN) {
254                                 goto read_err;
255                         }
256
257                         /* Read the rest of the data. */
258                         if ((total_len - len > 0) &&
259                             !cli_receive_smb_data(cli,cli->inbuf+len,total_len - len)) {
260                                 goto read_err;
261                         }
262
263                         if (cli->oplock_handler) {
264                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
265                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
266                                 if (!cli->oplock_handler(cli, fnum, level)) return false;
267                         }
268                         /* try to prevent loops */
269                         SCVAL(cli->inbuf,smb_com,0xFF);
270                         goto again;
271                 }
272         }
273
274         /* If it's not the above size it probably was an error packet. */
275
276         if ((len == (smb_size - 4) + 24) && !cli_is_error(cli)) {
277                 /* Check it's a non-chained readX reply. */
278                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) ||
279                         (CVAL(cli->inbuf,smb_vwv0) != 0xFF) ||
280                         (CVAL(cli->inbuf,smb_com) != SMBreadX)) {
281                         /* 
282                          * We're not coping here with asnyc replies to
283                          * other calls. Punt here - we need async client
284                          * libs for this.
285                          */
286                         goto read_err;
287                 }
288
289                 /* 
290                  * We know it's a readX reply - ensure we've read the
291                  * padding bytes also.
292                  */
293
294                 offset = SVAL(cli->inbuf,smb_vwv6);
295                 if (offset > len) {
296                         ssize_t ret;
297                         size_t padbytes = offset - len;
298                         ret = cli_receive_smb_data(cli,smb_buf(cli->inbuf),padbytes);
299                         if (ret != padbytes) {
300                                 goto read_err;
301                         }
302                 }
303         }
304
305         return true;
306
307   read_err:
308
309         cli->smb_rw_error = SMB_READ_ERROR;
310         close(cli->fd);
311         cli->fd = -1;
312         return false;
313 }
314
315 static ssize_t write_socket(int fd, const char *buf, size_t len)
316 {
317         ssize_t ret=0;
318
319         DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
320         ret = write_data(fd,buf,len);
321
322         DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
323         if(ret <= 0)
324                 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
325                         (int)len, fd, strerror(errno) ));
326
327         return(ret);
328 }
329
330 /****************************************************************************
331  Send an smb to a fd.
332 ****************************************************************************/
333
334 bool cli_send_smb(struct cli_state *cli)
335 {
336         size_t len;
337         size_t nwritten=0;
338         ssize_t ret;
339         char *buf_out = cli->outbuf;
340         bool enc_on = cli_encryption_on(cli);
341
342         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
343         if (cli->fd == -1)
344                 return false;
345
346         cli_calculate_sign_mac(cli);
347
348         if (enc_on) {
349                 NTSTATUS status = cli_encrypt_message(cli, &buf_out);
350                 if (!NT_STATUS_IS_OK(status)) {
351                         close(cli->fd);
352                         cli->fd = -1;
353                         cli->smb_rw_error = SMB_WRITE_ERROR;
354                         DEBUG(0,("Error in encrypting client message. Error %s\n",
355                                 nt_errstr(status) ));
356                         return false;
357                 }
358         }
359
360         len = smb_len(buf_out) + 4;
361
362         while (nwritten < len) {
363                 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
364                 if (ret <= 0) {
365                         if (enc_on) {
366                                 cli_free_enc_buffer(cli, buf_out);
367                         }
368                         close(cli->fd);
369                         cli->fd = -1;
370                         cli->smb_rw_error = SMB_WRITE_ERROR;
371                         DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
372                                 (int)len,(int)ret, strerror(errno) ));
373                         return false;
374                 }
375                 nwritten += ret;
376         }
377
378         if (enc_on) {
379                 cli_free_enc_buffer(cli, buf_out);
380         }
381
382         /* Increment the mid so we can tell between responses. */
383         cli->mid++;
384         if (!cli->mid)
385                 cli->mid++;
386         return true;
387 }
388
389 /****************************************************************************
390  Send a "direct" writeX smb to a fd.
391 ****************************************************************************/
392
393 bool cli_send_smb_direct_writeX(struct cli_state *cli,
394                                 const char *p,
395                                 size_t extradata)
396 {
397         /* First length to send is the offset to the data. */
398         size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
399         size_t nwritten=0;
400         ssize_t ret;
401
402         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
403         if (cli->fd == -1) {
404                 return false;
405         }
406
407         if (client_is_signing_on(cli)) {
408                 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
409                 return false;
410         }
411
412         while (nwritten < len) {
413                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
414                 if (ret <= 0) {
415                         close(cli->fd);
416                         cli->fd = -1;
417                         cli->smb_rw_error = SMB_WRITE_ERROR;
418                         DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
419                                 (int)len,(int)ret, strerror(errno) ));
420                         return false;
421                 }
422                 nwritten += ret;
423         }
424
425         /* Now write the extra data. */
426         nwritten=0;
427         while (nwritten < extradata) {
428                 ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
429                 if (ret <= 0) {
430                         close(cli->fd);
431                         cli->fd = -1;
432                         cli->smb_rw_error = SMB_WRITE_ERROR;
433                         DEBUG(0,("Error writing %d extradata "
434                                 "bytes to client. %d (%s)\n",
435                                 (int)extradata,(int)ret, strerror(errno) ));
436                         return false;
437                 }
438                 nwritten += ret;
439         }
440
441         /* Increment the mid so we can tell between responses. */
442         cli->mid++;
443         if (!cli->mid)
444                 cli->mid++;
445         return true;
446 }
447
448 /****************************************************************************
449  Setup basics in a outgoing packet.
450 ****************************************************************************/
451
452 void cli_setup_packet(struct cli_state *cli)
453 {
454         cli->rap_error = 0;
455         SSVAL(cli->outbuf,smb_pid,cli->pid);
456         SSVAL(cli->outbuf,smb_uid,cli->vuid);
457         SSVAL(cli->outbuf,smb_mid,cli->mid);
458         if (cli->protocol > PROTOCOL_CORE) {
459                 uint16 flags2;
460                 if (cli->case_sensitive) {
461                         SCVAL(cli->outbuf,smb_flg,0x0);
462                 } else {
463                         /* Default setting, case insensitive. */
464                         SCVAL(cli->outbuf,smb_flg,0x8);
465                 }
466                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
467                 if (cli->capabilities & CAP_UNICODE)
468                         flags2 |= FLAGS2_UNICODE_STRINGS;
469                 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
470                         flags2 |= FLAGS2_DFS_PATHNAMES;
471                 if (cli->capabilities & CAP_STATUS32)
472                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
473                 if (cli->use_spnego)
474                         flags2 |= FLAGS2_EXTENDED_SECURITY;
475                 SSVAL(cli->outbuf,smb_flg2, flags2);
476         }
477 }
478
479 /****************************************************************************
480  Setup the bcc length of the packet from a pointer to the end of the data.
481 ****************************************************************************/
482
483 void cli_setup_bcc(struct cli_state *cli, void *p)
484 {
485         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
486 }
487
488 /****************************************************************************
489  Initialise credentials of a client structure.
490 ****************************************************************************/
491
492 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
493 {
494         fstrcpy(cli->domain, domain);
495         fstrcpy(cli->user_name, username);
496         pwd_set_cleartext(&cli->pwd, password);
497         if (!*username) {
498                 cli->pwd.null_pwd = true;
499         }
500
501         DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
502 }
503
504 /****************************************************************************
505  Set the signing state (used from the command line).
506 ****************************************************************************/
507
508 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
509 {
510         if (signing_state == Undefined)
511                 return;
512
513         if (signing_state == false) {
514                 cli->sign_info.allow_smb_signing = false;
515                 cli->sign_info.mandatory_signing = false;
516                 return;
517         }
518
519         cli->sign_info.allow_smb_signing = true;
520
521         if (signing_state == Required) 
522                 cli->sign_info.mandatory_signing = true;
523 }
524
525 /****************************************************************************
526  Initialise a client structure. Always returns a malloc'ed struct.
527 ****************************************************************************/
528
529 struct cli_state *cli_initialise(void)
530 {
531         struct cli_state *cli = NULL;
532
533         /* Check the effective uid - make sure we are not setuid */
534         if (is_setuid_root()) {
535                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
536                 return NULL;
537         }
538
539         cli = SMB_MALLOC_P(struct cli_state);
540         if (!cli) {
541                 return NULL;
542         }
543
544         ZERO_STRUCTP(cli);
545
546         cli->port = 0;
547         cli->fd = -1;
548         cli->cnum = -1;
549         cli->pid = (uint16)sys_getpid();
550         cli->mid = 1;
551         cli->vuid = UID_FIELD_INVALID;
552         cli->protocol = PROTOCOL_NT1;
553         cli->timeout = 20000; /* Timeout is in milliseconds. */
554         cli->bufsize = CLI_BUFFER_SIZE+4;
555         cli->max_xmit = cli->bufsize;
556         cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
557         cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
558         cli->oplock_handler = cli_oplock_ack;
559         cli->case_sensitive = false;
560         cli->smb_rw_error = SMB_READ_OK;
561
562         cli->use_spnego = lp_client_use_spnego();
563
564         cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
565
566         /* Set the CLI_FORCE_DOSERR environment variable to test
567            client routines using DOS errors instead of STATUS32
568            ones.  This intended only as a temporary hack. */    
569         if (getenv("CLI_FORCE_DOSERR"))
570                 cli->force_dos_errors = true;
571
572         if (lp_client_signing()) 
573                 cli->sign_info.allow_smb_signing = true;
574
575         if (lp_client_signing() == Required) 
576                 cli->sign_info.mandatory_signing = true;
577                                    
578         if (!cli->outbuf || !cli->inbuf)
579                 goto error;
580
581         memset(cli->outbuf, 0, cli->bufsize);
582         memset(cli->inbuf, 0, cli->bufsize);
583
584
585 #if defined(DEVELOPER)
586         /* just because we over-allocate, doesn't mean it's right to use it */
587         clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
588         clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
589 #endif
590
591         /* initialise signing */
592         cli_null_set_signing(cli);
593
594         cli->initialised = 1;
595
596         return cli;
597
598         /* Clean up after malloc() error */
599
600  error:
601
602         SAFE_FREE(cli->inbuf);
603         SAFE_FREE(cli->outbuf);
604         SAFE_FREE(cli);
605         return NULL;
606 }
607
608 /****************************************************************************
609  External interface.
610  Close an open named pipe over SMB. Free any authentication data.
611  Returns false if the cli_close call failed.
612  ****************************************************************************/
613
614 bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
615 {
616         bool ret;
617
618         if (!cli) {
619                 return false;
620         }
621
622         ret = cli_close(cli->cli, cli->fnum);
623
624         if (!ret) {
625                 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
626                          "fnum 0x%x "
627                          "to machine %s.  Error was %s\n",
628                          cli->pipe_name,
629                          (int) cli->fnum,
630                          cli->cli->desthost,
631                          cli_errstr(cli->cli)));
632         }
633
634         if (cli->auth.cli_auth_data_free_func) {
635                 (*cli->auth.cli_auth_data_free_func)(&cli->auth);
636         }
637
638         DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
639                 cli->pipe_name, cli->cli->desthost ));
640
641         DLIST_REMOVE(cli->cli->pipe_list, cli);
642         talloc_destroy(cli->mem_ctx);
643         return ret;
644 }
645
646 /****************************************************************************
647  Close all pipes open on this session.
648 ****************************************************************************/
649
650 void cli_nt_pipes_close(struct cli_state *cli)
651 {
652         struct rpc_pipe_client *cp, *next;
653
654         for (cp = cli->pipe_list; cp; cp = next) {
655                 next = cp->next;
656                 cli_rpc_pipe_close(cp);
657         }
658 }
659
660 /****************************************************************************
661  Shutdown a client structure.
662 ****************************************************************************/
663
664 void cli_shutdown(struct cli_state *cli)
665 {
666         cli_nt_pipes_close(cli);
667
668         /*
669          * tell our peer to free his resources.  Wihtout this, when an
670          * application attempts to do a graceful shutdown and calls
671          * smbc_free_context() to clean up all connections, some connections
672          * can remain active on the peer end, until some (long) timeout period
673          * later.  This tree disconnect forces the peer to clean up, since the
674          * connection will be going away.
675          *
676          * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
677          * the only user for this so far is smbmount which passes opened connection
678          * down to kernel's smbfs module.
679          */
680         if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
681                 cli_tdis(cli);
682         }
683         
684         SAFE_FREE(cli->outbuf);
685         SAFE_FREE(cli->inbuf);
686
687         cli_free_signing_context(cli);
688         data_blob_free(&cli->secblob);
689         data_blob_free(&cli->user_session_key);
690
691         if (cli->fd != -1) {
692                 close(cli->fd);
693         }
694         cli->fd = -1;
695         cli->smb_rw_error = SMB_READ_OK;
696
697         SAFE_FREE(cli);
698 }
699
700 /****************************************************************************
701  Set socket options on a open connection.
702 ****************************************************************************/
703
704 void cli_sockopt(struct cli_state *cli, const char *options)
705 {
706         set_socket_options(cli->fd, options);
707 }
708
709 /****************************************************************************
710  Set the PID to use for smb messages. Return the old pid.
711 ****************************************************************************/
712
713 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
714 {
715         uint16 ret = cli->pid;
716         cli->pid = pid;
717         return ret;
718 }
719
720 /****************************************************************************
721  Set the case sensitivity flag on the packets. Returns old state.
722 ****************************************************************************/
723
724 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
725 {
726         bool ret = cli->case_sensitive;
727         cli->case_sensitive = case_sensitive;
728         return ret;
729 }
730
731 /****************************************************************************
732 Send a keepalive packet to the server
733 ****************************************************************************/
734
735 bool cli_send_keepalive(struct cli_state *cli)
736 {
737         if (cli->fd == -1) {
738                 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
739                 return false;
740         }
741         if (!send_keepalive(cli->fd)) {
742                 close(cli->fd);
743                 cli->fd = -1;
744                 DEBUG(0,("Error sending keepalive packet to client.\n"));
745                 return false;
746         }
747         return true;
748 }
749
750 /****************************************************************************
751  Send/receive a SMBecho command: ping the server
752 ****************************************************************************/
753
754 bool cli_echo(struct cli_state *cli, uint16 num_echos,
755               unsigned char *data, size_t length)
756 {
757         char *p;
758         int i;
759
760         SMB_ASSERT(length < 1024);
761
762         memset(cli->outbuf,'\0',smb_size);
763         cli_set_message(cli->outbuf,1,length,true);
764         SCVAL(cli->outbuf,smb_com,SMBecho);
765         SSVAL(cli->outbuf,smb_tid,65535);
766         SSVAL(cli->outbuf,smb_vwv0,num_echos);
767         cli_setup_packet(cli);
768         p = smb_buf(cli->outbuf);
769         memcpy(p, data, length);
770         p += length;
771
772         cli_setup_bcc(cli, p);
773
774         cli_send_smb(cli);
775
776         for (i=0; i<num_echos; i++) {
777                 if (!cli_receive_smb(cli)) {
778                         return false;
779                 }
780
781                 if (cli_is_error(cli)) {
782                         return false;
783                 }
784         }
785
786         return true;
787 }