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