this is the bug change to using connection_struct* instead of cnum.
[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=0;
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 *)mktemp(s));
58
59   fd = 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   for (i=0;i<msgpos;) {
66     if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
67       i++; continue;      
68     }
69     write(fd,&msgbuf[i++],1);
70   }
71   close(fd);
72
73
74   /* run the command */
75   if (*lp_msg_command())
76     {
77       pstrcpy(s,lp_msg_command());
78       string_sub(s,"%s",name);
79       string_sub(s,"%f",msgfrom);
80       string_sub(s,"%t",msgto);
81       standard_sub_basic(s);
82       smbrun(s,NULL,False);
83     }
84
85   msgpos = 0;
86 }
87
88
89
90 /****************************************************************************
91   reply to a sends
92 ****************************************************************************/
93 int reply_sends(connection_struct *conn,
94                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
95 {
96   int len;
97   char *orig,*dest,*msg;
98   int outsize = 0;
99
100   msgpos = 0;
101
102
103   if (! (*lp_msg_command()))
104     return(ERROR(ERRSRV,ERRmsgoff));
105
106   outsize = set_message(outbuf,0,0,True);
107
108   orig = smb_buf(inbuf)+1;
109   dest = skip_string(orig,1)+1;
110   msg = skip_string(dest,1)+1;
111
112   fstrcpy(msgfrom,orig);
113   fstrcpy(msgto,dest);
114
115   len = SVAL(msg,0);
116   len = MIN(len,1600-msgpos);
117
118   memcpy(&msgbuf[msgpos],msg+2,len);
119   msgpos += len;
120
121   DEBUG( 3, ( "SMBsends (from %s to %s)\n", orig, dest ) );
122
123   msg_deliver();
124
125   return(outsize);
126 }
127
128
129 /****************************************************************************
130   reply to a sendstrt
131 ****************************************************************************/
132 int reply_sendstrt(connection_struct *conn,
133                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
134 {
135   char *orig,*dest;
136   int outsize = 0;
137
138   if (! (*lp_msg_command()))
139     return(ERROR(ERRSRV,ERRmsgoff));
140
141   outsize = set_message(outbuf,1,0,True);
142
143   msgpos = 0;
144
145   orig = smb_buf(inbuf)+1;
146   dest = skip_string(orig,1)+1;
147
148   fstrcpy(msgfrom,orig);
149   fstrcpy(msgto,dest);
150
151   DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
152
153   return(outsize);
154 }
155
156
157 /****************************************************************************
158   reply to a sendtxt
159 ****************************************************************************/
160 int reply_sendtxt(connection_struct *conn,
161                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
162 {
163   int len;
164   int outsize = 0;
165   char *msg;
166
167   if (! (*lp_msg_command()))
168     return(ERROR(ERRSRV,ERRmsgoff));
169
170   outsize = set_message(outbuf,0,0,True);
171
172   msg = smb_buf(inbuf) + 1;
173
174   len = SVAL(msg,0);
175   len = MIN(len,1600-msgpos);
176
177   memcpy(&msgbuf[msgpos],msg+2,len);
178   msgpos += len;
179
180   DEBUG( 3, ( "SMBsendtxt\n" ) );
181
182   return(outsize);
183 }
184
185
186 /****************************************************************************
187   reply to a sendend
188 ****************************************************************************/
189 int reply_sendend(connection_struct *conn,
190                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
191 {
192   int outsize = 0;
193
194   if (! (*lp_msg_command()))
195     return(ERROR(ERRSRV,ERRmsgoff));
196
197   outsize = set_message(outbuf,0,0,True);
198
199   DEBUG(3,("SMBsendend\n"));
200
201   msg_deliver();
202
203   return(outsize);
204 }
205