r23779: Change from v2 or later to v3 or later.
[kai/samba.git] / source / 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21    This file handles the messaging system calls for winpopup style
22    messages
23 */
24
25
26 #include "includes.h"
27
28 extern userdom_struct current_user_info;
29
30 /* look in server.c for some explanation of these variables */
31 static char msgbuf[1600];
32 static int msgpos;
33 static fstring msgfrom;
34 static fstring msgto;
35
36 /****************************************************************************
37  Deliver the message.
38 ****************************************************************************/
39
40 static void msg_deliver(void)
41 {
42         pstring name;
43         int i;
44         int fd;
45         char *msg;
46         int len;
47         ssize_t sz;
48
49         if (! (*lp_msg_command())) {
50                 DEBUG(1,("no messaging command specified\n"));
51                 msgpos = 0;
52                 return;
53         }
54
55         /* put it in a temporary file */
56         slprintf(name,sizeof(name)-1, "%s/msg.XXXXXX",tmpdir());
57         fd = smb_mkstemp(name);
58
59         if (fd == -1) {
60                 DEBUG(1,("can't open message file %s\n",name));
61                 return;
62         }
63
64         /*
65          * Incoming message is in DOS codepage format. Convert to UNIX.
66          */
67   
68         if ((len = (int)convert_string_allocate(NULL,CH_DOS, CH_UNIX, msgbuf, msgpos, (void **)(void *)&msg, True)) < 0 || !msg) {
69                 DEBUG(3,("Conversion failed, delivering message in DOS codepage format\n"));
70                 for (i = 0; i < msgpos;) {
71                         if (msgbuf[i] == '\r' && i < (msgpos-1) && msgbuf[i+1] == '\n') {
72                                 i++;
73                                 continue;
74                         }
75                         sz = write(fd, &msgbuf[i++], 1);
76                         if ( sz != 1 ) {
77                                 DEBUG(0,("Write error to fd %d: %ld(%d)\n",fd, (long)sz, errno ));
78                         }
79                 }
80         } else {
81                 for (i = 0; i < len;) {
82                         if (msg[i] == '\r' && i < (len-1) && msg[i+1] == '\n') {
83                                 i++;
84                                 continue;
85                         }
86                         sz = write(fd, &msg[i++],1);
87                         if ( sz != 1 ) {
88                                 DEBUG(0,("Write error to fd %d: %ld(%d)\n",fd, (long)sz, errno ));
89                         }
90                 }
91                 SAFE_FREE(msg);
92         }
93         close(fd);
94
95         /* run the command */
96         if (*lp_msg_command()) {
97                 fstring alpha_msgfrom;
98                 fstring alpha_msgto;
99                 pstring s;
100
101                 pstrcpy(s,lp_msg_command());
102                 pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,NULL,sizeof(alpha_msgfrom)));
103                 pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,NULL,sizeof(alpha_msgto)));
104                 standard_sub_basic(current_user_info.smb_name,
105                                 current_user_info.domain, s, sizeof(s));
106                 pstring_sub(s,"%s",name);
107                 smbrun(s,NULL);
108         }
109
110         msgpos = 0;
111 }
112
113 /****************************************************************************
114  Reply to a sends.
115  conn POINTER CAN BE NULL HERE !
116 ****************************************************************************/
117
118 int reply_sends(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
119 {
120         int len;
121         char *msg;
122         int outsize = 0;
123         char *p;
124
125         START_PROFILE(SMBsends);
126
127         msgpos = 0;
128
129         if (! (*lp_msg_command())) {
130                 END_PROFILE(SMBsends);
131                 return(ERROR_DOS(ERRSRV,ERRmsgoff));
132         }
133
134         outsize = set_message(inbuf,outbuf,0,0,True);
135
136         p = smb_buf(inbuf)+1;
137         p += srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), msgfrom, p,
138                              sizeof(msgfrom), STR_ASCII|STR_TERMINATE) + 1;
139         p += srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), msgto, p,
140                              sizeof(msgto), STR_ASCII|STR_TERMINATE) + 1;
141
142         msg = p;
143
144         len = SVAL(msg,0);
145         len = MIN(len,sizeof(msgbuf)-msgpos);
146
147         memset(msgbuf,'\0',sizeof(msgbuf));
148
149         memcpy(&msgbuf[msgpos],msg+2,len);
150         msgpos += len;
151
152         msg_deliver();
153
154         END_PROFILE(SMBsends);
155         return(outsize);
156 }
157
158 /****************************************************************************
159  Reply to a sendstrt.
160  conn POINTER CAN BE NULL HERE !
161 ****************************************************************************/
162
163 int reply_sendstrt(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
164 {
165         int outsize = 0;
166         char *p;
167
168         START_PROFILE(SMBsendstrt);
169
170         if (! (*lp_msg_command())) {
171                 END_PROFILE(SMBsendstrt);
172                 return(ERROR_DOS(ERRSRV,ERRmsgoff));
173         }
174
175         outsize = set_message(inbuf,outbuf,1,0,True);
176
177         memset(msgbuf,'\0',sizeof(msgbuf));
178         msgpos = 0;
179
180         p = smb_buf(inbuf)+1;
181         p += srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), msgfrom, p,
182                              sizeof(msgfrom), STR_ASCII|STR_TERMINATE) + 1;
183         p += srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), msgto, p,
184                              sizeof(msgto), STR_ASCII|STR_TERMINATE) + 1;
185
186         DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
187
188         END_PROFILE(SMBsendstrt);
189         return(outsize);
190 }
191
192 /****************************************************************************
193  Reply to a sendtxt.
194  conn POINTER CAN BE NULL HERE !
195 ****************************************************************************/
196
197 int reply_sendtxt(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
198 {
199         int len;
200         int outsize = 0;
201         char *msg;
202         START_PROFILE(SMBsendtxt);
203
204         if (! (*lp_msg_command())) {
205                 END_PROFILE(SMBsendtxt);
206                 return(ERROR_DOS(ERRSRV,ERRmsgoff));
207         }
208
209         outsize = set_message(inbuf,outbuf,0,0,True);
210
211         msg = smb_buf(inbuf) + 1;
212
213         len = SVAL(msg,0);
214         len = MIN(len,sizeof(msgbuf)-msgpos);
215
216         memcpy(&msgbuf[msgpos],msg+2,len);
217         msgpos += len;
218
219         DEBUG( 3, ( "SMBsendtxt\n" ) );
220
221         END_PROFILE(SMBsendtxt);
222         return(outsize);
223 }
224
225 /****************************************************************************
226  Reply to a sendend.
227  conn POINTER CAN BE NULL HERE !
228 ****************************************************************************/
229
230 int reply_sendend(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
231 {
232         int outsize = 0;
233         START_PROFILE(SMBsendend);
234
235         if (! (*lp_msg_command())) {
236                 END_PROFILE(SMBsendend);
237                 return(ERROR_DOS(ERRSRV,ERRmsgoff));
238         }
239
240         outsize = set_message(inbuf,outbuf,0,0,True);
241
242         DEBUG(3,("SMBsendend\n"));
243
244         msg_deliver();
245
246         END_PROFILE(SMBsendend);
247         return(outsize);
248 }