summaryrefslogtreecommitdiffstats
path: root/qarshd.c
blob: ee9e55645835b2ad86feb1b4a6216afa3b5f5824 (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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <assert.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>


#include "sockutil.h"
#include "qarsh_packet.h"

/*
 * QA Remote Shell Daemon
 */


/* Globals */
struct sockaddr_in peername;
int child_exitted = 0;

int
setup_user(char *user, char *group)
{
	struct passwd *pw;
	struct group *grp;

	if ((pw = getpwnam(user)) == NULL) {
		syslog(LOG_WARNING, "User \"%s\" not found\n", user);
		return 0;
	}

	if (group && (grp = getgrnam(group))) {
		setgid(grp->gr_gid);
		initgroups(pw->pw_name, grp->gr_gid);
	} else {
		setgid(pw->pw_gid);
		initgroups(pw->pw_name, pw->pw_gid);
	}
	/* setuid is last so we can still do setgid and initgroups */
	setuid(pw->pw_uid);
	return 1;
}

void
sig_handler(int sig)
{
	if (sig == SIGCHLD) child_exitted++;
}

pid_t 
run_cmd(const char *cmd, int p_in, int p_out, int p_err)
{
	pid_t pid;
	int new_in, new_out, new_err;

	syslog(LOG_INFO, "Running cmdline: %s\n", cmd);
	if ((pid = fork()) < 0) {
		syslog(LOG_WARNING, 
			"Could not fork() in run_cmd(): %s\n", strerror(errno));
		exit(1);
	}

	if (pid == 0) { /* child */
		setpgrp();

		/* Connect stdin, stdout, and stderr to qarsh */
		new_in = connect_to_peer(&peername, p_in);
		if (new_in == -1) syslog(LOG_WARNING, "connect to new_in failed");
		dup2(new_in, fileno(stdin));
		new_out = connect_to_peer(&peername, p_out);
		if (new_out == -1) syslog(LOG_WARNING, "connect to new_out failed");
		dup2(new_out, fileno(stdout));
		new_err = connect_to_peer(&peername, p_err);
		if (new_err == -1) syslog(LOG_WARNING, "connect to new_err failed");
		dup2(new_err, fileno(stderr));

		execlp("sh", "sh", "-c", cmd, NULL);
		printf("exec of %s failed: %d, %s\n", cmd, errno, strerror(errno));
		exit(127);
	} 
	return pid;
}

ssize_t 
recvfile(const char *path, int if_port, size_t count, mode_t mode)
{
	int sd;
	int ofd;
	char buf[BUFSIZ];
	ssize_t nread;
	ssize_t nwrote;
	ssize_t nleft;

	/* Read count bytes from ifd (sd after we connect), 
	 * write into file @ path 
	 */

	sd = connect_to_peer(&peername, if_port);
	if (sd == -1) {
		syslog(LOG_WARNING, "connect to if_port failed\n");
		return -1;
	}

	if ((ofd = open(path, O_TRUNC|O_CREAT|O_WRONLY, mode)) < 0) {
		syslog(LOG_WARNING, "Could not open %s to recv file: %s\n",
		       path, strerror(errno));
		return -1;
	}
	
	fchmod(ofd, mode);

	nleft = count;
	while (nleft > 0) {
		nread = read(sd, buf, BUFSIZ);
		if (nread < 0) {
			return nread;
		} else if (nread == 0) {  /* EOF */
			break;
		}

		nwrote = write(ofd, buf, nread);
		nleft -= nread;
	}

	if (nleft != 0) {
		unlink(path);
		syslog(LOG_WARNING, "Short file transfer in recvfile(), "
		       "%d bytes lost, wanted %d\n", nleft, count);
	}

	close(sd);
	close(ofd);

	return count - nleft;
}

struct qa_packet *
rstat(const char *path)
{
	struct stat sb;
	struct qa_packet *rp;

	if (stat(path, &sb) < 0) {
		rp = make_qp_returncode(-1, errno, strerror(errno));
	} else {
		rp =  make_qp_rstat(path, &sb);
	}

	return rp;
}

void
handle_packets(int infd)
{
	fd_set rfds;
	int nfd;
	struct timespec timeout;
	struct qa_packet *qp = NULL, *rp = NULL;
	sigset_t sigmask, orig_sigmask;
	struct sigaction sa;

	pid_t child_pid = 0;
	int child_status;
	ssize_t nbytes;	

	sigemptyset(&sigmask);
	sigaddset(&sigmask, SIGCHLD);
	sigprocmask(SIG_BLOCK, &sigmask, &orig_sigmask);
	sa.sa_handler = sig_handler;
	sa.sa_mask = sigmask;
	sa.sa_flags = SA_RESTART;
	sigaction(SIGCHLD, &sa, NULL);

	for (;;) {
		FD_SET(infd, &rfds);
		timeout.tv_sec = 3;

		nfd = pselect(infd+1, &rfds, NULL, NULL, &timeout, &orig_sigmask);

		if (child_exitted) {
			waitpid(child_pid, &child_status, 0);
			child_exitted--;
			rp = make_qp_cmdexit(child_pid, child_status);
			send_packet(fileno(stdout), rp);
			qpfree(rp);
		}

		if (nfd < 0) {
			if (errno == EINTR) {
				/* signals handled above here */
				continue;
			} else {
				syslog(LOG_ERR, "select errno %d, %s\n", 
				       errno, strerror(errno));
			}
		} else if (nfd > 0) {
			qp = recv_packet(infd);
			if (qp == NULL) {
				syslog(LOG_INFO, "That's enough\n");
				break;
			}
			switch (qp->qp_type) {
			case QP_KILL:
				if (child_pid) {

					syslog(LOG_INFO, "Sending child %d signal %d", 
						child_pid, qp->qp_kill.qp_sig);
					kill(child_pid * -1, qp->qp_kill.qp_sig);
				}
				break;
			case QP_SETUSER:
				if (setup_user(qp->qp_setuser.qp_user,
						qp->qp_setuser.qp_group) == 0) {
					rp = make_qp_returncode(-1, 0, "User not found");
				} else {
					rp = make_qp_ack(QP_SETUSER, 1);
				}
				send_packet(fileno(stdout), rp);
				qpfree(rp);
				break;
			case QP_RUNCMD:
				child_pid = run_cmd(qp->qp_runcmd.qp_cmdline,
						qp->qp_runcmd.qp_stdin_port,
						qp->qp_runcmd.qp_stdout_port,
						qp->qp_runcmd.qp_stderr_port);
				break;
			case QP_RECVFILE: 
				syslog(LOG_INFO, "Got a QP_RECVFILE with path = %s, "
				       "ifd = %d, count = %d, mode = %o\n",
				        qp->qp_recvfile.qp_path,
				        qp->qp_recvfile.qp_if_port,
				        qp->qp_recvfile.qp_count,
				        qp->qp_recvfile.qp_mode);
				nbytes = recvfile(qp->qp_recvfile.qp_path, 
				                  qp->qp_recvfile.qp_if_port, 
				                  qp->qp_recvfile.qp_count, 
 				                  qp->qp_recvfile.qp_mode);
				if (nbytes < 0) {
					rp = make_qp_returncode(-1, errno, strerror(errno));
				} else if (nbytes < qp->qp_recvfile.qp_count) {
					char tmpstr[512];
					sprintf(tmpstr, "Excpected %d, wrote %d\n", 
					        qp->qp_recvfile.qp_count, nbytes);
					rp = make_qp_returncode(-1, 0, tmpstr);
				} else {
					rp = make_qp_returncode(0, 0, "Transfer Complete");
				}
				send_packet(fileno(stdout), rp);
				qpfree(rp);
				break;
			case QP_SENDFILE:
				syslog(LOG_INFO, "Got a QP_SENDFILE with path = %s, "
				       "ofd = %d, count = %d\n",
				        qp->qp_sendfile.qp_path,
				        qp->qp_sendfile.qp_of_port,
				        qp->qp_sendfile.qp_count);
				break;
			case QP_RSTAT:
				syslog(LOG_INFO, "Got a QP_RSTAT with path = %s\n",
				       qp->qp_rstat.qp_path);
				rp = rstat(qp->qp_rstat.qp_path);
				send_packet(fileno(stdout), rp);
				qpfree(rp);
				break;
			default:
				syslog(LOG_WARNING, 
					"Packet type %s unimplemented",
					qp_packet_type(qp->qp_type));
			}
		} else {
			syslog(LOG_DEBUG, "Nothing to do\n");
		}
		
	}
}

int
main(int argc, char *argv[])
{
	int ch;
	socklen_t peernamelen;

	openlog("qarshd", LOG_PID, LOG_DAEMON);

	while ((ch = getopt(argc, argv, "")) != -1) {
		switch (ch) {
			case '?':
			default:
				printf("unknown option '%c'\n", optopt);
				exit(1);
		}
	}

	/* daemon initialization */
	peernamelen = sizeof peername;
	getpeername(0, (struct sockaddr *)&peername, &peernamelen);
	syslog(LOG_INFO, "Talking to peer %s:%d", 
		inet_ntoa(peername.sin_addr), ntohs(peername.sin_port));
	/* Start reading packets from stdin */
	handle_packets(0);

	return 0;
}