File: task_server.c

package info (click to toggle)
dnprogs 2.52
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,644 kB
  • ctags: 4,021
  • sloc: ansic: 26,737; cpp: 10,666; makefile: 832; sh: 537; awk: 13
file content (294 lines) | stat: -rw-r--r-- 6,540 bytes parent folder | download | duplicates (4)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/******************************************************************************
    (c) 1999-2002 Christine Caulfield               christine.caulfield@googlemail.com

    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
    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.
 ******************************************************************************
*/
////
// task_server.c
// Task processing module
////

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#ifdef DNETUSE_DEVPTS
#include <pty.h>
#endif
#include "dn_endian.h"

static void execute_file(char *name, int newsock, int verbose);
static void copy (int pty, int sock, pid_t pid);

void task_server(int newsock, int verbosity, int secure)
{
    struct sockaddr_dn sockaddr;
    char name[200];
    char tryname[PATH_MAX];
    char *taskdir;
    struct stat st;
    socklen_t len = sizeof(sockaddr);
    int i;

    /* Get the object that was wanted */
    getsockname(newsock, (struct sockaddr *)&sockaddr, &len);
    if (sockaddr.sdn_objnamel)
    {

	strncpy(name, (char*)sockaddr.sdn_objname, dn_ntohs(sockaddr.sdn_objnamel));
	name[dn_ntohs(sockaddr.sdn_objnamel)] = '\0';
    }
    else
    {
	DNETLOG((LOG_WARNING, "connection to object number %d rejected\n", sockaddr.sdn_objnum));
	close(newsock);
	return; // Don't do object numbers !
    }

// Convert the name to lower case
    for (i=0; i<strlen(name); i++)
    {
	if (isupper(name[i])) name[i] = tolower(name[i]);
    }


    if (verbosity) DNETLOG((LOG_INFO, "got connection for %s\n", name));

    // Reject anything starting / or containing .. as
    // a security problem
    if (name[0] == '/' ||
	strstr(name, ".."))
    {
	DNETLOG((LOG_WARNING, "Rejecting %s as a security risk", name));
	dnet_reject(newsock, DNSTAT_OBJECT, NULL, 0);
	return;
    }


    // Look for the file.
    //
    // a) in the home directory (if not secure)
    if (!secure)
    {
	if (!stat(name, &st) && (st.st_mode & S_IXUSR))
	{
	    execute_file(name, newsock, verbosity);
	    return;
	}
    }

    // b) in $DNTASKDIR
    taskdir = getenv("DNTASKDIR");
    if (taskdir)
    {
	strcpy(tryname, taskdir);
	strcat(tryname, "/");
	strcat(tryname, name);
	if (!stat(tryname, &st) && (st.st_mode & S_IXUSR))
	{
	    execute_file(tryname, newsock, verbosity);
	    return;
	}
    }

    // c) in /usr/local/decnet/tasks
    strcpy(tryname, "/usr/local/decnet/tasks/");
    strcat(tryname, name);
    if (!stat(tryname, &st) && (st.st_mode & S_IXUSR))
    {
	execute_file(tryname, newsock, verbosity);
	return;
    }

// Can't find it.
    DNETLOG((LOG_INFO, "Unable to execute task %s, file was not found", name));
    dnet_reject(newsock, DNSTAT_OBJECT, NULL, 0);
}
/*
 * All of the mucking about with ptys is to do the (apparently)
 * trivial task of converting LF line endings into CRLF
 * line endings!
 */
static void execute_file(char *name, int newsock, int verbose)
{
    int	        i,c, t;
    pid_t       pid;
    int         pty;
    struct stat	stb;
    char	ptyname[] = "/dev/ptyCP";
    int	        gotpty =0;
    char       *line;
    char       *argv[2] = {name, NULL};
    char       *env[2] = {NULL};

    if (verbose) DNETLOG((LOG_INFO, "About to exec %s", name));
    dnet_accept(newsock, 0, NULL, 0);

#ifdef DNETUSE_DEVPTS
    if (openpty(&pty, &t,NULL, NULL, NULL) != 0)
    {
	DNETLOG((LOG_ERR, "No ptys available for object execution"));
	exit(-1);
    }
#else

    for (c='p'; c <= 'z'; c++)
    {
	line = ptyname;
	line[strlen("/dev/pty")] = c;
	line[strlen("/dev/ptyC")] = '0';
	if (stat(line,&stb) < 0)
	    break;
	for (i=0; i < 16; i++)
	{
	    line[strlen("/dev/ptyC")]= "0123456789abcdef"[i];
	    if ( (pty=open(line,O_RDWR)) > 0)
	    {
		gotpty = 1;
		break;
	    }
	}
	if (gotpty) break;
    }

    if (!gotpty)
    {
	DNETLOG((LOG_ERR, "No ptys available for connection"));
	return;
    }


    line[strlen("/dev/")] = 't';
    if ( (t=open(line,O_RDWR)) < 0)
    {
	DNETLOG((LOG_ERR, "Error connecting to physical terminal: %m"));
	return;
    }
#endif

    if ( ( pid=fork() ) < 0)
    {
	DNETLOG((LOG_ERR, "Error forking"));
	return;
    }

    if (pid)  // Parent
    {
	close(t); // close slave

	// do copy from pty to socket.
	copy(pty, newsock, pid);
	return ;
    }

    setsid();

    close(pty); close(newsock);
    if (t != 0) dup2 (t,0);
    if (t != 1) dup2 (t,1);
    if (t != 2) dup2 (t,2);

    if (t > 2) close(t);

    putenv("TERM=vt100");
    execve(name, argv, env);
    DNETLOG((LOG_ERR, "Error executing command"));
    return;
}


// Catch child process shutdown
static void sigchild(int s)
{
    int status, pid;

    // Make sure we reap all children
    do
    {
	pid = waitpid(-1, &status, WNOHANG);
    }
    while (pid > 0);
}

// Just copy stuff from the slave pty to the DECnet socket
static void copy (int pty, int sock, pid_t pid)
{
    char	buf[1024];
    fd_set	rdfs;
    int	        cnt;
    struct      sigaction siga;
    sigset_t    ss;

    setsid();

    signal(SIGTSTP, SIG_IGN);
    signal(SIGTTOU, SIG_IGN);

    sigemptyset(&ss);
    siga.sa_handler=sigchild;
    siga.sa_mask  = ss;
    siga.sa_flags = SA_NOCLDSTOP;
    sigaction(SIGCHLD, &siga, NULL);

    for (;;)
    {
	FD_ZERO(&rdfs);
	FD_SET(pty,&rdfs);
	FD_SET(sock,&rdfs);

	if (select(FD_SETSIZE,&rdfs,NULL,NULL,NULL) > 0)
	{
	    if (FD_ISSET(pty,&rdfs))
	    {
		cnt=read(pty,buf,sizeof(buf));
		if (cnt <= 0) goto finished;
		write(sock, buf, cnt);
	    }
	    if (FD_ISSET(sock,&rdfs))
	    {
		cnt=read(sock,buf,sizeof(buf));
		if (cnt <= 0) goto finished;
		write(pty, buf, cnt);
	    }
	}
	else
	{
	    DNETLOG((LOG_INFO, "Error in select: %m"));
	    goto finished;
	}
    }

 finished:
    DNETLOG((LOG_INFO, "Task completed"));
    close(pty);
    close(sock);
    kill(pid, SIGTERM);
    exit(0);
}