s3-build: really fix build of winbind_krb5_locator.
[Samba.git] / source / smbd / message.c
blob62df5c37ebfbb7a98af0e4350b99b5f2b6f800a0
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"
27 extern userdom_struct current_user_info;
29 struct msg_state {
30 char *from;
31 char *to;
32 char *msg;
35 static struct msg_state *smbd_msg_state;
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 = smb_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 char *msg;
144 char *p;
146 START_PROFILE(SMBsends);
148 if (!(*lp_msg_command())) {
149 reply_doserror(req, ERRSRV, ERRmsgoff);
150 END_PROFILE(SMBsends);
151 return;
154 state = talloc(talloc_tos(), struct msg_state);
156 p = smb_buf(req->inbuf)+1;
157 p += srvstr_pull_buf_talloc(
158 state, (char *)req->inbuf, req->flags2, &state->from, p,
159 STR_ASCII|STR_TERMINATE) + 1;
160 p += srvstr_pull_buf_talloc(
161 state, (char *)req->inbuf, req->flags2, &state->to, p,
162 STR_ASCII|STR_TERMINATE) + 1;
164 msg = p;
166 len = SVAL(msg,0);
167 len = MIN(len, smb_bufrem(req->inbuf, msg+2));
169 state->msg = talloc_array(state, char, len);
171 if (state->msg == NULL) {
172 reply_nterror(req, NT_STATUS_NO_MEMORY);
173 END_PROFILE(SMBsends);
174 return;
177 memcpy(state->msg, msg+2, len);
179 msg_deliver(state);
181 reply_outbuf(req, 0, 0);
183 END_PROFILE(SMBsends);
184 return;
187 /****************************************************************************
188 Reply to a sendstrt.
189 conn POINTER CAN BE NULL HERE !
190 ****************************************************************************/
192 void reply_sendstrt(struct smb_request *req)
194 char *p;
196 START_PROFILE(SMBsendstrt);
198 if (!(*lp_msg_command())) {
199 reply_doserror(req, ERRSRV, ERRmsgoff);
200 END_PROFILE(SMBsendstrt);
201 return;
204 TALLOC_FREE(smbd_msg_state);
206 smbd_msg_state = TALLOC_ZERO_P(NULL, struct msg_state);
208 if (smbd_msg_state == NULL) {
209 reply_nterror(req, NT_STATUS_NO_MEMORY);
210 END_PROFILE(SMBsendstrt);
211 return;
214 p = smb_buf(req->inbuf)+1;
215 p += srvstr_pull_buf_talloc(
216 smbd_msg_state, (char *)req->inbuf, req->flags2,
217 &smbd_msg_state->from, p, STR_ASCII|STR_TERMINATE) + 1;
218 p += srvstr_pull_buf_talloc(
219 smbd_msg_state, (char *)req->inbuf, req->flags2,
220 &smbd_msg_state->to, p, STR_ASCII|STR_TERMINATE) + 1;
222 DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", smbd_msg_state->from,
223 smbd_msg_state->to ) );
225 reply_outbuf(req, 0, 0);
227 END_PROFILE(SMBsendstrt);
228 return;
231 /****************************************************************************
232 Reply to a sendtxt.
233 conn POINTER CAN BE NULL HERE !
234 ****************************************************************************/
236 void reply_sendtxt(struct smb_request *req)
238 int len;
239 char *msg;
240 char *tmp;
241 size_t old_len;
243 START_PROFILE(SMBsendtxt);
245 if (! (*lp_msg_command())) {
246 reply_doserror(req, ERRSRV, ERRmsgoff);
247 END_PROFILE(SMBsendtxt);
248 return;
251 if (smbd_msg_state == NULL) {
252 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
253 END_PROFILE(SMBsendtxt);
254 return;
257 msg = smb_buf(req->inbuf) + 1;
259 old_len = talloc_get_size(smbd_msg_state->msg);
261 len = MIN(SVAL(msg, 0), smb_bufrem(req->inbuf, msg+2));
263 tmp = TALLOC_REALLOC_ARRAY(smbd_msg_state, smbd_msg_state->msg,
264 char, old_len + len);
266 if (tmp == NULL) {
267 reply_nterror(req, NT_STATUS_NO_MEMORY);
268 END_PROFILE(SMBsendtxt);
269 return;
272 smbd_msg_state->msg = tmp;
274 memcpy(&smbd_msg_state->msg[old_len], msg+2, len);
276 DEBUG( 3, ( "SMBsendtxt\n" ) );
278 reply_outbuf(req, 0, 0);
280 END_PROFILE(SMBsendtxt);
281 return;
284 /****************************************************************************
285 Reply to a sendend.
286 conn POINTER CAN BE NULL HERE !
287 ****************************************************************************/
289 void reply_sendend(struct smb_request *req)
291 START_PROFILE(SMBsendend);
293 if (! (*lp_msg_command())) {
294 reply_doserror(req, ERRSRV, ERRmsgoff);
295 END_PROFILE(SMBsendend);
296 return;
299 DEBUG(3,("SMBsendend\n"));
301 msg_deliver(smbd_msg_state);
303 TALLOC_FREE(smbd_msg_state);
305 reply_outbuf(req, 0, 0);
307 END_PROFILE(SMBsendend);
308 return;