summaryrefslogtreecommitdiff
path: root/rsh.c
blob: 1c636861ddec037854240d6d3f5fa517d605bc2f (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
#include "rsh.h"

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "cache.h"

#define COMMAND_SIZE 4096

/*
 * Write a shell-quoted version of a string into a buffer, and
 * return bytes that ought to be output excluding final null.
 */
static int shell_quote(char *buf, int nmax, const char *str)
{
	char ch;
	int nq;
	int oc = 0;

	while ( (ch = *str++) ) {
		nq = 0;
		if ( strchr(" !\"#$%&\'()*;<=>?[\\]^`{|}", ch) )
			nq = 1;

		if ( nq ) {
			if ( nmax > 1 ) {
				*buf++ = '\\';
				nmax--;
			}
			oc++;
		}

		if ( nmax > 1 ) {
			*buf++ = ch;
			nmax--;
		}
		oc++;
	}

	if ( nmax )
		*buf = '\0';

	return oc;
}
			
/*
 * Append a string to a string buffer, with or without quoting.  Return true
 * if the buffer overflowed.
 */
static int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
{
	char *p = *ptrp;
	int size = *sizep;
	int oc;

	if ( quote ) {
		oc = shell_quote(p, size, str);
	} else {
		oc = strlen(str);
		memcpy(p, str, (oc >= size) ? size-1 : oc);
	}

	if ( oc >= size ) {
		p[size-1] = '\0';
		*ptrp += size-1;
		*sizep = 1;
		return 1;	/* Overflow, string unusable */
	}

	*ptrp  += oc;
	*sizep -= oc;
	return 0;
}

int setup_connection(int *fd_in, int *fd_out, const char *remote_prog, 
		     char *url, int rmt_argc, char **rmt_argv)
{
	char *host;
	char *path;
	int sv[2];
	char command[COMMAND_SIZE];
	char *posn;
	int sizen;
	int of;
	int i;

	if (!strcmp(url, "-")) {
		*fd_in = 0;
		*fd_out = 1;
		return 0;
	}

	host = strstr(url, "//");
	if (host) {
		host += 2;
		path = strchr(host, '/');
	} else {
		host = url;
		path = strchr(host, ':');
		if (path)
			*(path++) = '\0';
	}
	if (!path) {
		return error("Bad URL: %s", url);
	}
	/* $GIT_RSH <host> "env GIR_DIR=<path> <remote_prog> <args...>" */
	sizen = COMMAND_SIZE;
	posn = command;
	of = 0;
	of |= add_to_string(&posn, &sizen, "env ", 0);
	of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT, 0);
	of |= add_to_string(&posn, &sizen, "=", 0);
	of |= add_to_string(&posn, &sizen, path, 1);
	of |= add_to_string(&posn, &sizen, " ", 0);
	of |= add_to_string(&posn, &sizen, remote_prog, 1);

	for ( i = 0 ; i < rmt_argc ; i++ ) {
		of |= add_to_string(&posn, &sizen, " ", 0);
		of |= add_to_string(&posn, &sizen, rmt_argv[i], 1);
	}

	of |= add_to_string(&posn, &sizen, " -", 0);

	if ( of )
		return error("Command line too long");

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
		return error("Couldn't create socket");

	if (!fork()) {
		const char *ssh, *ssh_basename;
		ssh = getenv("GIT_SSH");
		if (!ssh) ssh = "ssh";
		ssh_basename = strrchr(ssh, '/');
		if (!ssh_basename)
			ssh_basename = ssh;
		else
			ssh_basename++;
		close(sv[1]);
		dup2(sv[0], 0);
		dup2(sv[0], 1);
		execlp(ssh, ssh_basename, host, command, NULL);
	}
	close(sv[0]);
	*fd_in = sv[1];
	*fd_out = sv[1];
	return 0;
}