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