File: main.c.in

package info (click to toggle)
rssh 2.3.4-5+deb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,036 kB
  • sloc: ansic: 4,437; sh: 1,069; makefile: 62; awk: 19
file content (276 lines) | stat: -rw-r--r-- 8,072 bytes parent folder | download | duplicates (3)
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
/*
 * rssh.c - restricted shell for ssh to allow scp or sftp only
 * 
 * Copyright 2003-2006 Derek D. Martin ( code at pizzashack dot org ).
 *
 * This program is licensed under a BSD-style license, as follows: 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	notice, this list of conditions and the following disclaimer in the
 *	documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/* SYSTEM INCLUDES */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif /* HAVE_ERRNO_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_LIBGEN_H
#include <libgen.h>
#endif /* HAVE_LIBGEN_H */
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif /* HAVE_SYSLOG_H */
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif /* HAVE_PWD_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */ 
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */


/* LOCAL INCLUDES */
#include "rssh.h"
#include "rsshconf.h"
#include "pathnames.h"
#include "log.h"
#include "util.h"
#include "argvec.h"


/* FILE SCOPE FUNCTION DECLARATIONS */

char **build_shell_args( struct passwd uinfo, ShellOptions_t *opts, 
		         char *cmdline, char **cmd );
void vers_info( void );

/* GLOBAL VARIABLES */
extern int errno;

char *progname;
char *username;
char *version = "@PACKAGE_STRING@";
char *copyr = "Copyright 2002-2010 Derek D. Martin <@PACKAGE_BUGREPORT@>";

/* MAIN PROGRAM */
int main( int argc, char **argv )
{
	char		**argvec = NULL;	/* vector for execv() */
	char		*cmd;	/* name of the command to execv() */
	ShellOptions_t	opts;	/* options configured by config file */
	struct passwd	uinfo;	/* info about the user running rssh */
	struct passwd	*temp;	/* copy temp into uinfo */ 

	/* if we just want the version, give it an exit */
	if ( argc == 2 && !( strcmp(argv[1], "-v")) ){
		vers_info();
		exit(0);
	}

	/* initialize variables to defaults */
	opts.rssh_umask = 022;
	opts.shell_flags = 0;
	opts.chroot_path = NULL;
	memset(&uinfo, 0, sizeof uinfo);
	if ( (putenv("PATH=/bin:/usr/bin")) ){
		log_msg("fatal error: could not set PATH environment var");
		exit(1);
	}

	/* see who we are and set up logging */
	if ( (temp = getpwuid(getuid())) ){
		uinfo = *temp;
		username = uinfo.pw_name;
	}
	else
		/* this probably should never happen */
		username = "unknown user!";
	progname = strdup(log_make_ident(basename(argv[0])));
	log_set_facility(LOG_DAEMON);
	log_open();

	/* process the config file */
	if ( !(read_shell_config(&opts, PATH_RSSH_CONFIG, 1)) ){
		log_set_priority(LOG_ERR);
		log_msg("there were errors processing configuration file!");
		fail(0, argc, argv);
	}

	/* set the umask */
	umask(opts.rssh_umask);

	/* arg count check */
	if ( argc < 3 ) fail(opts.shell_flags, argc, argv);

	/* if first arg is anything but -c, it's no good */
	if ( strcmp("-c", argv[1]) ) fail(opts.shell_flags, argc, argv);

	/* get the arguments for execv() */
	if ( !(argvec = build_shell_args(uinfo, &opts, argv[2], &cmd)) )
		fail(opts.shell_flags, argc, argv);

	/* if all that passed, exec the relevant command */
	execv(cmd, argvec);

	/* we only get here if the exec fails */
	fprintf(stderr, "%s: execv() failed.  ", cmd);
	log_set_priority(LOG_ERR);
	log_msg("execv failed: cmd line %s", argv[2]);

	switch (errno){
	case EACCES:
	case ENOTDIR:
	case ENOENT:
		fprintf(stderr, "%s: %s is not an executable file, or "
			"permission denied.\n\n", basename(argv[0]), 
			argvec[0]);
		break;
	case EPERM:
		/* this shouldn't happen, as we don't run SUID */
		fprintf(stderr, "%s: FS mounted nosuid or process is being "
			"traced\n (and you are not root)\n\n", 
			basename(argv[0]));
		break;
	default:
			fprintf(stderr, "an unknown error occurred.\n\n");
	}
	
	exit(1);
}


char **build_shell_args( struct passwd uinfo, 
			 ShellOptions_t *opts,
			 char *cmdline,
		         char **cmd )
{
	char    **argvec;       /* argument vector for new cmd line */
	char	*temp;		/* to build chroot helper cmd line */
	int	len;

	/*
	 * determine if the command in cmdline is acceptable to run, and store
	 * name of program to exec in cmd
	 */
	if ( !(*cmd = get_command(cmdline, opts)) ) return NULL;

	/* if we need to do chroot processing, do it */
	if ( opts->shell_flags & RSSH_USE_CHROOT ){
		/* create vector of pointers to command line arguments */

		/* 
		 * we don't call build_arg_vector() here, because expanding
		 * the shell arguments before we chroot() results in a
		 * directory transversal attack possibility, i.e. the user can
		 * see some of the files outside the chroot jail.  We'll call
		 * build_arg_vector() in the chroot helper instead...
                 */

		if ( !(argvec = (char **)malloc(6 * sizeof (char *))) ){
			log_set_priority(LOG_ERR);
			log_msg("OOM error in build_shell_args() (fatal)");
			exit(1);
		}

		argvec[0] = PATH_CHROOT_HELPER;

		/* which one is it? */
		if ( !(strcmp(*cmd, PATH_SCP)) )
			argvec[1] = "1";
		else if ( !(strcmp(*cmd, PATH_SFTP_SERVER)) )
			argvec[1] = "2";
		else if ( !(strcmp(*cmd, PATH_CVS)) )
			argvec[1] = "3";
		else if ( !(strcmp(*cmd, PATH_RDIST)) )
			argvec[1] = "4";
		else if ( !(strcmp(*cmd, PATH_RSYNC)) )
			argvec[1] = "5";
		else if ( !(strcmp(*cmd, PATH_SVNSERVE)) )
			argvec[1] = "6";
		else {
			log_set_priority(LOG_ERR);
			log_msg("fatal error identifying the correct command "
				"(this should never happen)");
			exit(1);
		}
		argvec[2] = cmdline;
		argvec[3] = NULL;

		/* change the command to run to the chroot helper */
		*cmd = PATH_CHROOT_HELPER;

		/* set up buffer to log command line ('"' + ' ' + '\0' = 9) */
		len = strlen(cmdline) +  strlen(argvec[2]) + 
			strlen(PATH_CHROOT_HELPER) + 9;
		if ( !(temp = (char *)malloc(len)) ){
			log_set_priority(LOG_ERR);
			log_msg("OOM error in build_shell_args() (fatal)");
			exit(1);
		}

		/* stuff the args into the buffer */
		snprintf(temp, len, "%s %s \"%s\"",
			 PATH_CHROOT_HELPER,
			 argvec[1],
			 cmdline);

		/* now log 'em */
		log_set_priority(LOG_INFO);
		log_msg("chroot cmd line: %s", temp);

		return argvec;
	}

	/* return vector of pointers to command line arguments */
	argvec = build_arg_vector(cmdline, 0);
	if (check_command_line(argvec, opts)) return argvec;
	else return NULL;
}

void vers_info( void )
{
	printf("\n%s\n", version);
	printf("%s\n\n", copyr);
	printf("%20s = %s\n", "rssh config file", PATH_RSSH_CONFIG);
	printf("%20s = %s\n", "chroot helper path", PATH_CHROOT_HELPER);
	printf("%20s = %s\n", "scp binary path", PATH_SCP);
	printf("%20s = %s\n", "sftp server binary", PATH_SFTP_SERVER);
	printf("%20s = %s\n", "cvs binary path", PATH_CVS);
	printf("%20s = %s\n", "rdist binary path", PATH_RDIST);
	printf("%20s = %s\n", "rsync binary path", PATH_RSYNC);
	printf("%20s = %s\n\n", "svnserve binary path", PATH_SVNSERVE);
}