summaryrefslogtreecommitdiffstats
path: root/source/smbd/message.c
blob: 93a2d9d850a2c7be84e1ec7de311d1c658fbf55f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB messaging
   Copyright (C) Andrew Tridgell 1992-1997
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
   This file handles the messaging system calls for winpopup style
   messages
*/


#include "includes.h"

/* look in server.c for some explanation of these variables */
extern int DEBUGLEVEL;


static char msgbuf[1600];
static int msgpos=0;
static fstring msgfrom="";
static fstring msgto="";

/****************************************************************************
deliver the message
****************************************************************************/
static void msg_deliver(void)
{
  pstring s;
  fstring name;
  int i;
  int fd;

  if (! (*lp_msg_command()))
    {
      DEBUG(1,("no messaging command specified\n"));
      msgpos = 0;
      return;
    }

  /* put it in a temporary file */
  sprintf(s,"%s/msg.XXXXXX",tmpdir());
  strcpy(name,(char *)mktemp(s));

  fd = open(name,O_WRONLY|O_CREAT|O_TRUNC|O_EXCL,0600);
  if (fd == -1) {
    DEBUG(1,("can't open message file %s\n",name));
    return;
  }

  for (i=0;i<msgpos;) {
    if (msgbuf[i]=='\r' && i<(msgpos-1) && msgbuf[i+1]=='\n') {
      i++; continue;      
    }
    write(fd,&msgbuf[i++],1);
  }
  close(fd);


  /* run the command */
  if (*lp_msg_command())
    {
      strcpy(s,lp_msg_command());
      string_sub(s,"%s",name);
      string_sub(s,"%f",msgfrom);
      string_sub(s,"%t",msgto);
      standard_sub(-1,s);
      smbrun(s,NULL,False);
    }

  msgpos = 0;
}



/****************************************************************************
  reply to a sends
****************************************************************************/
int reply_sends(char *inbuf,char *outbuf)
{
  int len;
  char *orig,*dest,*msg;
  int outsize = 0;

  msgpos = 0;


  if (! (*lp_msg_command()))
    return(ERROR(ERRSRV,ERRmsgoff));

  outsize = set_message(outbuf,0,0,True);

  orig = smb_buf(inbuf)+1;
  dest = skip_string(orig,1)+1;
  msg = skip_string(dest,1)+1;

  strcpy(msgfrom,orig);
  strcpy(msgto,dest);

  len = SVAL(msg,0);
  len = MIN(len,1600-msgpos);

  memcpy(&msgbuf[msgpos],msg+2,len);
  msgpos += len;

  DEBUG(3,("%s SMBsends (from %s to %s)\n",timestring(),orig,dest));

  msg_deliver();

  return(outsize);
}


/****************************************************************************
  reply to a sendstrt
****************************************************************************/
int reply_sendstrt(char *inbuf,char *outbuf)
{
  char *orig,*dest;
  int outsize = 0;

  if (! (*lp_msg_command()))
    return(ERROR(ERRSRV,ERRmsgoff));

  outsize = set_message(outbuf,1,0,True);

  msgpos = 0;

  orig = smb_buf(inbuf)+1;
  dest = skip_string(orig,1)+1;

  strcpy(msgfrom,orig);
  strcpy(msgto,dest);

  DEBUG(3,("%s SMBsendstrt (from %s to %s)\n",timestring(),orig,dest));

  return(outsize);
}


/****************************************************************************
  reply to a sendtxt
****************************************************************************/
int reply_sendtxt(char *inbuf,char *outbuf)
{
  int len;
  int outsize = 0;
  char *msg;

  if (! (*lp_msg_command()))
    return(ERROR(ERRSRV,ERRmsgoff));

  outsize = set_message(outbuf,0,0,True);

  msg = smb_buf(inbuf) + 1;

  len = SVAL(msg,0);
  len = MIN(len,1600-msgpos);

  memcpy(&msgbuf[msgpos],msg+2,len);
  msgpos += len;

  DEBUG(3,("%s SMBsendtxt\n",timestring()));

  return(outsize);
}


/****************************************************************************
  reply to a sendend
****************************************************************************/
int reply_sendend(char *inbuf,char *outbuf)
{
  int outsize = 0;

  if (! (*lp_msg_command()))
    return(ERROR(ERRSRV,ERRmsgoff));

  outsize = set_message(outbuf,0,0,True);

  DEBUG(3,("%s SMBsendend\n",timestring()));

  msg_deliver();

  return(outsize);
}