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