Now conn is part of smb_request, we don't need it as
[nivanova/samba-autobuild/.git] / source3 / smbd / message.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB messaging
4    Copyright (C) Andrew Tridgell 1992-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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20    This file handles the messaging system calls for winpopup style
21    messages
22 */
23
24
25 #include "includes.h"
26
27 extern userdom_struct current_user_info;
28
29 struct msg_state {
30         char *from;
31         char *to;
32         char *msg;
33 };
34
35 static struct msg_state *smbd_msg_state;
36
37 /****************************************************************************
38  Deliver the message.
39 ****************************************************************************/
40
41 static void msg_deliver(struct msg_state *state)
42 {
43         TALLOC_CTX *frame = talloc_stackframe();
44         char *name = NULL;
45         int i;
46         int fd;
47         char *msg;
48         int len;
49         ssize_t sz;
50         fstring alpha_buf;
51         char *s;
52
53         if (! (*lp_msg_command())) {
54                 DEBUG(1,("no messaging command specified\n"));
55                 goto done;
56         }
57
58         /* put it in a temporary file */
59         name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
60         if (!name) {
61                 goto done;
62         }
63         fd = smb_mkstemp(name);
64
65         if (fd == -1) {
66                 DEBUG(1, ("can't open message file %s: %s\n", name,
67                           strerror(errno)));
68                 goto done;
69         }
70
71         /*
72          * Incoming message is in DOS codepage format. Convert to UNIX.
73          */
74
75         len = convert_string_talloc(
76                 talloc_tos(), CH_DOS, CH_UNIX, state->msg,
77                 talloc_get_size(state->msg), (void *)&msg, true);
78
79         if (len == -1) {
80                 DEBUG(3, ("Conversion failed, delivering message in DOS "
81                           "codepage format\n"));
82                 msg = state->msg;
83         }
84
85         for (i = 0; i < len; i++) {
86                 if ((msg[i] == '\r') && (i < (len-1)) && (msg[i+1] == '\n')) {
87                         continue;
88                 }
89                 sz = write(fd, &msg[i], 1);
90                 if ( sz != 1 ) {
91                         DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
92                                   (long)sz, strerror(errno)));
93                 }
94         }
95
96         close(fd);
97
98         /* run the command */
99         s = talloc_strdup(talloc_tos(), lp_msg_command());
100         if (s == NULL) {
101                 goto done;
102         }
103
104         alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
105
106         s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
107         if (s == NULL) {
108                 goto done;
109         }
110
111         alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
112
113         s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
114         if (s == NULL) {
115                 goto done;
116         }
117
118         s = talloc_sub_basic(talloc_tos(), current_user_info.smb_name,
119                              current_user_info.domain, s);
120         if (s == NULL) {
121                 goto done;
122         }
123
124         s = talloc_string_sub(talloc_tos(), s, "%s", name);
125         if (s == NULL) {
126                 goto done;
127         }
128         smbrun(s,NULL);
129
130  done:
131         TALLOC_FREE(frame);
132         return;
133 }
134
135 /****************************************************************************
136  Reply to a sends.
137  conn POINTER CAN BE NULL HERE !
138 ****************************************************************************/
139
140 void reply_sends(struct smb_request *req)
141 {
142         struct msg_state *state;
143         int len;
144         char *msg;
145         char *p;
146
147         START_PROFILE(SMBsends);
148
149         if (!(*lp_msg_command())) {
150                 reply_doserror(req, ERRSRV, ERRmsgoff);
151                 END_PROFILE(SMBsends);
152                 return;
153         }
154
155         state = talloc(talloc_tos(), struct msg_state);
156
157         p = smb_buf(req->inbuf)+1;
158         p += srvstr_pull_buf_talloc(
159                 state, (char *)req->inbuf, req->flags2, &state->from, p,
160                 STR_ASCII|STR_TERMINATE) + 1;
161         p += srvstr_pull_buf_talloc(
162                 state, (char *)req->inbuf, req->flags2, &state->to, p,
163                 STR_ASCII|STR_TERMINATE) + 1;
164
165         msg = p;
166
167         len = SVAL(msg,0);
168         len = MIN(len, smb_bufrem(req->inbuf, msg+2));
169
170         state->msg = talloc_array(state, char, len);
171
172         if (state->msg == NULL) {
173                 reply_nterror(req, NT_STATUS_NO_MEMORY);
174                 END_PROFILE(SMBsends);
175                 return;
176         }
177
178         memcpy(state->msg, msg+2, len);
179
180         msg_deliver(state);
181
182         reply_outbuf(req, 0, 0);
183
184         END_PROFILE(SMBsends);
185         return;
186 }
187
188 /****************************************************************************
189  Reply to a sendstrt.
190  conn POINTER CAN BE NULL HERE !
191 ****************************************************************************/
192
193 void reply_sendstrt(struct smb_request *req)
194 {
195         char *p;
196
197         START_PROFILE(SMBsendstrt);
198
199         if (!(*lp_msg_command())) {
200                 reply_doserror(req, ERRSRV, ERRmsgoff);
201                 END_PROFILE(SMBsendstrt);
202                 return;
203         }
204
205         TALLOC_FREE(smbd_msg_state);
206
207         smbd_msg_state = TALLOC_ZERO_P(NULL, struct msg_state);
208
209         if (smbd_msg_state == NULL) {
210                 reply_nterror(req, NT_STATUS_NO_MEMORY);
211                 END_PROFILE(SMBsendstrt);
212                 return;
213         }
214
215         p = smb_buf(req->inbuf)+1;
216         p += srvstr_pull_buf_talloc(
217                 smbd_msg_state, (char *)req->inbuf, req->flags2,
218                 &smbd_msg_state->from, p, STR_ASCII|STR_TERMINATE) + 1;
219         p += srvstr_pull_buf_talloc(
220                 smbd_msg_state, (char *)req->inbuf, req->flags2,
221                 &smbd_msg_state->to, p, STR_ASCII|STR_TERMINATE) + 1;
222
223         DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", smbd_msg_state->from,
224                     smbd_msg_state->to ) );
225
226         reply_outbuf(req, 0, 0);
227
228         END_PROFILE(SMBsendstrt);
229         return;
230 }
231
232 /****************************************************************************
233  Reply to a sendtxt.
234  conn POINTER CAN BE NULL HERE !
235 ****************************************************************************/
236
237 void reply_sendtxt(struct smb_request *req)
238 {
239         int len;
240         char *msg;
241         char *tmp;
242         size_t old_len;
243
244         START_PROFILE(SMBsendtxt);
245
246         if (! (*lp_msg_command())) {
247                 reply_doserror(req, ERRSRV, ERRmsgoff);
248                 END_PROFILE(SMBsendtxt);
249                 return;
250         }
251
252         if (smbd_msg_state == NULL) {
253                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
254                 END_PROFILE(SMBsendtxt);
255                 return;
256         }
257
258         msg = smb_buf(req->inbuf) + 1;
259
260         old_len = talloc_get_size(smbd_msg_state->msg);
261
262         len = MIN(SVAL(msg, 0), smb_bufrem(req->inbuf, msg+2));
263
264         tmp = TALLOC_REALLOC_ARRAY(smbd_msg_state, smbd_msg_state->msg,
265                                    char, old_len + len);
266
267         if (tmp == NULL) {
268                 reply_nterror(req, NT_STATUS_NO_MEMORY);
269                 END_PROFILE(SMBsendtxt);
270                 return;
271         }
272
273         smbd_msg_state->msg = tmp;
274
275         memcpy(&smbd_msg_state->msg[old_len], msg+2, len);
276
277         DEBUG( 3, ( "SMBsendtxt\n" ) );
278
279         reply_outbuf(req, 0, 0);
280
281         END_PROFILE(SMBsendtxt);
282         return;
283 }
284
285 /****************************************************************************
286  Reply to a sendend.
287  conn POINTER CAN BE NULL HERE !
288 ****************************************************************************/
289
290 void reply_sendend(struct smb_request *req)
291 {
292         START_PROFILE(SMBsendend);
293
294         if (! (*lp_msg_command())) {
295                 reply_doserror(req, ERRSRV, ERRmsgoff);
296                 END_PROFILE(SMBsendend);
297                 return;
298         }
299
300         DEBUG(3,("SMBsendend\n"));
301
302         msg_deliver(smbd_msg_state);
303
304         TALLOC_FREE(smbd_msg_state);
305
306         reply_outbuf(req, 0, 0);
307
308         END_PROFILE(SMBsendend);
309         return;
310 }