]> git.ipfire.org Git - thirdparty/git.git/blame - rsh.c
Merge branch 'fix'
[thirdparty/git.git] / rsh.c
CommitLineData
6eb7ed54 1#include <string.h>
ec8f8116 2#include <sys/types.h>
6eb7ed54
DB
3#include <sys/socket.h>
4
f336af17
PA
5#include "rsh.h"
6#include "quote.h"
6eb7ed54
DB
7#include "cache.h"
8
9#define COMMAND_SIZE 4096
10
0de68d28 11/*
f336af17
PA
12 * Append a string to a string buffer, with or without shell quoting.
13 * Return true if the buffer overflowed.
0de68d28
PA
14 */
15static int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
16{
17 char *p = *ptrp;
18 int size = *sizep;
19 int oc;
e433b071 20 int err = 0;
0de68d28
PA
21
22 if ( quote ) {
f336af17 23 oc = sq_quote_buf(p, size, str);
0de68d28
PA
24 } else {
25 oc = strlen(str);
26 memcpy(p, str, (oc >= size) ? size-1 : oc);
27 }
28
29 if ( oc >= size ) {
e433b071
PA
30 err = 1;
31 oc = size-1;
0de68d28
PA
32 }
33
34 *ptrp += oc;
e433b071 35 **ptrp = '\0';
0de68d28 36 *sizep -= oc;
e433b071 37 return err;
0de68d28
PA
38}
39
001d4a27 40int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
6eb7ed54
DB
41 char *url, int rmt_argc, char **rmt_argv)
42{
43 char *host;
44 char *path;
45 int sv[2];
46 char command[COMMAND_SIZE];
47 char *posn;
0de68d28
PA
48 int sizen;
49 int of;
6eb7ed54
DB
50 int i;
51
52 if (!strcmp(url, "-")) {
53 *fd_in = 0;
54 *fd_out = 1;
55 return 0;
56 }
57
58 host = strstr(url, "//");
001d4a27
LT
59 if (host) {
60 host += 2;
61 path = strchr(host, '/');
62 } else {
63 host = url;
64 path = strchr(host, ':');
479346ad
SV
65 if (path)
66 *(path++) = '\0';
6eb7ed54 67 }
6eb7ed54
DB
68 if (!path) {
69 return error("Bad URL: %s", url);
70 }
25738089 71 /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */
0de68d28
PA
72 sizen = COMMAND_SIZE;
73 posn = command;
74 of = 0;
75 of |= add_to_string(&posn, &sizen, "env ", 0);
977ed47d 76 of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT "=", 0);
0de68d28
PA
77 of |= add_to_string(&posn, &sizen, path, 1);
78 of |= add_to_string(&posn, &sizen, " ", 0);
79 of |= add_to_string(&posn, &sizen, remote_prog, 1);
80
81 for ( i = 0 ; i < rmt_argc ; i++ ) {
82 of |= add_to_string(&posn, &sizen, " ", 0);
83 of |= add_to_string(&posn, &sizen, rmt_argv[i], 1);
6eb7ed54 84 }
0de68d28
PA
85
86 of |= add_to_string(&posn, &sizen, " -", 0);
87
88 if ( of )
89 return error("Command line too long");
90
91 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
6eb7ed54 92 return error("Couldn't create socket");
0de68d28 93
6eb7ed54 94 if (!fork()) {
c7c81b3a
JR
95 const char *ssh, *ssh_basename;
96 ssh = getenv("GIT_SSH");
97 if (!ssh) ssh = "ssh";
98 ssh_basename = strrchr(ssh, '/');
4852f723
MS
99 if (!ssh_basename)
100 ssh_basename = ssh;
101 else
102 ssh_basename++;
6eb7ed54
DB
103 close(sv[1]);
104 dup2(sv[0], 0);
105 dup2(sv[0], 1);
4852f723 106 execlp(ssh, ssh_basename, host, command, NULL);
6eb7ed54
DB
107 }
108 close(sv[0]);
109 *fd_in = sv[1];
110 *fd_out = sv[1];
111 return 0;
112}