s3:passdb: add sid_check_object_is_for_passdb()
[kai/samba.git] / source3 / smbd / message.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB messaging
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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/>.
18 */
19 /*
20    This file handles the messaging system calls for winpopup style
21    messages
22 */
23
24
25 #include "includes.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "smbprofile.h"
29
30 extern userdom_struct current_user_info;
31
32 struct msg_state {
33         char *from;
34         char *to;
35         char *msg;
36 };
37
38 /****************************************************************************
39  Deliver the message.
40 ****************************************************************************/
41
42 static void msg_deliver(struct msg_state *state)
43 {
44         TALLOC_CTX *frame = talloc_stackframe();
45         char *name = NULL;
46         int i;
47         int fd;
48         char *msg;
49         size_t len;
50         ssize_t sz;
51         fstring alpha_buf;
52         char *s;
53
54         if (! (*lp_msg_command(frame))) {
55                 DEBUG(1,("no messaging command specified\n"));
56                 goto done;
57         }
58
59         /* put it in a temporary file */
60         name = talloc_asprintf(talloc_tos(), "%s/msg.XXXXXX", tmpdir());
61         if (!name) {
62                 goto done;
63         }
64         fd = mkstemp(name);
65
66         if (fd == -1) {
67                 DEBUG(1, ("can't open message file %s: %s\n", name,
68                           strerror(errno)));
69                 goto done;
70         }
71
72         /*
73          * Incoming message is in DOS codepage format. Convert to UNIX.
74          */
75
76         if (!convert_string_talloc(talloc_tos(), CH_DOS, CH_UNIX, state->msg,
77                                    talloc_get_size(state->msg), (void *)&msg,
78                                    &len)) {
79                 DEBUG(3, ("Conversion failed, delivering message in DOS "
80                           "codepage format\n"));
81                 msg = state->msg;
82         }
83
84         for (i = 0; i < len; i++) {
85                 if ((msg[i] == '\r') &&
86                     (i < (len-1)) && (msg[i+1] == '\n')) {
87                         continue;
88                 }
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)));
93                 }
94         }
95
96         close(fd);
97
98         /* run the command */
99         s = lp_msg_command(frame);
100         if (s == NULL) {
101                 goto done;
102         }
103
104         alpha_strcpy(alpha_buf, state->from, NULL, sizeof(alpha_buf));
105
106         s = talloc_string_sub(talloc_tos(), s, "%f", alpha_buf);
107         if (s == NULL) {
108                 goto done;
109         }
110
111         alpha_strcpy(alpha_buf, state->to, NULL, sizeof(alpha_buf));
112
113         s = talloc_string_sub(talloc_tos(), s, "%t", alpha_buf);
114         if (s == NULL) {
115                 goto done;
116         }
117
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;
122         }
123
124         s = talloc_string_sub(talloc_tos(), s, "%s", name);
125         if (s == NULL) {
126                 goto done;
127         }
128         smbrun(s,NULL);
129
130  done:
131         TALLOC_FREE(frame);
132         return;
133 }
134
135 /****************************************************************************
136  Reply to a sends.
137  conn POINTER CAN BE NULL HERE !
138 ****************************************************************************/
139
140 void reply_sends(struct smb_request *req)
141 {
142         struct msg_state *state;
143         int len;
144         const char *msg;
145         const char *p;
146
147         START_PROFILE(SMBsends);
148
149         if (!(*lp_msg_command(talloc_tos()))) {
150                 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
151                 END_PROFILE(SMBsends);
152                 return;
153         }
154
155         state = talloc(talloc_tos(), struct msg_state);
156
157         p = (const char *)req->buf + 1;
158         p += srvstr_pull_req_talloc(
159                 state, req, &state->from, p, STR_ASCII|STR_TERMINATE) + 1;
160         p += srvstr_pull_req_talloc(
161                 state, req, &state->to, p, STR_ASCII|STR_TERMINATE) + 1;
162
163         msg = p;
164
165         len = SVAL(msg,0);
166         len = MIN(len, smbreq_bufrem(req, msg+2));
167
168         state->msg = talloc_array(state, char, len);
169
170         if (state->msg == NULL) {
171                 reply_nterror(req, NT_STATUS_NO_MEMORY);
172                 END_PROFILE(SMBsends);
173                 return;
174         }
175
176         memcpy(state->msg, msg+2, len);
177
178         msg_deliver(state);
179
180         reply_outbuf(req, 0, 0);
181
182         END_PROFILE(SMBsends);
183         return;
184 }
185
186 /****************************************************************************
187  Reply to a sendstrt.
188  conn POINTER CAN BE NULL HERE !
189 ****************************************************************************/
190
191 void reply_sendstrt(struct smb_request *req)
192 {
193         const char *p;
194
195         START_PROFILE(SMBsendstrt);
196
197         if (!(*lp_msg_command(talloc_tos()))) {
198                 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
199                 END_PROFILE(SMBsendstrt);
200                 return;
201         }
202
203         TALLOC_FREE(req->sconn->conn->msg_state);
204
205         req->sconn->conn->msg_state = talloc_zero(NULL, struct msg_state);
206
207         if (req->sconn->conn->msg_state == NULL) {
208                 reply_nterror(req, NT_STATUS_NO_MEMORY);
209                 END_PROFILE(SMBsendstrt);
210                 return;
211         }
212
213         p = (const char *)req->buf+1;
214         p += srvstr_pull_req_talloc(
215                 req->sconn->conn->msg_state, req,
216                 &req->sconn->conn->msg_state->from, p,
217                 STR_ASCII|STR_TERMINATE) + 1;
218         p += srvstr_pull_req_talloc(
219                 req->sconn->conn->msg_state, req,
220                 &req->sconn->conn->msg_state->to, p,
221                 STR_ASCII|STR_TERMINATE) + 1;
222
223         DEBUG(3, ("SMBsendstrt (from %s to %s)\n",
224                   req->sconn->conn->msg_state->from,
225                   req->sconn->conn->msg_state->to));
226
227         reply_outbuf(req, 0, 0);
228
229         END_PROFILE(SMBsendstrt);
230         return;
231 }
232
233 /****************************************************************************
234  Reply to a sendtxt.
235  conn POINTER CAN BE NULL HERE !
236 ****************************************************************************/
237
238 void reply_sendtxt(struct smb_request *req)
239 {
240         int len;
241         const char *msg;
242         char *tmp;
243         size_t old_len;
244
245         START_PROFILE(SMBsendtxt);
246
247         if (! (*lp_msg_command(talloc_tos()))) {
248                 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
249                 END_PROFILE(SMBsendtxt);
250                 return;
251         }
252
253         if ((req->sconn->conn->msg_state == NULL) || (req->buflen < 3)) {
254                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
255                 END_PROFILE(SMBsendtxt);
256                 return;
257         }
258
259         msg = (const char *)req->buf + 1;
260
261         old_len = talloc_get_size(req->sconn->conn->msg_state->msg);
262
263         len = MIN(SVAL(msg, 0), smbreq_bufrem(req, msg+2));
264
265         tmp = talloc_realloc(req->sconn->conn->msg_state,
266                              req->sconn->conn->msg_state->msg,
267                              char, old_len + len);
268
269         if (tmp == NULL) {
270                 reply_nterror(req, NT_STATUS_NO_MEMORY);
271                 END_PROFILE(SMBsendtxt);
272                 return;
273         }
274
275         req->sconn->conn->msg_state->msg = tmp;
276
277         memcpy(&req->sconn->conn->msg_state->msg[old_len], msg+2, len);
278
279         DEBUG( 3, ( "SMBsendtxt\n" ) );
280
281         reply_outbuf(req, 0, 0);
282
283         END_PROFILE(SMBsendtxt);
284         return;
285 }
286
287 /****************************************************************************
288  Reply to a sendend.
289  conn POINTER CAN BE NULL HERE !
290 ****************************************************************************/
291
292 void reply_sendend(struct smb_request *req)
293 {
294         START_PROFILE(SMBsendend);
295
296         if (! (*lp_msg_command(talloc_tos()))) {
297                 reply_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
298                 END_PROFILE(SMBsendend);
299                 return;
300         }
301
302         DEBUG(3,("SMBsendend\n"));
303
304         msg_deliver(req->sconn->conn->msg_state);
305
306         TALLOC_FREE(req->sconn->conn->msg_state);
307
308         reply_outbuf(req, 0, 0);
309
310         END_PROFILE(SMBsendend);
311         return;
312 }