Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 1 | #include "rsh.h" |
| 2 | |
| 3 | #include <string.h> |
Andreas Gal | ec8f811 | 2005-04-27 20:46:36 | [diff] [blame] | 4 | #include <sys/types.h> |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 5 | #include <sys/socket.h> |
| 6 | |
| 7 | #include "cache.h" |
| 8 | |
| 9 | #define COMMAND_SIZE 4096 |
| 10 | |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 11 | /* |
| 12 | * Write a shell-quoted version of a string into a buffer, and |
| 13 | * return bytes that ought to be output excluding final null. |
| 14 | */ |
| 15 | static int shell_quote(char *buf, int nmax, const char *str) |
| 16 | { |
| 17 | char ch; |
| 18 | int nq; |
| 19 | int oc = 0; |
| 20 | |
| 21 | while ( (ch = *str++) ) { |
| 22 | nq = 0; |
| 23 | if ( strchr(" !\"#$%&\'()*;<=>?[\\]^`{|}", ch) ) |
| 24 | nq = 1; |
| 25 | |
| 26 | if ( nq ) { |
| 27 | if ( nmax > 1 ) { |
| 28 | *buf++ = '\\'; |
| 29 | nmax--; |
| 30 | } |
| 31 | oc++; |
| 32 | } |
| 33 | |
| 34 | if ( nmax > 1 ) { |
| 35 | *buf++ = ch; |
| 36 | nmax--; |
| 37 | } |
| 38 | oc++; |
| 39 | } |
| 40 | |
| 41 | if ( nmax ) |
| 42 | *buf = '\0'; |
| 43 | |
| 44 | return oc; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Append a string to a string buffer, with or without quoting. Return true |
| 49 | * if the buffer overflowed. |
| 50 | */ |
| 51 | static int add_to_string(char **ptrp, int *sizep, const char *str, int quote) |
| 52 | { |
| 53 | char *p = *ptrp; |
| 54 | int size = *sizep; |
| 55 | int oc; |
H. Peter Anvin | e433b07 | 2005-09-23 23:30:50 | [diff] [blame] | 56 | int err = 0; |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 57 | |
| 58 | if ( quote ) { |
| 59 | oc = shell_quote(p, size, str); |
| 60 | } else { |
| 61 | oc = strlen(str); |
| 62 | memcpy(p, str, (oc >= size) ? size-1 : oc); |
| 63 | } |
| 64 | |
| 65 | if ( oc >= size ) { |
H. Peter Anvin | e433b07 | 2005-09-23 23:30:50 | [diff] [blame] | 66 | err = 1; |
| 67 | oc = size-1; |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | *ptrp += oc; |
H. Peter Anvin | e433b07 | 2005-09-23 23:30:50 | [diff] [blame] | 71 | **ptrp = '\0'; |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 72 | *sizep -= oc; |
H. Peter Anvin | e433b07 | 2005-09-23 23:30:50 | [diff] [blame] | 73 | return err; |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 74 | } |
| 75 | |
Linus Torvalds | 001d4a2 | 2005-06-07 21:23:46 | [diff] [blame] | 76 | int setup_connection(int *fd_in, int *fd_out, const char *remote_prog, |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 77 | char *url, int rmt_argc, char **rmt_argv) |
| 78 | { |
| 79 | char *host; |
| 80 | char *path; |
| 81 | int sv[2]; |
| 82 | char command[COMMAND_SIZE]; |
| 83 | char *posn; |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 84 | int sizen; |
| 85 | int of; |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 86 | int i; |
| 87 | |
| 88 | if (!strcmp(url, "-")) { |
| 89 | *fd_in = 0; |
| 90 | *fd_out = 1; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | host = strstr(url, "//"); |
Linus Torvalds | 001d4a2 | 2005-06-07 21:23:46 | [diff] [blame] | 95 | if (host) { |
| 96 | host += 2; |
| 97 | path = strchr(host, '/'); |
| 98 | } else { |
| 99 | host = url; |
| 100 | path = strchr(host, ':'); |
Sven Verdoolaege | 479346a | 2005-06-14 10:37:38 | [diff] [blame] | 101 | if (path) |
| 102 | *(path++) = '\0'; |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 103 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 104 | if (!path) { |
| 105 | return error("Bad URL: %s", url); |
| 106 | } |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 107 | /* $GIT_RSH <host> "env GIR_DIR=<path> <remote_prog> <args...>" */ |
| 108 | sizen = COMMAND_SIZE; |
| 109 | posn = command; |
| 110 | of = 0; |
| 111 | of |= add_to_string(&posn, &sizen, "env ", 0); |
| 112 | of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT, 0); |
| 113 | of |= add_to_string(&posn, &sizen, "=", 0); |
| 114 | of |= add_to_string(&posn, &sizen, path, 1); |
| 115 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 116 | of |= add_to_string(&posn, &sizen, remote_prog, 1); |
| 117 | |
| 118 | for ( i = 0 ; i < rmt_argc ; i++ ) { |
| 119 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 120 | of |= add_to_string(&posn, &sizen, rmt_argv[i], 1); |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 121 | } |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 122 | |
| 123 | of |= add_to_string(&posn, &sizen, " -", 0); |
| 124 | |
| 125 | if ( of ) |
| 126 | return error("Command line too long"); |
| 127 | |
| 128 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 129 | return error("Couldn't create socket"); |
H. Peter Anvin | 0de68d2 | 2005-09-15 19:33:14 | [diff] [blame] | 130 | |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 131 | if (!fork()) { |
Jason Riedy | c7c81b3 | 2005-08-23 20:34:07 | [diff] [blame] | 132 | const char *ssh, *ssh_basename; |
| 133 | ssh = getenv("GIT_SSH"); |
| 134 | if (!ssh) ssh = "ssh"; |
| 135 | ssh_basename = strrchr(ssh, '/'); |
Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 136 | if (!ssh_basename) |
| 137 | ssh_basename = ssh; |
| 138 | else |
| 139 | ssh_basename++; |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 140 | close(sv[1]); |
| 141 | dup2(sv[0], 0); |
| 142 | dup2(sv[0], 1); |
Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 143 | execlp(ssh, ssh_basename, host, command, NULL); |
Daniel Barkalow | 6eb7ed5 | 2005-04-24 01:47:23 | [diff] [blame] | 144 | } |
| 145 | close(sv[0]); |
| 146 | *fd_in = sv[1]; |
| 147 | *fd_out = sv[1]; |
| 148 | return 0; |
| 149 | } |