Debian Codegraph @

contrib/hgsh/hgsh.c

raw
1 /*
2 * hgsh.c - restricted login shell for mercurial
3 *
4 * Copyright 2006 Vadim Gelfer <[email protected]>
5 *
6 * This software may be used and distributed according to the terms of the
7 * GNU General Public License, incorporated herein by reference.
8 *
9 * this program is login shell for dedicated mercurial user account. it
10 * only allows few actions:
11 *
12 * 1. run hg in server mode on specific repository. no other hg commands
13 * are allowed. we try to verify that repo to be accessed exists under
14 * given top-level directory.
15 *
16 * 2. (optional) forward ssh connection from firewall/gateway machine to
17 * "real" mercurial host, to let users outside intranet pull and push
18 * changes through firewall.
19 *
20 * 3. (optional) run normal shell, to allow to "su" to mercurial user, use
21 * "sudo" to run programs as that user, or run cron jobs as that user.
22 *
23 * only tested on linux yet. patches for non-linux systems welcome.
24 */
25
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE /* for asprintf */
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sysexits.h>
36 #include <unistd.h>
37
38 /*
39 * user config.
40 *
41 * if you see a hostname below, just use first part of hostname. example,
42 * if you have host named foo.bar.com, use "foo".
43 */
44
45 /*
46 * HG_GATEWAY: hostname of gateway/firewall machine that people outside your
47 * intranet ssh into if they need to ssh to other machines. if you do not
48 * have such machine, set to NULL.
49 */
50 #ifndef HG_GATEWAY
51 #define HG_GATEWAY "gateway"
52 #endif
53
54 /*
55 * HG_HOST: hostname of mercurial server. if any machine is allowed, set to
56 * NULL.
57 */
58 #ifndef HG_HOST
59 #define HG_HOST "mercurial"
60 #endif
61
62 /*
63 * HG_USER: username to log in from HG_GATEWAY to HG_HOST. if gateway and
64 * host username are same, set to NULL.
65 */
66 #ifndef HG_USER
67 #define HG_USER "hg"
68 #endif
69
70 /*
71 * HG_ROOT: root of tree full of mercurial repos. if you do not want to
72 * validate location of repo when someone is try to access, set to NULL.
73 */
74 #ifndef HG_ROOT
75 #define HG_ROOT "/home/hg/repos"
76 #endif
77
78 /*
79 * HG: path to the mercurial executable to run.
80 */
81 #ifndef HG
82 #define HG "/home/hg/bin/hg"
83 #endif
84
85 /*
86 * HG_SHELL: shell to use for actions like "sudo" and "su" access to
87 * mercurial user, and cron jobs. if you want to make these things
88 * impossible, set to NULL.
89 */
90 #ifndef HG_SHELL
91 #define HG_SHELL NULL
92 /* #define HG_SHELL "/bin/bash" */
93 #endif
94
95 /*
96 * HG_HELP: some way for users to get support if they have problem. if they
97 * should not get helpful message, set to NULL.
98 */
99 #ifndef HG_HELP
100 #define HG_HELP "please contact [email protected] for help."
101 #endif
102
103 /*
104 * SSH: path to ssh executable to run, if forwarding from HG_GATEWAY to
105 * HG_HOST. if you want to use rsh instead (why?), you need to modify
106 * arguments it is called with. see forward_through_gateway.
107 */
108 #ifndef SSH
109 #define SSH "/usr/bin/ssh"
110 #endif
111
112 /*
113 * tell whether to print command that is to be executed. useful for
114 * debugging. should not interfere with mercurial operation, since
115 * mercurial only cares about stdin and stdout, and this prints to stderr.
116 */
117 static const int debug = 0;
118
119 static void print_cmdline(int argc, char **argv)
120 {
121 FILE *fp = stderr;
122 int i;
123
124 fputs("command: ", fp);
125
126 for (i = 0; i < argc; i++) {
127 char *spc = strpbrk(argv[i], " \t\r\n");
128 if (spc) {
129 fputc('\'', fp);
130 }
131 fputs(argv[i], fp);
132 if (spc) {
133 fputc('\'', fp);
134 }
135 if (i < argc - 1) {
136 fputc(' ', fp);
137 }
138 }
139 fputc('\n', fp);
140 fflush(fp);
141 }
142
143 static void usage(const char *reason, int exitcode)
144 {
145 char *hg_help = HG_HELP;
146
147 if (reason) {
148 fprintf(stderr, "*** Error: %s.\n", reason);
149 }
150 fprintf(stderr, "*** This program has been invoked incorrectly.\n");
151 if (hg_help) {
152 fprintf(stderr, "*** %s\n", hg_help);
153 }
154 exit(exitcode ? exitcode : EX_USAGE);
155 }
156
157 /*
158 * run on gateway host to make another ssh connection, to "real" mercurial
159 * server. it sends its command line unmodified to far end.
160 *
161 * never called if HG_GATEWAY is NULL.
162 */
163 static void forward_through_gateway(int argc, char **argv)
164 {
165 char *ssh = SSH;
166 char *hg_host = HG_HOST;
167 char *hg_user = HG_USER;
168 char **nargv = alloca((10 + argc) * sizeof(char *));
169 int i = 0, j;
170
171 nargv[i++] = ssh;
172 nargv[i++] = "-q";
173 nargv[i++] = "-T";
174 nargv[i++] = "-x";
175 if (hg_user) {
176 nargv[i++] = "-l";
177 nargv[i++] = hg_user;
178 }
179 nargv[i++] = hg_host;
180
181 /*
182 * sshd called us with added "-c", because it thinks we are a shell.
183 * drop it if we find it.
184 */
185 j = 1;
186 if (j < argc && strcmp(argv[j], "-c") == 0) {
187 j++;
188 }
189
190 for (; j < argc; i++, j++) {
191 nargv[i] = argv[j];
192 }
193 nargv[i] = NULL;
194
195 if (debug) {
196 print_cmdline(i, nargv);
197 }
198
199 execv(ssh, nargv);
200 perror(ssh);
201 exit(EX_UNAVAILABLE);
202 }
203
204 /*
205 * run shell. let administrator "su" to mercurial user's account to do
206 * administrative works.
207 *
208 * never called if HG_SHELL is NULL.
209 */
210 static void run_shell(int argc, char **argv)
211 {
212 char *hg_shell = HG_SHELL;
213 char **nargv;
214 char *c;
215 int i;
216
217 nargv = alloca((argc + 3) * sizeof(char *));
218 c = strrchr(hg_shell, '/');
219
220 /* tell "real" shell it is login shell, if needed. */
221
222 if (argv[0][0] == '-' && c) {
223 nargv[0] = strdup(c);
224 if (nargv[0] == NULL) {
225 perror("malloc");
226 exit(EX_OSERR);
227 }
228 nargv[0][0] = '-';
229 } else {
230 nargv[0] = hg_shell;
231 }
232
233 for (i = 1; i < argc; i++) {
234 nargv[i] = argv[i];
235 }
236 nargv[i] = NULL;
237
238 if (debug) {
239 print_cmdline(i, nargv);
240 }
241
242 execv(hg_shell, nargv);
243 perror(hg_shell);
244 exit(EX_OSFILE);
245 }
246
247 enum cmdline {
248 hg_init,
249 hg_serve,
250 };
251
252 /*
253 * attempt to verify that a directory is really a hg repo, by testing
254 * for the existence of a subdirectory.
255 */
256 static int validate_repo(const char *repo_root, const char *subdir)
257 {
258 char *abs_path;
259 struct stat st;
260 int ret;
261
262 if (asprintf(&abs_path, "%s.hg/%s", repo_root, subdir) == -1) {
263 ret = -1;
264 goto bail;
265 }
266
267 /* verify that we really are looking at valid repo. */
268
269 if (stat(abs_path, &st) == -1) {
270 ret = 0;
271 } else {
272 ret = 1;
273 }
274
275 bail:
276 return ret;
277 }
278
279 /*
280 * paranoid wrapper, runs hg executable in server mode.
281 */
282 static void serve_data(int argc, char **argv)
283 {
284 char *hg_root = HG_ROOT;
285 char *repo, *repo_root;
286 enum cmdline cmd;
287 char *nargv[6];
288 size_t repolen;
289 int i;
290
291 /*
292 * check argv for looking okay. we should be invoked with argv
293 * resembling like this:
294 *
295 * hgsh
296 * -c
297 * hg -R some/path serve --stdio
298 *
299 * the "-c" is added by sshd, because it thinks we are login shell.
300 */
301
302 if (argc != 3) {
303 goto badargs;
304 }
305
306 if (strcmp(argv[1], "-c") != 0) {
307 goto badargs;
308 }
309
310 if (sscanf(argv[2], "hg init %as", &repo) == 1) {
311 cmd = hg_init;
312 } else if (sscanf(argv[2], "hg -R %as serve --stdio", &repo) == 1) {
313 cmd = hg_serve;
314 } else {
315 goto badargs;
316 }
317
318 repolen = repo ? strlen(repo) : 0;
319
320 if (repolen == 0) {
321 goto badargs;
322 }
323
324 if (hg_root) {
325 if (asprintf(&repo_root, "%s/%s/", hg_root, repo) == -1) {
326 goto badargs;
327 }
328
329 /*
330 * attempt to stop break out from inside the
331 * repository tree. could do something more clever
332 * here, because e.g. we could traverse a symlink that
333 * looks safe, but really breaks us out of tree.
334 */
335
336 if (strstr(repo_root, "/../") != NULL) {
337 goto badargs;
338 }
339
340 /* only hg init expects no repo. */
341
342 if (cmd != hg_init) {
343 int valid;
344
345 valid = validate_repo(repo_root, "data");
346
347 if (valid == -1) {
348 goto badargs;
349 }
350
351 if (valid == 0) {
352 valid = validate_repo(repo_root, "store");
353
354 if (valid == -1) {
355 goto badargs;
356 }
357 }
358
359 if (valid == 0) {
360 perror(repo);
361 exit(EX_DATAERR);
362 }
363 }
364
365 if (chdir(hg_root) == -1) {
366 perror(hg_root);
367 exit(EX_SOFTWARE);
368 }
369 }
370
371 i = 0;
372
373 switch (cmd) {
374 case hg_serve:
375 nargv[i++] = HG;
376 nargv[i++] = "-R";
377 nargv[i++] = repo;
378 nargv[i++] = "serve";
379 nargv[i++] = "--stdio";
380 break;
381 case hg_init:
382 nargv[i++] = HG;
383 nargv[i++] = "init";
384 nargv[i++] = repo;
385 break;
386 }
387
388 nargv[i] = NULL;
389
390 if (debug) {
391 print_cmdline(i, nargv);
392 }
393
394 execv(HG, nargv);
395 perror(HG);
396 exit(EX_UNAVAILABLE);
397
398 badargs:
399 /* print useless error message. */
400
401 usage("invalid arguments", EX_DATAERR);
402 }
403
404 int main(int argc, char **argv)
405 {
406 char host[1024];
407 char *c;
408
409 if (gethostname(host, sizeof(host)) == -1) {
410 perror("gethostname");
411 exit(EX_OSERR);
412 }
413
414 if ((c = strchr(host, '.')) != NULL) {
415 *c = '\0';
416 }
417
418 if (getenv("SSH_CLIENT")) {
419 char *hg_gateway = HG_GATEWAY;
420 char *hg_host = HG_HOST;
421
422 if (hg_gateway && strcmp(host, hg_gateway) == 0) {
423 forward_through_gateway(argc, argv);
424 }
425
426 if (hg_host && strcmp(host, hg_host) != 0) {
427 usage("invoked on unexpected host", EX_USAGE);
428 }
429
430 serve_data(argc, argv);
431 } else if (HG_SHELL) {
432 run_shell(argc, argv);
433 } else {
434 usage("invalid arguments", EX_DATAERR);
435 }
436
437 return 0;
438 }