changes to sync with 2.2. tree
[nivanova/samba-autobuild/.git] / source3 / smbd / message.c
1 #define OLD_NTDOMAIN 1
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    SMB messaging
7    Copyright (C) Andrew Tridgell 1992-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 /*
24    This file handles the messaging system calls for winpopup style
25    messages
26 */
27
28
29 #include "includes.h"
30
31 /* look in server.c for some explanation of these variables */
32 extern int DEBUGLEVEL;
33
34
35 static char msgbuf[1600];
36 static int msgpos=0;
37 static fstring msgfrom="";
38 static fstring msgto="";
39
40 /****************************************************************************
41 deliver the message
42 ****************************************************************************/
43 static void msg_deliver(void)
44 {
45   pstring s;
46   fstring name;
47   int i;
48   int fd;
49
50   if (! (*lp_msg_command()))
51     {
52       DEBUG(1,("no messaging command specified\n"));
53       msgpos = 0;
54       return;
55     }
56
57   /* put it in a temporary file */
58   slprintf(s,sizeof(s)-1, "%s/msg.XXXXXX",tmpdir());
59   fstrcpy(name,(char *)smbd_mktemp(s));
60
61   fd = sys_open(name,O_WRONLY|O_CREAT|O_TRUNC|O_EXCL,0600);
62   if (fd == -1) {
63     DEBUG(1,("can't open message file %s\n",name));
64     return;
65   }
66
67   /*
68    * Incoming message is in DOS codepage format. Convert to UNIX in
69    * place.
70    */
71
72   if(msgpos > 0) {
73     msgbuf[msgpos] = '\0'; /* Ensure null terminated. */
74     dos_to_unix(msgbuf,True);
75   }
76
77   for (i=0;i<msgpos;) {
78     if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
79       i++; continue;      
80     }
81     write(fd,&msgbuf[i++],1);
82   }
83   close(fd);
84
85
86   /* run the command */
87   if (*lp_msg_command())
88     {
89       fstring alpha_msgfrom;
90       fstring alpha_msgto;
91
92       pstrcpy(s,lp_msg_command());
93       pstring_sub(s,"%s",name);
94       pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,sizeof(alpha_msgfrom)));
95       pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,sizeof(alpha_msgto)));
96       standard_sub_basic(s);
97       smbrun(s,NULL,False);
98     }
99
100   msgpos = 0;
101 }
102
103
104
105 /****************************************************************************
106   reply to a sends
107 ****************************************************************************/
108 int reply_sends(connection_struct *conn,
109                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
110 {
111   int len;
112   char *orig,*dest,*msg;
113   int outsize = 0;
114   START_PROFILE(SMBsends);
115
116   msgpos = 0;
117
118   if (! (*lp_msg_command())) {
119     END_PROFILE(SMBsends);
120     return(ERROR(ERRSRV,ERRmsgoff));
121   }
122
123   outsize = set_message(outbuf,0,0,True);
124
125   orig = smb_buf(inbuf)+1;
126   dest = skip_string(orig,1)+1;
127   msg = skip_string(dest,1)+1;
128
129   fstrcpy(msgfrom,orig);
130   fstrcpy(msgto,dest);
131
132   len = SVAL(msg,0);
133   len = MIN(len,sizeof(msgbuf)-msgpos);
134
135   memset(msgbuf,'\0',sizeof(msgbuf));
136
137   memcpy(&msgbuf[msgpos],msg+2,len);
138   msgpos += len;
139
140   DEBUG( 3, ( "SMBsends (from %s to %s)\n", orig, dest ) );
141
142   msg_deliver();
143
144   END_PROFILE(SMBsends);
145   return(outsize);
146 }
147
148
149 /****************************************************************************
150   reply to a sendstrt
151 ****************************************************************************/
152 int reply_sendstrt(connection_struct *conn,
153                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
154 {
155   char *orig,*dest;
156   int outsize = 0;
157   START_PROFILE(SMBsendstrt);
158
159   if (! (*lp_msg_command())) {
160     END_PROFILE(SMBsendstrt);
161     return(ERROR(ERRSRV,ERRmsgoff));
162   }
163
164   outsize = set_message(outbuf,1,0,True);
165
166   memset(msgbuf,'\0',sizeof(msgbuf));
167   msgpos = 0;
168
169   orig = smb_buf(inbuf)+1;
170   dest = skip_string(orig,1)+1;
171
172   fstrcpy(msgfrom,orig);
173   fstrcpy(msgto,dest);
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(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(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 }
238
239 #undef OLD_NTDOMAIN