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