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