add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / smbd / message.c
blobd4e8871da945f181af63d468102093c5fb9e0c64
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB messaging
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
22 This file handles the messaging system calls for winpopup style
23 messages
27 #include "includes.h"
29 /* look in server.c for some explanation of these variables */
31 static char msgbuf[1600];
32 static int msgpos=0;
33 static fstring msgfrom="";
34 static fstring msgto="";
36 /****************************************************************************
37 deliver the message
38 ****************************************************************************/
39 static void msg_deliver(void)
41 pstring name;
42 int i;
43 int fd;
45 if (! (*lp_msg_command()))
47 DEBUG(1,("no messaging command specified\n"));
48 msgpos = 0;
49 return;
52 /* put it in a temporary file */
53 slprintf(name,sizeof(name)-1, "%s/msg.XXXXXX",tmpdir());
54 fd = smb_mkstemp(name);
56 if (fd == -1) {
57 DEBUG(1,("can't open message file %s\n",name));
58 return;
62 * Incoming message is in DOS codepage format. Convert to UNIX.
65 if(msgpos > 0) {
66 msgbuf[msgpos] = '\0'; /* Ensure null terminated. */
67 pstrcpy(msgbuf,dos_to_unix_static(msgbuf));
70 for (i=0;i<msgpos;) {
71 if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
72 i++; continue;
74 write(fd,&msgbuf[i++],1);
76 close(fd);
79 /* run the command */
80 if (*lp_msg_command())
82 fstring alpha_msgfrom;
83 fstring alpha_msgto;
84 pstring s;
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(s,sizeof(s));
90 pstring_sub(s,"%s",name);
91 smbrun(s,NULL);
94 msgpos = 0;
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)
105 int len;
106 char *orig,*dest,*msg;
107 int outsize = 0;
108 START_PROFILE(SMBsends);
110 msgpos = 0;
112 if (! (*lp_msg_command())) {
113 END_PROFILE(SMBsends);
114 return(ERROR_DOS(ERRSRV,ERRmsgoff));
117 outsize = set_message(outbuf,0,0,True);
119 orig = smb_buf(inbuf)+1;
120 dest = skip_string(orig,1)+1;
121 msg = skip_string(dest,1)+1;
123 fstrcpy(msgfrom,orig);
124 fstrcpy(msgto,dest);
126 len = SVAL(msg,0);
127 len = MIN(len,sizeof(msgbuf)-msgpos);
129 memset(msgbuf,'\0',sizeof(msgbuf));
131 memcpy(&msgbuf[msgpos],msg+2,len);
132 msgpos += len;
134 DEBUG( 3, ( "SMBsends (from %s to %s)\n", orig, dest ) );
136 msg_deliver();
138 END_PROFILE(SMBsends);
139 return(outsize);
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)
149 char *orig,*dest;
150 int outsize = 0;
151 START_PROFILE(SMBsendstrt);
153 if (! (*lp_msg_command())) {
154 END_PROFILE(SMBsendstrt);
155 return(ERROR_DOS(ERRSRV,ERRmsgoff));
158 outsize = set_message(outbuf,1,0,True);
160 memset(msgbuf,'\0',sizeof(msgbuf));
161 msgpos = 0;
163 orig = smb_buf(inbuf)+1;
164 dest = skip_string(orig,1)+1;
166 fstrcpy(msgfrom,orig);
167 fstrcpy(msgto,dest);
169 DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
171 END_PROFILE(SMBsendstrt);
172 return(outsize);
176 /****************************************************************************
177 reply to a sendtxt
178 ****************************************************************************/
179 int reply_sendtxt(connection_struct *conn,
180 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
182 int len;
183 int outsize = 0;
184 char *msg;
185 START_PROFILE(SMBsendtxt);
187 if (! (*lp_msg_command())) {
188 END_PROFILE(SMBsendtxt);
189 return(ERROR_DOS(ERRSRV,ERRmsgoff));
192 outsize = set_message(outbuf,0,0,True);
194 msg = smb_buf(inbuf) + 1;
196 len = SVAL(msg,0);
197 len = MIN(len,sizeof(msgbuf)-msgpos);
199 memcpy(&msgbuf[msgpos],msg+2,len);
200 msgpos += len;
202 DEBUG( 3, ( "SMBsendtxt\n" ) );
204 END_PROFILE(SMBsendtxt);
205 return(outsize);
209 /****************************************************************************
210 reply to a sendend
211 ****************************************************************************/
212 int reply_sendend(connection_struct *conn,
213 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
215 int outsize = 0;
216 START_PROFILE(SMBsendend);
218 if (! (*lp_msg_command())) {
219 END_PROFILE(SMBsendend);
220 return(ERROR_DOS(ERRSRV,ERRmsgoff));
223 outsize = set_message(outbuf,0,0,True);
225 DEBUG(3,("SMBsendend\n"));
227 msg_deliver();
229 END_PROFILE(SMBsendend);
230 return(outsize);