more merging voodoo
[tprouty/samba.git] / source / smbd / message.c
1 #define OLD_NTDOMAIN 1
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    SMB messaging
7    Copyright (C) Andrew Tridgell 1992-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 /*
24    This file handles the messaging system calls for winpopup style
25    messages
26 */
27
28
29 #include "includes.h"
30
31 /* look in server.c for some explanation of these variables */
32 extern int DEBUGLEVEL;
33
34
35 static char msgbuf[1600];
36 static int msgpos=0;
37 static fstring msgfrom="";
38 static fstring msgto="";
39
40 /****************************************************************************
41 deliver the message
42 ****************************************************************************/
43 static void msg_deliver(void)
44 {
45   pstring s;
46   fstring name;
47   int i;
48   int fd;
49
50   if (! (*lp_msg_command()))
51     {
52       DEBUG(1,("no messaging command specified\n"));
53       msgpos = 0;
54       return;
55     }
56
57   /* put it in a temporary file */
58   slprintf(s,sizeof(s)-1, "%s/msg.XXXXXX",tmpdir());
59   fstrcpy(name,(char *)smbd_mktemp(s));
60
61   fd = sys_open(name,O_WRONLY|O_CREAT|O_TRUNC|O_EXCL,0600);
62   if (fd == -1) {
63     DEBUG(1,("can't open message file %s\n",name));
64     return;
65   }
66
67   /*
68    * Incoming message is in DOS codepage format. Convert to UNIX in
69    * place.
70    */
71
72   if(msgpos > 0) {
73     msgbuf[msgpos] = '\0'; /* Ensure null terminated. */
74     dos_to_unix(msgbuf,True);
75   }
76
77   for (i=0;i<msgpos;) {
78     if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
79       i++; continue;      
80     }
81     write(fd,&msgbuf[i++],1);
82   }
83   close(fd);
84
85
86   /* run the command */
87   if (*lp_msg_command())
88     {
89       fstring alpha_msgfrom;
90       fstring alpha_msgto;
91
92       pstrcpy(s,lp_msg_command());
93       pstring_sub(s,"%s",name);
94       pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,sizeof(alpha_msgfrom)));
95       pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,sizeof(alpha_msgto)));
96       standard_sub_basic(s);
97       smbrun(s,NULL,False);
98     }
99
100   msgpos = 0;
101 }
102
103
104
105 /****************************************************************************
106   reply to a sends
107 ****************************************************************************/
108 int reply_sends(connection_struct *conn,
109                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
110 {
111   int len;
112   char *orig,*dest,*msg;
113   int outsize = 0;
114
115   msgpos = 0;
116
117   if (! (*lp_msg_command()))
118     return(ERROR(ERRSRV,ERRmsgoff));
119
120   outsize = set_message(outbuf,0,0,True);
121
122   orig = smb_buf(inbuf)+1;
123   dest = skip_string(orig,1)+1;
124   msg = skip_string(dest,1)+1;
125
126   fstrcpy(msgfrom,orig);
127   fstrcpy(msgto,dest);
128
129   len = SVAL(msg,0);
130   len = MIN(len,sizeof(msgbuf)-msgpos);
131
132   memset(msgbuf,'\0',sizeof(msgbuf));
133
134   memcpy(&msgbuf[msgpos],msg+2,len);
135   msgpos += len;
136
137   DEBUG( 3, ( "SMBsends (from %s to %s)\n", orig, dest ) );
138
139   msg_deliver();
140
141   return(outsize);
142 }
143
144
145 /****************************************************************************
146   reply to a sendstrt
147 ****************************************************************************/
148 int reply_sendstrt(connection_struct *conn,
149                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
150 {
151   char *orig,*dest;
152   int outsize = 0;
153
154   if (! (*lp_msg_command()))
155     return(ERROR(ERRSRV,ERRmsgoff));
156
157   outsize = set_message(outbuf,1,0,True);
158
159   memset(msgbuf,'\0',sizeof(msgbuf));
160   msgpos = 0;
161
162   orig = smb_buf(inbuf)+1;
163   dest = skip_string(orig,1)+1;
164
165   fstrcpy(msgfrom,orig);
166   fstrcpy(msgto,dest);
167
168   DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
169
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
184   if (! (*lp_msg_command()))
185     return(ERROR(ERRSRV,ERRmsgoff));
186
187   outsize = set_message(outbuf,0,0,True);
188
189   msg = smb_buf(inbuf) + 1;
190
191   len = SVAL(msg,0);
192   len = MIN(len,sizeof(msgbuf)-msgpos);
193
194   memcpy(&msgbuf[msgpos],msg+2,len);
195   msgpos += len;
196
197   DEBUG( 3, ( "SMBsendtxt\n" ) );
198
199   return(outsize);
200 }
201
202
203 /****************************************************************************
204   reply to a sendend
205 ****************************************************************************/
206 int reply_sendend(connection_struct *conn,
207                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
208 {
209   int outsize = 0;
210
211   if (! (*lp_msg_command()))
212     return(ERROR(ERRSRV,ERRmsgoff));
213
214   outsize = set_message(outbuf,0,0,True);
215
216   DEBUG(3,("SMBsendend\n"));
217
218   msg_deliver();
219
220   return(outsize);
221 }
222
223 #undef OLD_NTDOMAIN