Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 1 | #include <string.h> |
Andreas Gal | ec8f811 | 2005-04-27 13:46:36 -0700 | [diff] [blame] | 2 | #include <sys/types.h> |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 3 | #include <sys/socket.h> |
| 4 | |
H. Peter Anvin | f336af1 | 2005-10-10 14:46:14 -0700 | [diff] [blame] | 5 | #include "rsh.h" |
| 6 | #include "quote.h" |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 7 | #include "cache.h" |
| 8 | |
| 9 | #define COMMAND_SIZE 4096 |
| 10 | |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 11 | /* |
H. Peter Anvin | f336af1 | 2005-10-10 14:46:14 -0700 | [diff] [blame] | 12 | * Append a string to a string buffer, with or without shell quoting. |
| 13 | * Return true if the buffer overflowed. |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 14 | */ |
| 15 | static 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 Anvin | e433b07 | 2005-09-23 16:30:50 -0700 | [diff] [blame] | 20 | int err = 0; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 21 | |
| 22 | if ( quote ) { |
H. Peter Anvin | f336af1 | 2005-10-10 14:46:14 -0700 | [diff] [blame] | 23 | oc = sq_quote_buf(p, size, str); |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 24 | } else { |
| 25 | oc = strlen(str); |
| 26 | memcpy(p, str, (oc >= size) ? size-1 : oc); |
| 27 | } |
| 28 | |
| 29 | if ( oc >= size ) { |
H. Peter Anvin | e433b07 | 2005-09-23 16:30:50 -0700 | [diff] [blame] | 30 | err = 1; |
| 31 | oc = size-1; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | *ptrp += oc; |
H. Peter Anvin | e433b07 | 2005-09-23 16:30:50 -0700 | [diff] [blame] | 35 | **ptrp = '\0'; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 36 | *sizep -= oc; |
H. Peter Anvin | e433b07 | 2005-09-23 16:30:50 -0700 | [diff] [blame] | 37 | return err; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 40 | int setup_connection(int *fd_in, int *fd_out, const char *remote_prog, |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 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; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 48 | int sizen; |
| 49 | int of; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 50 | int i; |
Paul T Darga | c9bc159 | 2006-06-08 14:14:47 -0400 | [diff] [blame] | 51 | pid_t pid; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 52 | |
| 53 | if (!strcmp(url, "-")) { |
| 54 | *fd_in = 0; |
| 55 | *fd_out = 1; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | host = strstr(url, "//"); |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 60 | if (host) { |
| 61 | host += 2; |
| 62 | path = strchr(host, '/'); |
| 63 | } else { |
| 64 | host = url; |
| 65 | path = strchr(host, ':'); |
Sven Verdoolaege | 479346a | 2005-06-14 12:37:38 +0200 | [diff] [blame] | 66 | if (path) |
| 67 | *(path++) = '\0'; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 68 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 69 | if (!path) { |
| 70 | return error("Bad URL: %s", url); |
| 71 | } |
Ralf Baechle | 2573808 | 2005-10-13 10:01:38 -0700 | [diff] [blame] | 72 | /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */ |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 73 | sizen = COMMAND_SIZE; |
| 74 | posn = command; |
| 75 | of = 0; |
| 76 | of |= add_to_string(&posn, &sizen, "env ", 0); |
H. Peter Anvin | 977ed47 | 2005-10-10 14:46:12 -0700 | [diff] [blame] | 77 | of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT "=", 0); |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 78 | of |= add_to_string(&posn, &sizen, path, 1); |
| 79 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 80 | of |= add_to_string(&posn, &sizen, remote_prog, 1); |
| 81 | |
| 82 | for ( i = 0 ; i < rmt_argc ; i++ ) { |
| 83 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 84 | of |= add_to_string(&posn, &sizen, rmt_argv[i], 1); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 85 | } |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 86 | |
| 87 | of |= add_to_string(&posn, &sizen, " -", 0); |
| 88 | |
| 89 | if ( of ) |
| 90 | return error("Command line too long"); |
| 91 | |
| 92 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 93 | return error("Couldn't create socket"); |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 94 | |
Paul T Darga | c9bc159 | 2006-06-08 14:14:47 -0400 | [diff] [blame] | 95 | pid = fork(); |
| 96 | if (pid < 0) |
| 97 | return error("Couldn't fork"); |
| 98 | if (!pid) { |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 99 | const char *ssh, *ssh_basename; |
| 100 | ssh = getenv("GIT_SSH"); |
| 101 | if (!ssh) ssh = "ssh"; |
| 102 | ssh_basename = strrchr(ssh, '/'); |
Martin Sivak | 4852f72 | 2005-08-03 17:15:42 +0200 | [diff] [blame] | 103 | if (!ssh_basename) |
| 104 | ssh_basename = ssh; |
| 105 | else |
| 106 | ssh_basename++; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 107 | close(sv[1]); |
| 108 | dup2(sv[0], 0); |
| 109 | dup2(sv[0], 1); |
Martin Sivak | 4852f72 | 2005-08-03 17:15:42 +0200 | [diff] [blame] | 110 | execlp(ssh, ssh_basename, host, command, NULL); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 111 | } |
| 112 | close(sv[0]); |
| 113 | *fd_in = sv[1]; |
| 114 | *fd_out = sv[1]; |
| 115 | return 0; |
| 116 | } |