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