summaryrefslogtreecommitdiff
path: root/ssh-bleach.c
blob: 79273faaf8d51d73887e9da0a0cdd053f5e7a9dc (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
/*
    ssh-bleach - sanitize an ssh command line

    Copyright © 2006  Keith Packard and Carl Worth

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; using version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int verbose = 0;

int cvs_server (char *line) {
    if (!strcmp (line, "cvs server")) {
	if (verbose)
	    printf ("cvs server\n");
	return execl ("/usr/bin/cvs", "cvs", "server", NULL);
    }
    return 0;
}

char *get_quoted_arg (char *line)
{
    char    *first = strchr (line, '\'');
    char    *old;
    char    *last;
    char    *new, *n;
    
    if (!first)
	return NULL;
    last = strrchr (line, '\'');
    if (!last || last == first)
	return NULL;
    first++;
    new = malloc (last - first + 1);
    if (!new)
	return NULL;
    n = new;
    for (old = first; old < last; old++) {
	if (*old == '\\')
	    continue;
	*n++ = *old;
    }
    *n++ = '\0';
    if (verbose)
	printf ("First argument is \"%s\"\n", new);
    return new;
}

int git_receive_pack (char *line) {
    char *arg;
    
    if (!strncmp (line, "git-receive-pack '", 18) && 
	(arg = get_quoted_arg (line)))
    {
	if (verbose)
	    printf ("git-receive-pack '%s'\n", arg);
	return execl ("/usr/local/bin/git-receive-pack", 
		      "git-receive-pack",
		      arg,
		      NULL);
    }
    return 0;
}

int git_upload_pack (char *line) {
    char *arg;
    
    if (!strncmp (line, "git-upload-pack '", 17) && 
	(arg = get_quoted_arg (line)))
    {
	if (verbose)
	    printf ("git-upload-pack '%s'\n", arg);
	return execl ("/usr/local/bin/git-upload-pack", 
		      "git-upload-pack",
		      arg,
		      NULL);
    }
    return 0;
}

int (*commands[]) (char *line) = {
    cvs_server,
    git_receive_pack,
    git_upload_pack,
    NULL
};

int main (int argc, char **argv)
{
    char    *line = getenv ("SSH_ORIGINAL_COMMAND");
    int	    i;
    
    if (argv[1] && !strcmp (argv[1], "-v"))
	verbose = 1;
    if (!line)
	return 1;
    for (i = 0; commands[i]; i++)
	if ((*commands[i]) (line) != 0)
	    break;
    return 1;
}