Fix coverity CID-602. Possible use of uninitialized var.
[Samba.git] / source / smbd / message.c
bloba870f03df98c945add02c81ba3b13b15292e3adb
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 int 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 len = convert_string_talloc(
76 talloc_tos(), CH_DOS, CH_UNIX, state->msg,
77 talloc_get_size(state->msg), (void *)&msg, true);
79 if (len == -1) {
80 DEBUG(3, ("Conversion failed, delivering message in DOS "
81 "codepage format\n"));
82 msg = state->msg;
85 for (i = 0; i < len; i++) {
86 if ((msg[i] == '\r') && (i < (len-1)) && (msg[i+1] == '\n')) {
87 continue;
89 sz = write(fd, &msg[i], 1);
90 if ( sz != 1 ) {
91 DEBUG(0, ("Write error to fd %d: %ld(%s)\n", fd,
92 (long)sz, strerror(errno)));
96 close(fd);
98 /* run the command */
99 s = talloc_strdup(talloc_tos(), lp_msg_command());
100 if (s == NULL) {
101 goto done;
104 alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
106 s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
107 if (s == NULL) {
108 goto done;
111 alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
113 s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
114 if (s == NULL) {
115 goto done;
118 s = talloc_sub_basic(talloc_tos(), current_user_info.smb_name,
119 current_user_info.domain, s);
120 if (s == NULL) {
121 goto done;
124 s = talloc_string_sub(talloc_tos(), s, "%s", name);
125 if (s == NULL) {
126 goto done;
128 smbrun(s,NULL);
130 done:
131 TALLOC_FREE(frame);
132 return;
135 /****************************************************************************
136 Reply to a sends.
137 conn POINTER CAN BE NULL HERE !
138 ****************************************************************************/
140 void reply_sends(struct smb_request *req)
142 struct msg_state *state;
143 int len;
144 char *msg;
145 char *p;
147 START_PROFILE(SMBsends);
149 if (!(*lp_msg_command())) {
150 reply_doserror(req, ERRSRV, ERRmsgoff);
151 END_PROFILE(SMBsends);
152 return;
155 state = talloc(talloc_tos(), struct msg_state);
157 p = smb_buf(req->inbuf)+1;
158 p += srvstr_pull_buf_talloc(
159 state, (char *)req->inbuf, req->flags2, &state->from, p,
160 STR_ASCII|STR_TERMINATE) + 1;
161 p += srvstr_pull_buf_talloc(
162 state, (char *)req->inbuf, req->flags2, &state->to, p,
163 STR_ASCII|STR_TERMINATE) + 1;
165 msg = p;
167 len = SVAL(msg,0);
168 len = MIN(len, smb_bufrem(req->inbuf, msg+2));
170 state->msg = talloc_array(state, char, len);
172 if (state->msg == NULL) {
173 reply_nterror(req, NT_STATUS_NO_MEMORY);
174 END_PROFILE(SMBsends);
175 return;
178 memcpy(state->msg, msg+2, len);
180 msg_deliver(state);
182 reply_outbuf(req, 0, 0);
184 END_PROFILE(SMBsends);
185 return;
188 /****************************************************************************
189 Reply to a sendstrt.
190 conn POINTER CAN BE NULL HERE !
191 ****************************************************************************/
193 void reply_sendstrt(struct smb_request *req)
195 char *p;
197 START_PROFILE(SMBsendstrt);
199 if (!(*lp_msg_command())) {
200 reply_doserror(req, ERRSRV, ERRmsgoff);
201 END_PROFILE(SMBsendstrt);
202 return;
205 TALLOC_FREE(smbd_msg_state);
207 smbd_msg_state = TALLOC_ZERO_P(NULL, struct msg_state);
209 if (smbd_msg_state == NULL) {
210 reply_nterror(req, NT_STATUS_NO_MEMORY);
211 END_PROFILE(SMBsendstrt);
212 return;
215 p = smb_buf(req->inbuf)+1;
216 p += srvstr_pull_buf_talloc(
217 smbd_msg_state, (char *)req->inbuf, req->flags2,
218 &smbd_msg_state->from, p, STR_ASCII|STR_TERMINATE) + 1;
219 p += srvstr_pull_buf_talloc(
220 smbd_msg_state, (char *)req->inbuf, req->flags2,
221 &smbd_msg_state->to, p, STR_ASCII|STR_TERMINATE) + 1;
223 DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", smbd_msg_state->from,
224 smbd_msg_state->to ) );
226 reply_outbuf(req, 0, 0);
228 END_PROFILE(SMBsendstrt);
229 return;
232 /****************************************************************************
233 Reply to a sendtxt.
234 conn POINTER CAN BE NULL HERE !
235 ****************************************************************************/
237 void reply_sendtxt(struct smb_request *req)
239 int len;
240 char *msg;
241 char *tmp;
242 size_t old_len;
244 START_PROFILE(SMBsendtxt);
246 if (! (*lp_msg_command())) {
247 reply_doserror(req, ERRSRV, ERRmsgoff);
248 END_PROFILE(SMBsendtxt);
249 return;
252 if (smbd_msg_state == NULL) {
253 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
254 END_PROFILE(SMBsendtxt);
255 return;
258 msg = smb_buf(req->inbuf) + 1;
260 old_len = talloc_get_size(smbd_msg_state->msg);
262 len = MIN(SVAL(msg, 0), smb_bufrem(req->inbuf, msg+2));
264 tmp = TALLOC_REALLOC_ARRAY(smbd_msg_state, smbd_msg_state->msg,
265 char, old_len + len);
267 if (tmp == NULL) {
268 reply_nterror(req, NT_STATUS_NO_MEMORY);
269 END_PROFILE(SMBsendtxt);
270 return;
273 smbd_msg_state->msg = tmp;
275 memcpy(&smbd_msg_state->msg[old_len], msg+2, len);
277 DEBUG( 3, ( "SMBsendtxt\n" ) );
279 reply_outbuf(req, 0, 0);
281 END_PROFILE(SMBsendtxt);
282 return;
285 /****************************************************************************
286 Reply to a sendend.
287 conn POINTER CAN BE NULL HERE !
288 ****************************************************************************/
290 void reply_sendend(struct smb_request *req)
292 START_PROFILE(SMBsendend);
294 if (! (*lp_msg_command())) {
295 reply_doserror(req, ERRSRV, ERRmsgoff);
296 END_PROFILE(SMBsendend);
297 return;
300 DEBUG(3,("SMBsendend\n"));
302 msg_deliver(smbd_msg_state);
304 TALLOC_FREE(smbd_msg_state);
306 reply_outbuf(req, 0, 0);
308 END_PROFILE(SMBsendend);
309 return;