fd28df0d801fa2aeab2c715d8a20d7f5d45f82dd
[samba] / source / 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   char *msg;
45   int len;
46   ssize_t sz;
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(name,sizeof(name)-1, "%s/msg.XXXXXX",tmpdir());
57   fd = smb_mkstemp(name);
58
59   if (fd == -1) {
60     DEBUG(1,("can't open message file %s\n",name));
61     return;
62   }
63
64   /*
65    * Incoming message is in DOS codepage format. Convert to UNIX.
66    */
67   
68   if ((len = (int)convert_string_allocate(NULL,CH_DOS, CH_UNIX, msgbuf, msgpos, (void **)(void *)&msg, True)) < 0 || !msg) {
69     DEBUG(3,("Conversion failed, delivering message in DOS codepage format\n"));
70     for (i = 0; i < msgpos;) {
71       if (msgbuf[i] == '\r' && i < (msgpos-1) && msgbuf[i+1] == '\n') {
72         i++; continue;
73       }
74       sz = write(fd, &msgbuf[i++], 1);
75       if ( sz != 1 ) {
76         DEBUG(0,("Write error to fd %d: %ld(%d)\n",fd, (long)sz, errno ));
77       }
78     }
79   } else {
80     for (i = 0; i < len;) {
81       if (msg[i] == '\r' && i < (len-1) && msg[i+1] == '\n') {
82         i++; continue;
83       }
84       sz = write(fd, &msg[i++],1);
85       if ( sz != 1 ) {
86         DEBUG(0,("Write error to fd %d: %ld(%d)\n",fd, (long)sz, errno ));
87       }
88     }
89     SAFE_FREE(msg);
90   }
91   close(fd);
92
93
94   /* run the command */
95   if (*lp_msg_command())
96     {
97       fstring alpha_msgfrom;
98       fstring alpha_msgto;
99       pstring s;
100
101       pstrcpy(s,lp_msg_command());
102       pstring_sub(s,"%f",alpha_strcpy(alpha_msgfrom,msgfrom,NULL,sizeof(alpha_msgfrom)));
103       pstring_sub(s,"%t",alpha_strcpy(alpha_msgto,msgto,NULL,sizeof(alpha_msgto)));
104       standard_sub_basic(current_user_info.smb_name, s, sizeof(s));
105       pstring_sub(s,"%s",name);
106       smbrun(s,NULL);
107     }
108
109   msgpos = 0;
110 }
111
112
113
114 /****************************************************************************
115   reply to a sends
116 ****************************************************************************/
117 int reply_sends(connection_struct *conn,
118                 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
119 {
120   int len;
121   char *msg;
122   int outsize = 0;
123   char *p;
124
125   START_PROFILE(SMBsends);
126
127   msgpos = 0;
128
129   if (! (*lp_msg_command())) {
130     END_PROFILE(SMBsends);
131     return(ERROR_DOS(ERRSRV,ERRmsgoff));
132   }
133
134   outsize = set_message(outbuf,0,0,True);
135
136   p = smb_buf(inbuf)+1;
137   p += srvstr_pull_buf(inbuf, msgfrom, p, sizeof(msgfrom), STR_ASCII|STR_TERMINATE) + 1;
138   p += srvstr_pull_buf(inbuf, msgto, p, sizeof(msgto), STR_ASCII|STR_TERMINATE) + 1;
139
140   msg = p;
141
142   len = SVAL(msg,0);
143   len = MIN(len,sizeof(msgbuf)-msgpos);
144
145   memset(msgbuf,'\0',sizeof(msgbuf));
146
147   memcpy(&msgbuf[msgpos],msg+2,len);
148   msgpos += len;
149
150   msg_deliver();
151
152   END_PROFILE(SMBsends);
153   return(outsize);
154 }
155
156
157 /****************************************************************************
158   reply to a sendstrt
159 ****************************************************************************/
160 int reply_sendstrt(connection_struct *conn,
161                    char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
162 {
163   int outsize = 0;
164   char *p;
165
166   START_PROFILE(SMBsendstrt);
167
168   if (! (*lp_msg_command())) {
169     END_PROFILE(SMBsendstrt);
170     return(ERROR_DOS(ERRSRV,ERRmsgoff));
171   }
172
173   outsize = set_message(outbuf,1,0,True);
174
175   memset(msgbuf,'\0',sizeof(msgbuf));
176   msgpos = 0;
177
178   p = smb_buf(inbuf)+1;
179   p += srvstr_pull_buf(inbuf, msgfrom, p, sizeof(msgfrom), STR_ASCII|STR_TERMINATE) + 1;
180   p += srvstr_pull_buf(inbuf, msgto, p, sizeof(msgto), STR_ASCII|STR_TERMINATE) + 1;
181
182   DEBUG( 3, ( "SMBsendstrt (from %s to %s)\n", msgfrom, msgto ) );
183
184   END_PROFILE(SMBsendstrt);
185   return(outsize);
186 }
187
188
189 /****************************************************************************
190   reply to a sendtxt
191 ****************************************************************************/
192 int reply_sendtxt(connection_struct *conn,
193                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
194 {
195   int len;
196   int outsize = 0;
197   char *msg;
198   START_PROFILE(SMBsendtxt);
199
200   if (! (*lp_msg_command())) {
201     END_PROFILE(SMBsendtxt);
202     return(ERROR_DOS(ERRSRV,ERRmsgoff));
203   }
204
205   outsize = set_message(outbuf,0,0,True);
206
207   msg = smb_buf(inbuf) + 1;
208
209   len = SVAL(msg,0);
210   len = MIN(len,sizeof(msgbuf)-msgpos);
211
212   memcpy(&msgbuf[msgpos],msg+2,len);
213   msgpos += len;
214
215   DEBUG( 3, ( "SMBsendtxt\n" ) );
216
217   END_PROFILE(SMBsendtxt);
218   return(outsize);
219 }
220
221
222 /****************************************************************************
223   reply to a sendend
224 ****************************************************************************/
225 int reply_sendend(connection_struct *conn,
226                   char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
227 {
228   int outsize = 0;
229   START_PROFILE(SMBsendend);
230
231   if (! (*lp_msg_command())) {
232     END_PROFILE(SMBsendend);
233     return(ERROR_DOS(ERRSRV,ERRmsgoff));
234   }
235
236   outsize = set_message(outbuf,0,0,True);
237
238   DEBUG(3,("SMBsendend\n"));
239
240   msg_deliver();
241
242   END_PROFILE(SMBsendend);
243   return(outsize);
244 }