aboutsummaryrefslogtreecommitdiff
path: root/anoncvssh.c
blob: ba4ffd1c06c4b21c407dda0be88af32b9ee6b5c8 (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
/*
 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
 * Copyright (c) 1997 Bob Beck <beck@obtuse.com>
 * Copyright (c) 1996 Thorsten Lockert <tholo@sigmasoft.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
#include <paths.h>
#endif
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>

#ifndef __P
#if defined(__STDC__) || defined(__cplusplus)
#define	__P(protos)	protos		/* full-blown ANSI C */
#else
#define	__P(protos)	()		/* traditional C preprocessor */
#endif
#endif

/*
 * You may need to change this path to ensure that RCS, CVS and diff
 * can be found
 */
#ifndef _PATH_DEFPATH
#define	_PATH_DEFPATH	"/bin:/usr/bin"
#endif

/*
 * This should not normally have to be changed
 */
#ifndef _PATH_BSHELL
#define _PATH_BSHELL	"/bin/sh"
#endif

/*
 * Location of CVS tree, relative to the anonymous CVS user's
 * home directory
 */
#ifndef LOCALROOT
#define	LOCALROOT	"/cvs"
#endif

/*
 * Hostname to be used when accessing the remote repository.
 */
#ifndef HOSTNAME
#define	HOSTNAME	"dim13.org"
#endif

/*
 * Username to be used when accessing the remote repository.
 */
#ifndef USERNAME
#define USERNAME	"anoncvs"
#endif

/*
 * $CVSROOT is created based on USERNAME HOSTNAME and LOCALROOT above
 */
#ifndef CVSROOT
#define	CVSROOT		USERNAME "@" HOSTNAME ":"LOCALROOT
#endif

/*
 * We define PSERVER_SUPPORT to allow anoncvssh to spawn a "cvs pserver".
 * You may undefine this if you aren't going to be running pserver.
 */
#ifndef PSERVER_SUPPORT
#define PSERVER_SUPPORT
#endif

/*
 * Define USE_SYSLOG if you want anoncvssh to log pserver connections 
 * using syslog()
 */
#define USE_SYSLOG

#ifdef USE_SYSLOG
#include <string.h>
#include <syslog.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define LOG_FACILITY LOG_DAEMON
#define LOG_PRIO LOG_INFO
#endif

/* Define ANONCVS_USER if you want anoncvssh to complain if invoked by
 * anyone other than root or ANONCVS_USER.
 */
/* #define ANONCVS_USER USERNAME */

/*
 * If you want to be able to run an alternate OpenCVS binary on your
 * anoncvs server, define OPENCVS_USER as the user who will invoke it.
 */
/* #define OPENCVS_USER "opencvs" */

int main __P((int, char *[]));

char * const env[] = {
	"PATH="_PATH_DEFPATH,
	"SHELL="_PATH_BSHELL,
	"CVSROOT="LOCALROOT,
	"HOME=/",
	"CVSREADONLYFS=1",
	NULL
};

int
main(argc, argv)
int argc;
char *argv[];
{
	struct passwd *pw;
#ifdef DEBUG
	int i;
#endif /* DEBUG */
#if defined(OPENCVS_USER)
	int opencvs;
#endif

	pw = getpwuid(getuid());
	if (pw == NULL) {
		fprintf(stderr, "no user for uid %d\n", getuid());
		exit(1);
	}
	if (pw->pw_dir == NULL) {
		fprintf(stderr, "no directory\n");
		exit(1);
	}

#ifdef USE_SYSLOG
	openlog("anoncvssh", LOG_PID | LOG_NDELAY, LOG_FACILITY);
#endif /* USE_SYSLOG */
	
#ifdef ANONCVS_USER
	/* 
	 * I love lusers who have to test every setuid binary on my machine.
	 */
	if (getuid() != 0 && (strcmp (pw->pw_name, ANONCVS_USER) != 0)) {
		fprintf(stderr, "You're not supposed to be running me!\n"); 
#ifdef USE_SYSLOG
		syslog(LOG_NOTICE,
		       "User %s(%d) invoked anoncvssh - Possible twink?",
		       pw->pw_name, pw->pw_uid); 
#endif /* USE_SYSLOG */
		exit(1);
	}
#endif /* ANONCVS_USER */


	setuid(0);
	if (chroot(pw->pw_dir) == -1) {
		perror("chroot");
		exit (1);
	}
	chdir("/");
	setuid(pw->pw_uid);

#if defined(OPENCVS_USER)
	if (!strcmp(pw->pw_name, OPENCVS_USER))
		opencvs = 1;
	else
		opencvs = 0;
#endif

	/*
	 * program now "safe"
	 */

#ifdef PSERVER_SUPPORT
	/* If we want pserver functionality */
	if ((argc == 2) && (strcmp("pserver", argv[1]) == 0)) {
#ifdef USE_SYSLOG
 	        int slen;
		struct sockaddr_in my_sa, peer_sa;
		char *us, *them;

#if defined(OPENCVS_USER)
		if (opencvs == 1) {
			fprintf(stderr, "OpenCVS does not support pserver\n");
			sleep(10);
			exit(1);
		}
#endif

		slen = sizeof(my_sa);
		if (getsockname(0, (struct sockaddr *) &my_sa, &slen)
		    != 0) {
		  perror("getsockname");
		  exit(1);
		}
		us = strdup(inet_ntoa(my_sa.sin_addr));
		if (us == NULL) {
		  fprintf(stderr, "malloc failed\n");
		  exit(1);
		}
		slen = sizeof(peer_sa);
		if (getpeername(0, (struct sockaddr *) &peer_sa, &slen)
		    != 0) {
		  perror("getpeername");
		  exit(1);
		}
		them=strdup(inet_ntoa(peer_sa.sin_addr));
		if (them == NULL) {
		  fprintf(stderr, "malloc failed\n");
		  exit(1);
		}
	        syslog(LOG_PRIO, 
		       "pserver connection from %s:%d to %s:%d\n",
		       them, ntohs(peer_sa.sin_port),
		       us, ntohs(my_sa.sin_port));
#endif /* USE_SYSLOG */
		execle("/usr/bin/cvs", "cvs",
		    "--allow-root="LOCALROOT, "pserver", (char *)NULL, env);
		perror("execle: cvs");
		fprintf(stderr, "unable to exec CVS pserver!\n");
		exit(1);
		/* NOTREACHED */
	}
#endif

	if (argc != 3 || 
		strcmp("anoncvssh",  argv[0]) != 0 ||
		strcmp("-c",         argv[1]) != 0 ||
		(strcmp("cvs server", argv[2]) != 0 &&
		 strcmp("cvs -d "LOCALROOT" server", argv[2]) != 0)) {
		fprintf(stderr, "\nTo use anonymous CVS install the latest ");
		fprintf(stderr,"version of CVS on your local machine.\n");
		fprintf(stderr,"Then set your CVSROOT environment variable ");
		fprintf(stderr,"to the following value:\n");
#if defined(OPENCVS_USER)
		fprintf(stderr, "\t%s@%s:%s for OpenCVS\n", OPENCVS_USER,
		    HOSTNAME, LOCALROOT);
#endif
		fprintf(stderr,"\t%s\n\n", CVSROOT);
#ifdef DEBUG
		fprintf(stderr, "argc = %d\n", argc);
		for (i = 0 ; i < argc ; i++)
			fprintf(stderr, "argv[%d] = \"%s\"\n", i, argv[i]);
#endif /* DEBUG */
		sleep(10);
		exit(0);
	}

#if defined(OPENCVS_USER)
	if (opencvs == 1) {
		execle("/usr/bin/opencvs", "opencvs",
		    "server", (char *)NULL, env);
	} else {
#endif
		execle("/usr/bin/cvs", "cvs", "server", (char *)NULL, env);
#if defined(OPENCVS_USER)
	}
#endif

	perror("execle: cvs");
	fprintf(stderr, "unable to exec CVS server!\n");
	exit(1);
	/* NOTREACHED */
}