blob: d66526941fbe45f99e51babc2c55a63b4baa027c [file] [log] [blame]
Daniel Barkalow6eb7ed52005-04-24 01:47:231#include <string.h>
Andreas Galec8f8112005-04-27 20:46:362#include <sys/types.h>
Daniel Barkalow6eb7ed52005-04-24 01:47:233#include <sys/socket.h>
4
H. Peter Anvinf336af12005-10-10 21:46:145#include "rsh.h"
6#include "quote.h"
Daniel Barkalow6eb7ed52005-04-24 01:47:237#include "cache.h"
8
9#define COMMAND_SIZE 4096
10
H. Peter Anvin0de68d22005-09-15 19:33:1411/*
H. Peter Anvinf336af12005-10-10 21:46:1412 * Append a string to a string buffer, with or without shell quoting.
13 * Return true if the buffer overflowed.
H. Peter Anvin0de68d22005-09-15 19:33:1414 */
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;
H. Peter Anvine433b072005-09-23 23:30:5020 int err = 0;
H. Peter Anvin0de68d22005-09-15 19:33:1421
22 if ( quote ) {
H. Peter Anvinf336af12005-10-10 21:46:1423 oc = sq_quote_buf(p, size, str);
H. Peter Anvin0de68d22005-09-15 19:33:1424 } else {
25 oc = strlen(str);
26 memcpy(p, str, (oc >= size) ? size-1 : oc);
27 }
28
29 if ( oc >= size ) {
H. Peter Anvine433b072005-09-23 23:30:5030 err = 1;
31 oc = size-1;
H. Peter Anvin0de68d22005-09-15 19:33:1432 }
33
34 *ptrp += oc;
H. Peter Anvine433b072005-09-23 23:30:5035 **ptrp = '\0';
H. Peter Anvin0de68d22005-09-15 19:33:1436 *sizep -= oc;
H. Peter Anvine433b072005-09-23 23:30:5037 return err;
H. Peter Anvin0de68d22005-09-15 19:33:1438}
39
Linus Torvalds001d4a22005-06-07 21:23:4640int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
Daniel Barkalow6eb7ed52005-04-24 01:47:2341 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;
H. Peter Anvin0de68d22005-09-15 19:33:1448 int sizen;
49 int of;
Daniel Barkalow6eb7ed52005-04-24 01:47:2350 int i;
51
52 if (!strcmp(url, "-")) {
53 *fd_in = 0;
54 *fd_out = 1;
55 return 0;
56 }
57
58 host = strstr(url, "//");
Linus Torvalds001d4a22005-06-07 21:23:4659 if (host) {
60 host += 2;
61 path = strchr(host, '/');
62 } else {
63 host = url;
64 path = strchr(host, ':');
Sven Verdoolaege479346a2005-06-14 10:37:3865 if (path)
66 *(path++) = '\0';
Daniel Barkalow6eb7ed52005-04-24 01:47:2367 }
Daniel Barkalow6eb7ed52005-04-24 01:47:2368 if (!path) {
69 return error("Bad URL: %s", url);
70 }
Ralf Baechle25738082005-10-13 17:01:3871 /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */
H. Peter Anvin0de68d22005-09-15 19:33:1472 sizen = COMMAND_SIZE;
73 posn = command;
74 of = 0;
75 of |= add_to_string(&posn, &sizen, "env ", 0);
H. Peter Anvin977ed472005-10-10 21:46:1276 of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT "=", 0);
H. Peter Anvin0de68d22005-09-15 19:33:1477 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);
Daniel Barkalow6eb7ed52005-04-24 01:47:2384 }
H. Peter Anvin0de68d22005-09-15 19:33:1485
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))
Daniel Barkalow6eb7ed52005-04-24 01:47:2392 return error("Couldn't create socket");
H. Peter Anvin0de68d22005-09-15 19:33:1493
Daniel Barkalow6eb7ed52005-04-24 01:47:2394 if (!fork()) {
Jason Riedyc7c81b32005-08-23 20:34:0795 const char *ssh, *ssh_basename;
96 ssh = getenv("GIT_SSH");
97 if (!ssh) ssh = "ssh";
98 ssh_basename = strrchr(ssh, '/');
Martin Sivak4852f722005-08-03 15:15:4299 if (!ssh_basename)
100 ssh_basename = ssh;
101 else
102 ssh_basename++;
Daniel Barkalow6eb7ed52005-04-24 01:47:23103 close(sv[1]);
104 dup2(sv[0], 0);
105 dup2(sv[0], 1);
Martin Sivak4852f722005-08-03 15:15:42106 execlp(ssh, ssh_basename, host, command, NULL);
Daniel Barkalow6eb7ed52005-04-24 01:47:23107 }
108 close(sv[0]);
109 *fd_in = sv[1];
110 *fd_out = sv[1];
111 return 0;
112}