tests/krb5: Clarify checksum type assertion message
[Samba.git] / source3 / smbd / message.c
blobb97289468893f4b16aa3b56ceca1fd26230b3b4b
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 "system/filesys.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "smbprofile.h"
31 extern userdom_struct current_user_info;
33 struct msg_state {
34 char *from;
35 char *to;
36 char *msg;
39 /****************************************************************************
40 Deliver the message.
41 ****************************************************************************/
43 static void msg_deliver(struct msg_state *state)
45 TALLOC_CTX *frame = talloc_stackframe();
46 const struct loadparm_substitution *lp_sub =
47 loadparm_s3_global_substitution();
48 char *name = NULL;
49 int i;
50 int fd;
51 char *msg;
52 size_t len;
53 ssize_t sz;
54 fstring alpha_buf;
55 char *s;
56 mode_t mask;
58 if (! (*lp_message_command(frame, lp_sub))) {
59 DEBUG(1,("no messaging command specified\n"));
60 goto done;
63 /* put it in a temporary file */
64 name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
65 if (!name) {
66 goto done;
68 mask = umask(S_IRWXO | S_IRWXG);
69 fd = mkstemp(name);
70 umask(mask);
72 if (fd == -1) {
73 DEBUG(1, ("can't open message file %s: %s\n", name,
74 strerror(errno)));
75 goto done;
79 * Incoming message is in DOS codepage format. Convert to UNIX.
82 if (!convert_string_talloc(talloc_tos(), CH_DOS, CH_UNIX, state->msg,
83 talloc_get_size(state->msg), (void *)&msg,
84 &len)) {
85 DEBUG(3, ("Conversion failed, delivering message in DOS "
86 "codepage format\n"));
87 msg = state->msg;
90 for (i = 0; i < len; i++) {
91 if ((msg[i] == '\r') &&
92 (i < (len-1)) && (msg[i+1] == '\n')) {
93 continue;
95 sz = write(fd, &msg[i], 1);
96 if ( sz != 1 ) {
97 DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
98 (long)sz, strerror(errno)));
102 close(fd);
104 /* run the command */
105 s = lp_message_command(frame, lp_sub);
106 if (s == NULL) {
107 goto done;
110 alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
112 s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
113 if (s == NULL) {
114 goto done;
117 alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
119 s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
120 if (s == NULL) {
121 goto done;
124 s = talloc_sub_basic(talloc_tos(), current_user_info.smb_name,
125 current_user_info.domain, s);
126 if (s == NULL) {
127 goto done;
130 s = talloc_string_sub(talloc_tos(), s, "%s", name);
131 if (s == NULL) {
132 goto done;
134 smbrun(s, NULL, NULL);
136 done:
137 TALLOC_FREE(frame);
138 return;
141 /****************************************************************************
142 Reply to a sends.
143 conn POINTER CAN BE NULL HERE !
144 ****************************************************************************/
146 void reply_sends(struct smb_request *req)
148 const struct loadparm_substitution *lp_sub =
149 loadparm_s3_global_substitution();
150 struct msg_state *state;
151 int len;
152 const uint8_t *msg;
153 const uint8_t *p;
155 START_PROFILE(SMBsends);
157 if (!(*lp_message_command(talloc_tos(), lp_sub))) {
158 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
159 END_PROFILE(SMBsends);
160 return;
163 state = talloc(talloc_tos(), struct msg_state);
165 p = req->buf + 1;
166 p += srvstr_pull_req_talloc(
167 state, req, &state->from, p, STR_ASCII|STR_TERMINATE) + 1;
168 p += srvstr_pull_req_talloc(
169 state, req, &state->to, p, STR_ASCII|STR_TERMINATE) + 1;
171 msg = p;
173 len = SVAL(msg,0);
174 len = MIN(len, smbreq_bufrem(req, msg+2));
176 state->msg = talloc_array(state, char, len);
178 if (state->msg == NULL) {
179 reply_nterror(req, NT_STATUS_NO_MEMORY);
180 END_PROFILE(SMBsends);
181 return;
184 memcpy(state->msg, msg+2, len);
186 msg_deliver(state);
188 reply_outbuf(req, 0, 0);
190 END_PROFILE(SMBsends);
191 return;
194 /****************************************************************************
195 Reply to a sendstrt.
196 conn POINTER CAN BE NULL HERE !
197 ****************************************************************************/
199 void reply_sendstrt(struct smb_request *req)
201 const struct loadparm_substitution *lp_sub =
202 loadparm_s3_global_substitution();
203 struct smbXsrv_connection *xconn = req->xconn;
204 const uint8_t *p;
206 START_PROFILE(SMBsendstrt);
208 if (!(*lp_message_command(talloc_tos(), lp_sub))) {
209 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
210 END_PROFILE(SMBsendstrt);
211 return;
214 TALLOC_FREE(xconn->smb1.msg_state);
216 xconn->smb1.msg_state = talloc_zero(xconn, struct msg_state);
218 if (xconn->smb1.msg_state == NULL) {
219 reply_nterror(req, NT_STATUS_NO_MEMORY);
220 END_PROFILE(SMBsendstrt);
221 return;
224 p = req->buf+1;
225 p += srvstr_pull_req_talloc(
226 xconn->smb1.msg_state, req,
227 &xconn->smb1.msg_state->from, p,
228 STR_ASCII|STR_TERMINATE) + 1;
229 p += srvstr_pull_req_talloc(
230 xconn->smb1.msg_state, req,
231 &xconn->smb1.msg_state->to, p,
232 STR_ASCII|STR_TERMINATE) + 1;
234 DEBUG(3, ("SMBsendstrt (from %s to %s)\n",
235 xconn->smb1.msg_state->from,
236 xconn->smb1.msg_state->to));
238 reply_outbuf(req, 0, 0);
240 END_PROFILE(SMBsendstrt);
241 return;
244 /****************************************************************************
245 Reply to a sendtxt.
246 conn POINTER CAN BE NULL HERE !
247 ****************************************************************************/
249 void reply_sendtxt(struct smb_request *req)
251 const struct loadparm_substitution *lp_sub =
252 loadparm_s3_global_substitution();
253 struct smbXsrv_connection *xconn = req->xconn;
254 int len;
255 const char *msg;
256 char *tmp;
257 size_t old_len;
259 START_PROFILE(SMBsendtxt);
261 if (! (*lp_message_command(talloc_tos(), lp_sub))) {
262 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
263 END_PROFILE(SMBsendtxt);
264 return;
267 if ((xconn->smb1.msg_state == NULL) || (req->buflen < 3)) {
268 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
269 END_PROFILE(SMBsendtxt);
270 return;
273 msg = (const char *)req->buf + 1;
275 old_len = talloc_get_size(xconn->smb1.msg_state->msg);
277 len = MIN(SVAL(msg, 0), smbreq_bufrem(req, msg+2));
279 tmp = talloc_realloc(xconn->smb1.msg_state,
280 xconn->smb1.msg_state->msg,
281 char, old_len + len);
283 if (tmp == NULL) {
284 reply_nterror(req, NT_STATUS_NO_MEMORY);
285 END_PROFILE(SMBsendtxt);
286 return;
289 xconn->smb1.msg_state->msg = tmp;
291 memcpy(&xconn->smb1.msg_state->msg[old_len], msg+2, len);
293 DEBUG( 3, ( "SMBsendtxt\n" ) );
295 reply_outbuf(req, 0, 0);
297 END_PROFILE(SMBsendtxt);
298 return;
301 /****************************************************************************
302 Reply to a sendend.
303 conn POINTER CAN BE NULL HERE !
304 ****************************************************************************/
306 void reply_sendend(struct smb_request *req)
308 const struct loadparm_substitution *lp_sub =
309 loadparm_s3_global_substitution();
310 struct smbXsrv_connection *xconn = req->xconn;
311 START_PROFILE(SMBsendend);
313 if (! (*lp_message_command(talloc_tos(), lp_sub))) {
314 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
315 END_PROFILE(SMBsendend);
316 return;
319 if (xconn->smb1.msg_state == NULL) {
320 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
321 END_PROFILE(SMBsendend);
322 return;
325 DEBUG(3,("SMBsendend\n"));
327 msg_deliver(xconn->smb1.msg_state);
329 TALLOC_FREE(xconn->smb1.msg_state);
331 reply_outbuf(req, 0, 0);
333 END_PROFILE(SMBsendend);
334 return;