s3: include smbd/smbd.h where needed.
[Samba.git] / source3 / smbd / message.c
blob06603af0b115d9826d90ebbb6664864e268c6867
1 /*
2 Unix SMB/CIFS implementation.
3 SMB messaging
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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/>.
20 This file handles the messaging system calls for winpopup style
21 messages
25 #include "includes.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
29 extern userdom_struct current_user_info;
31 struct msg_state {
32 char *from;
33 char *to;
34 char *msg;
37 /****************************************************************************
38 Deliver the message.
39 ****************************************************************************/
41 static void msg_deliver(struct msg_state *state)
43 TALLOC_CTX *frame = talloc_stackframe();
44 char *name = NULL;
45 int i;
46 int fd;
47 char *msg;
48 size_t len;
49 ssize_t sz;
50 fstring alpha_buf;
51 char *s;
53 if (! (*lp_msg_command())) {
54 DEBUG(1,("no messaging command specified\n"));
55 goto done;
58 /* put it in a temporary file */
59 name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
60 if (!name) {
61 goto done;
63 fd = mkstemp(name);
65 if (fd == -1) {
66 DEBUG(1, ("can't open message file %s: %s\n", name,
67 strerror(errno)));
68 goto done;
72 * Incoming message is in DOS codepage format. Convert to UNIX.
75 if (!convert_string_talloc(talloc_tos(), CH_DOS, CH_UNIX, state->msg,
76 talloc_get_size(state->msg), (void *)&msg,
77 &len, true)) {
78 DEBUG(3, ("Conversion failed, delivering message in DOS "
79 "codepage format\n"));
80 msg = state->msg;
83 for (i = 0; i < len; i++) {
84 if ((msg[i] == '\r') &&
85 (i < (len-1)) && (msg[i+1] == '\n')) {
86 continue;
88 sz = write(fd, &msg[i], 1);
89 if ( sz != 1 ) {
90 DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
91 (long)sz, strerror(errno)));
95 close(fd);
97 /* run the command */
98 s = talloc_strdup(talloc_tos(), lp_msg_command());
99 if (s == NULL) {
100 goto done;
103 alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
105 s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
106 if (s == NULL) {
107 goto done;
110 alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
112 s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
113 if (s == NULL) {
114 goto done;
117 s = talloc_sub_basic(talloc_tos(), current_user_info.smb_name,
118 current_user_info.domain, s);
119 if (s == NULL) {
120 goto done;
123 s = talloc_string_sub(talloc_tos(), s, "%s", name);
124 if (s == NULL) {
125 goto done;
127 smbrun(s,NULL);
129 done:
130 TALLOC_FREE(frame);
131 return;
134 /****************************************************************************
135 Reply to a sends.
136 conn POINTER CAN BE NULL HERE !
137 ****************************************************************************/
139 void reply_sends(struct smb_request *req)
141 struct msg_state *state;
142 int len;
143 const char *msg;
144 const char *p;
146 START_PROFILE(SMBsends);
148 if (!(*lp_msg_command())) {
149 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
150 END_PROFILE(SMBsends);
151 return;
154 state = talloc(talloc_tos(), struct msg_state);
156 p = (const char *)req->buf + 1;
157 p += srvstr_pull_req_talloc(
158 state, req, &state->from, p, STR_ASCII|STR_TERMINATE) + 1;
159 p += srvstr_pull_req_talloc(
160 state, req, &state->to, p, STR_ASCII|STR_TERMINATE) + 1;
162 msg = p;
164 len = SVAL(msg,0);
165 len = MIN(len, smbreq_bufrem(req, msg+2));
167 state->msg = talloc_array(state, char, len);
169 if (state->msg == NULL) {
170 reply_nterror(req, NT_STATUS_NO_MEMORY);
171 END_PROFILE(SMBsends);
172 return;
175 memcpy(state->msg, msg+2, len);
177 msg_deliver(state);
179 reply_outbuf(req, 0, 0);
181 END_PROFILE(SMBsends);
182 return;
185 /****************************************************************************
186 Reply to a sendstrt.
187 conn POINTER CAN BE NULL HERE !
188 ****************************************************************************/
190 void reply_sendstrt(struct smb_request *req)
192 const char *p;
194 START_PROFILE(SMBsendstrt);
196 if (!(*lp_msg_command())) {
197 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
198 END_PROFILE(SMBsendstrt);
199 return;
202 TALLOC_FREE(smbd_msg_state);
204 smbd_msg_state = TALLOC_ZERO_P(NULL, struct msg_state);
206 if (smbd_msg_state == NULL) {
207 reply_nterror(req, NT_STATUS_NO_MEMORY);
208 END_PROFILE(SMBsendstrt);
209 return;
212 p = (const char *)req->buf+1;
213 p += srvstr_pull_req_talloc(
214 smbd_msg_state, req, &smbd_msg_state->from, p,
215 STR_ASCII|STR_TERMINATE) + 1;
216 p += srvstr_pull_req_talloc(
217 smbd_msg_state, req, &smbd_msg_state->to, p,
218 STR_ASCII|STR_TERMINATE) + 1;
220 DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", smbd_msg_state->from,
221 smbd_msg_state->to ) );
223 reply_outbuf(req, 0, 0);
225 END_PROFILE(SMBsendstrt);
226 return;
229 /****************************************************************************
230 Reply to a sendtxt.
231 conn POINTER CAN BE NULL HERE !
232 ****************************************************************************/
234 void reply_sendtxt(struct smb_request *req)
236 int len;
237 const char *msg;
238 char *tmp;
239 size_t old_len;
241 START_PROFILE(SMBsendtxt);
243 if (! (*lp_msg_command())) {
244 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
245 END_PROFILE(SMBsendtxt);
246 return;
249 if ((smbd_msg_state == NULL) || (req->buflen < 3)) {
250 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
251 END_PROFILE(SMBsendtxt);
252 return;
255 msg = (const char *)req->buf + 1;
257 old_len = talloc_get_size(smbd_msg_state->msg);
259 len = MIN(SVAL(msg, 0), smbreq_bufrem(req, msg+2));
261 tmp = TALLOC_REALLOC_ARRAY(smbd_msg_state, smbd_msg_state->msg,
262 char, old_len + len);
264 if (tmp == NULL) {
265 reply_nterror(req, NT_STATUS_NO_MEMORY);
266 END_PROFILE(SMBsendtxt);
267 return;
270 smbd_msg_state->msg = tmp;
272 memcpy(&smbd_msg_state->msg[old_len], msg+2, len);
274 DEBUG( 3, ( "SMBsendtxt\n" ) );
276 reply_outbuf(req, 0, 0);
278 END_PROFILE(SMBsendtxt);
279 return;
282 /****************************************************************************
283 Reply to a sendend.
284 conn POINTER CAN BE NULL HERE !
285 ****************************************************************************/
287 void reply_sendend(struct smb_request *req)
289 START_PROFILE(SMBsendend);
291 if (! (*lp_msg_command())) {
292 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
293 END_PROFILE(SMBsendend);
294 return;
297 DEBUG(3,("SMBsendend\n"));
299 msg_deliver(smbd_msg_state);
301 TALLOC_FREE(smbd_msg_state);
303 reply_outbuf(req, 0, 0);
305 END_PROFILE(SMBsendend);
306 return;