s3-rpc_server: Use the new endpoint register functions.
[Samba.git] / source3 / smbd / message.c
blob386aca1727cd9932f82d879b68d444f100c5bc37
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/globals.h"
28 extern userdom_struct current_user_info;
30 struct msg_state {
31 char *from;
32 char *to;
33 char *msg;
36 /****************************************************************************
37 Deliver the message.
38 ****************************************************************************/
40 static void msg_deliver(struct msg_state *state)
42 TALLOC_CTX *frame = talloc_stackframe();
43 char *name = NULL;
44 int i;
45 int fd;
46 char *msg;
47 size_t len;
48 ssize_t sz;
49 fstring alpha_buf;
50 char *s;
52 if (! (*lp_msg_command())) {
53 DEBUG(1,("no messaging command specified\n"));
54 goto done;
57 /* put it in a temporary file */
58 name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
59 if (!name) {
60 goto done;
62 fd = mkstemp(name);
64 if (fd == -1) {
65 DEBUG(1, ("can't open message file %s: %s\n", name,
66 strerror(errno)));
67 goto done;
71 * Incoming message is in DOS codepage format. Convert to UNIX.
74 if (!convert_string_talloc(talloc_tos(), CH_DOS, CH_UNIX, state->msg,
75 talloc_get_size(state->msg), (void *)&msg,
76 &len, true)) {
77 DEBUG(3, ("Conversion failed, delivering message in DOS "
78 "codepage format\n"));
79 msg = state->msg;
82 for (i = 0; i < len; i++) {
83 if ((msg[i] == '\r') &&
84 (i < (len-1)) && (msg[i+1] == '\n')) {
85 continue;
87 sz = write(fd, &msg[i], 1);
88 if ( sz != 1 ) {
89 DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
90 (long)sz, strerror(errno)));
94 close(fd);
96 /* run the command */
97 s = talloc_strdup(talloc_tos(), lp_msg_command());
98 if (s == NULL) {
99 goto done;
102 alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
104 s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
105 if (s == NULL) {
106 goto done;
109 alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
111 s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
112 if (s == NULL) {
113 goto done;
116 s = talloc_sub_basic(talloc_tos(), current_user_info.smb_name,
117 current_user_info.domain, s);
118 if (s == NULL) {
119 goto done;
122 s = talloc_string_sub(talloc_tos(), s, "%s", name);
123 if (s == NULL) {
124 goto done;
126 smbrun(s,NULL);
128 done:
129 TALLOC_FREE(frame);
130 return;
133 /****************************************************************************
134 Reply to a sends.
135 conn POINTER CAN BE NULL HERE !
136 ****************************************************************************/
138 void reply_sends(struct smb_request *req)
140 struct msg_state *state;
141 int len;
142 const char *msg;
143 const char *p;
145 START_PROFILE(SMBsends);
147 if (!(*lp_msg_command())) {
148 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
149 END_PROFILE(SMBsends);
150 return;
153 state = talloc(talloc_tos(), struct msg_state);
155 p = (const char *)req->buf + 1;
156 p += srvstr_pull_req_talloc(
157 state, req, &state->from, p, STR_ASCII|STR_TERMINATE) + 1;
158 p += srvstr_pull_req_talloc(
159 state, req, &state->to, p, STR_ASCII|STR_TERMINATE) + 1;
161 msg = p;
163 len = SVAL(msg,0);
164 len = MIN(len, smbreq_bufrem(req, msg+2));
166 state->msg = talloc_array(state, char, len);
168 if (state->msg == NULL) {
169 reply_nterror(req, NT_STATUS_NO_MEMORY);
170 END_PROFILE(SMBsends);
171 return;
174 memcpy(state->msg, msg+2, len);
176 msg_deliver(state);
178 reply_outbuf(req, 0, 0);
180 END_PROFILE(SMBsends);
181 return;
184 /****************************************************************************
185 Reply to a sendstrt.
186 conn POINTER CAN BE NULL HERE !
187 ****************************************************************************/
189 void reply_sendstrt(struct smb_request *req)
191 const char *p;
193 START_PROFILE(SMBsendstrt);
195 if (!(*lp_msg_command())) {
196 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
197 END_PROFILE(SMBsendstrt);
198 return;
201 TALLOC_FREE(smbd_msg_state);
203 smbd_msg_state = TALLOC_ZERO_P(NULL, struct msg_state);
205 if (smbd_msg_state == NULL) {
206 reply_nterror(req, NT_STATUS_NO_MEMORY);
207 END_PROFILE(SMBsendstrt);
208 return;
211 p = (const char *)req->buf+1;
212 p += srvstr_pull_req_talloc(
213 smbd_msg_state, req, &smbd_msg_state->from, p,
214 STR_ASCII|STR_TERMINATE) + 1;
215 p += srvstr_pull_req_talloc(
216 smbd_msg_state, req, &smbd_msg_state->to, p,
217 STR_ASCII|STR_TERMINATE) + 1;
219 DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", smbd_msg_state->from,
220 smbd_msg_state->to ) );
222 reply_outbuf(req, 0, 0);
224 END_PROFILE(SMBsendstrt);
225 return;
228 /****************************************************************************
229 Reply to a sendtxt.
230 conn POINTER CAN BE NULL HERE !
231 ****************************************************************************/
233 void reply_sendtxt(struct smb_request *req)
235 int len;
236 const char *msg;
237 char *tmp;
238 size_t old_len;
240 START_PROFILE(SMBsendtxt);
242 if (! (*lp_msg_command())) {
243 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
244 END_PROFILE(SMBsendtxt);
245 return;
248 if ((smbd_msg_state == NULL) || (req->buflen < 3)) {
249 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
250 END_PROFILE(SMBsendtxt);
251 return;
254 msg = (const char *)req->buf + 1;
256 old_len = talloc_get_size(smbd_msg_state->msg);
258 len = MIN(SVAL(msg, 0), smbreq_bufrem(req, msg+2));
260 tmp = TALLOC_REALLOC_ARRAY(smbd_msg_state, smbd_msg_state->msg,
261 char, old_len + len);
263 if (tmp == NULL) {
264 reply_nterror(req, NT_STATUS_NO_MEMORY);
265 END_PROFILE(SMBsendtxt);
266 return;
269 smbd_msg_state->msg = tmp;
271 memcpy(&smbd_msg_state->msg[old_len], msg+2, len);
273 DEBUG( 3, ( "SMBsendtxt\n" ) );
275 reply_outbuf(req, 0, 0);
277 END_PROFILE(SMBsendtxt);
278 return;
281 /****************************************************************************
282 Reply to a sendend.
283 conn POINTER CAN BE NULL HERE !
284 ****************************************************************************/
286 void reply_sendend(struct smb_request *req)
288 START_PROFILE(SMBsendend);
290 if (! (*lp_msg_command())) {
291 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
292 END_PROFILE(SMBsendend);
293 return;
296 DEBUG(3,("SMBsendend\n"));
298 msg_deliver(smbd_msg_state);
300 TALLOC_FREE(smbd_msg_state);
302 reply_outbuf(req, 0, 0);
304 END_PROFILE(SMBsendend);
305 return;