Patch from Samuel Thibault to convert messages from unix to dos charset. Works
[samba.git] / source / libsmb / climessage.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client message handling routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25
26 /****************************************************************************
27 start a message sequence
28 ****************************************************************************/
29 BOOL cli_message_start(struct cli_state *cli, char *host, char *username, 
30                               int *grp)
31 {
32         char *p;
33
34         /* send a SMBsendstrt command */
35         memset(cli->outbuf,'\0',smb_size);
36         set_message(cli->outbuf,0,0,True);
37         SCVAL(cli->outbuf,smb_com,SMBsendstrt);
38         SSVAL(cli->outbuf,smb_tid,cli->cnum);
39         cli_setup_packet(cli);
40         
41         p = smb_buf(cli->outbuf);
42         *p++ = 4;
43         p += clistr_push(cli, p, username, -1, STR_TERMINATE);
44         *p++ = 4;
45         p += clistr_push(cli, p, host, -1, STR_TERMINATE);
46         
47         cli_setup_bcc(cli, p);
48         
49         cli_send_smb(cli);      
50         
51         if (!cli_receive_smb(cli)) {
52                 return False;
53         }
54
55         if (cli_is_error(cli)) return False;
56
57         *grp = SVAL(cli->inbuf,smb_vwv0);
58
59         return True;
60 }
61
62
63 /****************************************************************************
64 send a message 
65 ****************************************************************************/
66 BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
67 {
68         char *msgdos;
69         int lendos;
70         char *p;
71
72         memset(cli->outbuf,'\0',smb_size);
73         set_message(cli->outbuf,1,0,True);
74         SCVAL(cli->outbuf,smb_com,SMBsendtxt);
75         SSVAL(cli->outbuf,smb_tid,cli->cnum);
76         cli_setup_packet(cli);
77
78         SSVAL(cli->outbuf,smb_vwv0,grp);
79         
80         p = smb_buf(cli->outbuf);
81         *p++ = 1;
82
83         if ((lendos = convert_string_allocate(CH_UNIX, CH_DOS, msg,len, (void **) &msgdos)) < 0 || !msgdos) {
84                 DEBUG(3,("Conversion failed, sending message in UNIX charset\n"));
85                 SSVAL(p, 0, len); p += 2;
86                 memcpy(p, msg, len);
87                 p += len;
88         } else {
89                 SSVAL(p, 0, lendos); p += 2;
90                 memcpy(p, msgdos, lendos);
91                 p += lendos;
92                 SAFE_FREE(msgdos);
93         }
94
95         cli_setup_bcc(cli, p);
96         cli_send_smb(cli);
97
98         if (!cli_receive_smb(cli)) {
99                 return False;
100         }
101
102         if (cli_is_error(cli)) return False;
103
104         return True;
105 }      
106
107 /****************************************************************************
108 end a message 
109 ****************************************************************************/
110 BOOL cli_message_end(struct cli_state *cli, int grp)
111 {
112         memset(cli->outbuf,'\0',smb_size);
113         set_message(cli->outbuf,1,0,True);
114         SCVAL(cli->outbuf,smb_com,SMBsendend);
115         SSVAL(cli->outbuf,smb_tid,cli->cnum);
116
117         SSVAL(cli->outbuf,smb_vwv0,grp);
118
119         cli_setup_packet(cli);
120         
121         cli_send_smb(cli);
122
123         if (!cli_receive_smb(cli)) {
124                 return False;
125         }
126
127         if (cli_is_error(cli)) return False;
128
129         return True;
130 }      
131