sync 3.0 into HEAD for the last time
[nivanova/samba-autobuild/.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 2 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 static void msg_deliver(void)
40 {
41   pstring name;
42   int i;
43   int fd;
44   char *msg;
45   int len;
46
47   if (! (*lp_msg_command()))
48     {
49       DEBUG(1,("no messaging command specified\n"));
50       msgpos = 0;
51       return;
52     }
53
54   /* put it in a temporary file */
55   slprintf(name,sizeof(name)-1, "%s/msg.XXXXXX",tmpdir());
56   fd = smb_mkstemp(name);
57
58   if (fd == -1) {
59     DEBUG(1,("can't open message file %s\n",name));
60     return;
61   }
62
63   /*
64    * Incoming message is in DOS codepage format. Convert to UNIX.
65    */
66   
67   if ((len = convert_string_allocate(NULL,CH_DOS, CH_UNIX, msgbuf, msgpos, (void **) &msg)) < 0 || !msg) {
68     DEBUG(3,("Conversion failed, delivering message in DOS codepage format\n"));
69     for (i = 0; i < msgpos;) {
70       if (msgbuf[i] == '\r' && i < (msgpos-1) && msgbuf[i+1] == '\n') {
71         i++; continue;
72       }
73       write(fd, &msgbuf[i++], 1);
74     }
75   } else {
76     for (i = 0; i < len;) {
77       if (msg[i] == '\r' && i < (len-1) && msg[i+1] == '\n') {
78         i++; continue;
79       }
80       write(fd, &msg[i++],1);
81     }
82     SAFE_FREE(msg);
83   }
84   close(fd);
85
86
87   /* run the command */
88   if (*lp_msg_command())
89     {
90       fstring alpha_msgfrom;
91       fstring alpha_msgto;
92       pstring s;
93
94       pstrcpy(s,lp_msg_command());
95       pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,NULL,sizeof(alpha_msgfrom)));
96       pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,NULL,sizeof(alpha_msgto)));
97       standard_sub_basic(current_user_info.smb_name, s, sizeof(s));
98       pstring_sub(s,"%s",name);
99       smbrun(s,NULL);
100     }
101
102   msgpos = 0;
103 }
104
105
106
107 /****************************************************************************
108   reply to a sends
109 ****************************************************************************/
110 int reply_sends(connection_struct *conn,
111                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
112 {
113   int len;
114   char *msg;
115   int outsize = 0;
116   char *p;
117
118   START_PROFILE(SMBsends);
119
120   msgpos = 0;
121
122   if (! (*lp_msg_command())) {
123     END_PROFILE(SMBsends);
124     return(ERROR_DOS(ERRSRV,ERRmsgoff));
125   }
126
127   outsize = set_message(outbuf,0,0,True);
128
129   p = smb_buf(inbuf)+1;
130   p += srvstr_pull_buf(inbuf, msgfrom, p, sizeof(msgfrom), STR_TERMINATE) + 1;
131   p += srvstr_pull_buf(inbuf, msgto, p, sizeof(msgto), STR_TERMINATE) + 1;
132
133   msg = p;
134
135   len = SVAL(msg,0);
136   len = MIN(len,sizeof(msgbuf)-msgpos);
137
138   memset(msgbuf,'\0',sizeof(msgbuf));
139
140   memcpy(&msgbuf[msgpos],msg+2,len);
141   msgpos += len;
142
143   msg_deliver();
144
145   END_PROFILE(SMBsends);
146   return(outsize);
147 }
148
149
150 /****************************************************************************
151   reply to a sendstrt
152 ****************************************************************************/
153 int reply_sendstrt(connection_struct *conn,
154                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
155 {
156   int outsize = 0;
157   char *p;
158
159   START_PROFILE(SMBsendstrt);
160
161   if (! (*lp_msg_command())) {
162     END_PROFILE(SMBsendstrt);
163     return(ERROR_DOS(ERRSRV,ERRmsgoff));
164   }
165
166   outsize = set_message(outbuf,1,0,True);
167
168   memset(msgbuf,'\0',sizeof(msgbuf));
169   msgpos = 0;
170
171   p = smb_buf(inbuf)+1;
172   p += srvstr_pull_buf(inbuf, msgfrom, p, sizeof(msgfrom), STR_TERMINATE) + 1;
173   p += srvstr_pull_buf(inbuf, msgto, p, sizeof(msgto), STR_TERMINATE) + 1;
174
175   DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
176
177   END_PROFILE(SMBsendstrt);
178   return(outsize);
179 }
180
181
182 /****************************************************************************
183   reply to a sendtxt
184 ****************************************************************************/
185 int reply_sendtxt(connection_struct *conn,
186                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
187 {
188   int len;
189   int outsize = 0;
190   char *msg;
191   START_PROFILE(SMBsendtxt);
192
193   if (! (*lp_msg_command())) {
194     END_PROFILE(SMBsendtxt);
195     return(ERROR_DOS(ERRSRV,ERRmsgoff));
196   }
197
198   outsize = set_message(outbuf,0,0,True);
199
200   msg = smb_buf(inbuf) + 1;
201
202   len = SVAL(msg,0);
203   len = MIN(len,sizeof(msgbuf)-msgpos);
204
205   memcpy(&msgbuf[msgpos],msg+2,len);
206   msgpos += len;
207
208   DEBUG( 3, ( "SMBsendtxt\n" ) );
209
210   END_PROFILE(SMBsendtxt);
211   return(outsize);
212 }
213
214
215 /****************************************************************************
216   reply to a sendend
217 ****************************************************************************/
218 int reply_sendend(connection_struct *conn,
219                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
220 {
221   int outsize = 0;
222   START_PROFILE(SMBsendend);
223
224   if (! (*lp_msg_command())) {
225     END_PROFILE(SMBsendend);
226     return(ERROR_DOS(ERRSRV,ERRmsgoff));
227   }
228
229   outsize = set_message(outbuf,0,0,True);
230
231   DEBUG(3,("SMBsendend\n"));
232
233   msg_deliver();
234
235   END_PROFILE(SMBsendend);
236   return(outsize);
237 }