The big character set handling changeover!
[samba.git] / source / 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 extern int DEBUGLEVEL;
31
32
33 static char msgbuf[1600];
34 static int msgpos;
35 static fstring msgfrom;
36 static fstring msgto;
37
38 /****************************************************************************
39 deliver the message
40 ****************************************************************************/
41 static void msg_deliver(void)
42 {
43   pstring name;
44   int i;
45   int fd;
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(msgpos > 0) {
68     msgbuf[msgpos] = '\0'; /* Ensure null terminated. */
69   }
70
71   for (i=0;i<msgpos;) {
72     if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
73       i++; continue;      
74     }
75     write(fd,&msgbuf[i++],1);
76   }
77   close(fd);
78
79
80   /* run the command */
81   if (*lp_msg_command())
82     {
83       fstring alpha_msgfrom;
84       fstring alpha_msgto;
85       pstring s;
86
87       pstrcpy(s,lp_msg_command());
88       pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,NULL,sizeof(alpha_msgfrom)));
89       pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,NULL,sizeof(alpha_msgto)));
90       standard_sub_basic(s);
91       pstring_sub(s,"%s",name);
92       smbrun(s,NULL);
93     }
94
95   msgpos = 0;
96 }
97
98
99
100 /****************************************************************************
101   reply to a sends
102 ****************************************************************************/
103 int reply_sends(connection_struct *conn,
104                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
105 {
106   int len;
107   char *msg;
108   int outsize = 0;
109   char *p;
110
111   START_PROFILE(SMBsends);
112
113   msgpos = 0;
114
115   if (! (*lp_msg_command())) {
116     END_PROFILE(SMBsends);
117     return(ERROR(ERRSRV,ERRmsgoff));
118   }
119
120   outsize = set_message(outbuf,0,0,True);
121
122   p = smb_buf(inbuf)+1;
123   p += srvstr_pull(inbuf, msgfrom, p, sizeof(msgfrom), -1, STR_TERMINATE) + 1;
124   p += srvstr_pull(inbuf, msgto, p, sizeof(msgto), -1, STR_TERMINATE) + 1;
125
126   msg = p;
127
128   len = SVAL(msg,0);
129   len = MIN(len,sizeof(msgbuf)-msgpos);
130
131   memset(msgbuf,'\0',sizeof(msgbuf));
132
133   memcpy(&msgbuf[msgpos],msg+2,len);
134   msgpos += len;
135
136   msg_deliver();
137
138   END_PROFILE(SMBsends);
139   return(outsize);
140 }
141
142
143 /****************************************************************************
144   reply to a sendstrt
145 ****************************************************************************/
146 int reply_sendstrt(connection_struct *conn,
147                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
148 {
149   int outsize = 0;
150   char *p;
151
152   START_PROFILE(SMBsendstrt);
153
154   if (! (*lp_msg_command())) {
155     END_PROFILE(SMBsendstrt);
156     return(ERROR(ERRSRV,ERRmsgoff));
157   }
158
159   outsize = set_message(outbuf,1,0,True);
160
161   memset(msgbuf,'\0',sizeof(msgbuf));
162   msgpos = 0;
163
164   p = smb_buf(inbuf)+1;
165   p += srvstr_pull(inbuf, msgfrom, p, sizeof(msgfrom), -1, STR_TERMINATE) + 1;
166   p += srvstr_pull(inbuf, msgto, p, sizeof(msgto), -1, STR_TERMINATE) + 1;
167
168   DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
169
170   END_PROFILE(SMBsendstrt);
171   return(outsize);
172 }
173
174
175 /****************************************************************************
176   reply to a sendtxt
177 ****************************************************************************/
178 int reply_sendtxt(connection_struct *conn,
179                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
180 {
181   int len;
182   int outsize = 0;
183   char *msg;
184   START_PROFILE(SMBsendtxt);
185
186   if (! (*lp_msg_command())) {
187     END_PROFILE(SMBsendtxt);
188     return(ERROR(ERRSRV,ERRmsgoff));
189   }
190
191   outsize = set_message(outbuf,0,0,True);
192
193   msg = smb_buf(inbuf) + 1;
194
195   len = SVAL(msg,0);
196   len = MIN(len,sizeof(msgbuf)-msgpos);
197
198   memcpy(&msgbuf[msgpos],msg+2,len);
199   msgpos += len;
200
201   DEBUG( 3, ( "SMBsendtxt\n" ) );
202
203   END_PROFILE(SMBsendtxt);
204   return(outsize);
205 }
206
207
208 /****************************************************************************
209   reply to a sendend
210 ****************************************************************************/
211 int reply_sendend(connection_struct *conn,
212                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
213 {
214   int outsize = 0;
215   START_PROFILE(SMBsendend);
216
217   if (! (*lp_msg_command())) {
218     END_PROFILE(SMBsendend);
219     return(ERROR(ERRSRV,ERRmsgoff));
220   }
221
222   outsize = set_message(outbuf,0,0,True);
223
224   DEBUG(3,("SMBsendend\n"));
225
226   msg_deliver();
227
228   END_PROFILE(SMBsendend);
229   return(outsize);
230 }