ctdb/docs: Include ceph rados namespace support in man page
[Samba.git] / source3 / smbd / smb1_message.c
blobca7201e2e7f131ef78544bfb73b130308e8da0d8
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"
30 #include "source3/lib/substitute.h"
32 struct msg_state {
33 char *from;
34 char *to;
35 char *msg;
38 /****************************************************************************
39 Deliver the message.
40 ****************************************************************************/
42 static void msg_deliver(struct msg_state *state)
44 TALLOC_CTX *frame = talloc_stackframe();
45 const struct loadparm_substitution *lp_sub =
46 loadparm_s3_global_substitution();
47 char *name = NULL;
48 int i;
49 int fd;
50 char *msg;
51 size_t len;
52 ssize_t sz;
53 fstring alpha_buf;
54 char *s;
55 mode_t mask;
57 if (! (*lp_message_command(frame, lp_sub))) {
58 DEBUG(1,("no messaging command specified\n"));
59 goto done;
62 /* put it in a temporary file */
63 name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
64 if (!name) {
65 goto done;
67 mask = umask(S_IRWXO | S_IRWXG);
68 fd = mkstemp(name);
69 umask(mask);
71 if (fd == -1) {
72 DEBUG(1, ("can't open message file %s: %s\n", name,
73 strerror(errno)));
74 goto done;
78 * Incoming message is in DOS codepage format. Convert to UNIX.
81 if (!convert_string_talloc(talloc_tos(), CH_DOS, CH_UNIX, state->msg,
82 talloc_get_size(state->msg), (void *)&msg,
83 &len)) {
84 DEBUG(3, ("Conversion failed, delivering message in DOS "
85 "codepage format\n"));
86 msg = state->msg;
89 for (i = 0; i < len; i++) {
90 if ((msg[i] == '\r') &&
91 (i < (len-1)) && (msg[i+1] == '\n')) {
92 continue;
94 sz = write(fd, &msg[i], 1);
95 if ( sz != 1 ) {
96 DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
97 (long)sz, strerror(errno)));
101 close(fd);
103 /* run the command */
104 s = lp_message_command(frame, lp_sub);
105 if (s == NULL) {
106 goto done;
109 alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
111 s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
112 if (s == NULL) {
113 goto done;
116 alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
118 s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
119 if (s == NULL) {
120 goto done;
123 s = talloc_sub_basic(talloc_tos(), get_current_username(),
124 get_current_user_info_domain(), s);
125 if (s == NULL) {
126 goto done;
129 s = talloc_string_sub(talloc_tos(), s, "%s", name);
130 if (s == NULL) {
131 goto done;
133 smbrun(s, NULL, NULL);
135 done:
136 TALLOC_FREE(frame);
137 return;
140 /****************************************************************************
141 Reply to a sends.
142 conn POINTER CAN BE NULL HERE !
143 ****************************************************************************/
145 void reply_sends(struct smb_request *req)
147 const struct loadparm_substitution *lp_sub =
148 loadparm_s3_global_substitution();
149 struct msg_state *state;
150 int len;
151 const uint8_t *msg;
152 const uint8_t *p;
154 START_PROFILE(SMBsends);
156 if (!(*lp_message_command(talloc_tos(), lp_sub))) {
157 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
158 END_PROFILE(SMBsends);
159 return;
162 state = talloc_zero(talloc_tos(), struct msg_state);
164 p = req->buf + 1;
165 p += srvstr_pull_req_talloc(
166 state, req, &state->from, p, STR_ASCII|STR_TERMINATE) + 1;
167 p += srvstr_pull_req_talloc(
168 state, req, &state->to, p, STR_ASCII|STR_TERMINATE) + 1;
170 msg = p;
172 len = SVAL(msg,0);
173 len = MIN(len, smbreq_bufrem(req, msg+2));
175 state->msg = talloc_array(state, char, len);
177 if (state->msg == NULL) {
178 reply_nterror(req, NT_STATUS_NO_MEMORY);
179 END_PROFILE(SMBsends);
180 return;
183 memcpy(state->msg, msg+2, len);
185 msg_deliver(state);
187 reply_smb1_outbuf(req, 0, 0);
189 END_PROFILE(SMBsends);
190 return;
193 /****************************************************************************
194 Reply to a sendstrt.
195 conn POINTER CAN BE NULL HERE !
196 ****************************************************************************/
198 void reply_sendstrt(struct smb_request *req)
200 const struct loadparm_substitution *lp_sub =
201 loadparm_s3_global_substitution();
202 struct smbXsrv_connection *xconn = req->xconn;
203 const uint8_t *p;
205 START_PROFILE(SMBsendstrt);
207 if (!(*lp_message_command(talloc_tos(), lp_sub))) {
208 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
209 END_PROFILE(SMBsendstrt);
210 return;
213 TALLOC_FREE(xconn->smb1.msg_state);
215 xconn->smb1.msg_state = talloc_zero(xconn, struct msg_state);
217 if (xconn->smb1.msg_state == NULL) {
218 reply_nterror(req, NT_STATUS_NO_MEMORY);
219 END_PROFILE(SMBsendstrt);
220 return;
223 p = req->buf+1;
224 p += srvstr_pull_req_talloc(
225 xconn->smb1.msg_state, req,
226 &xconn->smb1.msg_state->from, p,
227 STR_ASCII|STR_TERMINATE) + 1;
228 p += srvstr_pull_req_talloc(
229 xconn->smb1.msg_state, req,
230 &xconn->smb1.msg_state->to, p,
231 STR_ASCII|STR_TERMINATE) + 1;
233 DEBUG(3, ("SMBsendstrt (from %s to %s)\n",
234 xconn->smb1.msg_state->from,
235 xconn->smb1.msg_state->to));
237 reply_smb1_outbuf(req, 0, 0);
239 END_PROFILE(SMBsendstrt);
240 return;
243 /****************************************************************************
244 Reply to a sendtxt.
245 conn POINTER CAN BE NULL HERE !
246 ****************************************************************************/
248 void reply_sendtxt(struct smb_request *req)
250 const struct loadparm_substitution *lp_sub =
251 loadparm_s3_global_substitution();
252 struct smbXsrv_connection *xconn = req->xconn;
253 int len;
254 const char *msg;
255 char *tmp;
256 size_t old_len;
258 START_PROFILE(SMBsendtxt);
260 if (! (*lp_message_command(talloc_tos(), lp_sub))) {
261 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
262 END_PROFILE(SMBsendtxt);
263 return;
266 if ((xconn->smb1.msg_state == NULL) || (req->buflen < 3)) {
267 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
268 END_PROFILE(SMBsendtxt);
269 return;
272 msg = (const char *)req->buf + 1;
274 old_len = talloc_get_size(xconn->smb1.msg_state->msg);
276 len = MIN(SVAL(msg, 0), smbreq_bufrem(req, msg+2));
278 tmp = talloc_realloc(xconn->smb1.msg_state,
279 xconn->smb1.msg_state->msg,
280 char, old_len + len);
282 if (tmp == NULL) {
283 reply_nterror(req, NT_STATUS_NO_MEMORY);
284 END_PROFILE(SMBsendtxt);
285 return;
288 xconn->smb1.msg_state->msg = tmp;
290 memcpy(&xconn->smb1.msg_state->msg[old_len], msg+2, len);
292 DEBUG( 3, ( "SMBsendtxt\n" ) );
294 reply_smb1_outbuf(req, 0, 0);
296 END_PROFILE(SMBsendtxt);
297 return;
300 /****************************************************************************
301 Reply to a sendend.
302 conn POINTER CAN BE NULL HERE !
303 ****************************************************************************/
305 void reply_sendend(struct smb_request *req)
307 const struct loadparm_substitution *lp_sub =
308 loadparm_s3_global_substitution();
309 struct smbXsrv_connection *xconn = req->xconn;
310 START_PROFILE(SMBsendend);
312 if (! (*lp_message_command(talloc_tos(), lp_sub))) {
313 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
314 END_PROFILE(SMBsendend);
315 return;
318 if (xconn->smb1.msg_state == NULL) {
319 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
320 END_PROFILE(SMBsendend);
321 return;
324 DEBUG(3,("SMBsendend\n"));
326 msg_deliver(xconn->smb1.msg_state);
328 TALLOC_FREE(xconn->smb1.msg_state);
330 reply_smb1_outbuf(req, 0, 0);
332 END_PROFILE(SMBsendend);
333 return;