Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 2 | #include "transport.h" |
| 3 | #include "run-command.h" |
Jeff King | df1ed03 | 2015-09-24 17:07:27 -0400 | [diff] [blame] | 4 | #include "pkt-line.h" |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 5 | |
| 6 | /* |
| 7 | * URL syntax: |
| 8 | * 'command [arg1 [arg2 [...]]]' Invoke command with given arguments. |
| 9 | * Special characters: |
| 10 | * '% ': Literal space in argument. |
| 11 | * '%%': Literal percent sign. |
| 12 | * '%S': Name of service (git-upload-pack/git-upload-archive/ |
| 13 | * git-receive-pack. |
| 14 | * '%s': Same as \s, but with possible git- prefix stripped. |
| 15 | * '%G': Only allowed as first 'character' of argument. Do not pass this |
| 16 | * Argument to command, instead send this as name of repository |
| 17 | * in in-line git://-style request (also activates sending this |
| 18 | * style of request). |
| 19 | * '%V': Only allowed as first 'character' of argument. Used in |
| 20 | * conjunction with '%G': Do not pass this argument to command, |
| 21 | * instead send this as vhost in git://-style request (note: does |
| 22 | * not activate sending git:// style request). |
| 23 | */ |
| 24 | |
| 25 | static char *git_req; |
| 26 | static char *git_req_vhost; |
| 27 | |
| 28 | static char *strip_escapes(const char *str, const char *service, |
| 29 | const char **next) |
| 30 | { |
| 31 | size_t rpos = 0; |
| 32 | int escape = 0; |
| 33 | char special = 0; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 34 | const char *service_noprefix = service; |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 35 | struct strbuf ret = STRBUF_INIT; |
| 36 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 37 | skip_prefix(service_noprefix, "git-", &service_noprefix); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 38 | |
| 39 | /* Pass the service to command. */ |
| 40 | setenv("GIT_EXT_SERVICE", service, 1); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 41 | setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 42 | |
| 43 | /* Scan the length of argument. */ |
| 44 | while (str[rpos] && (escape || str[rpos] != ' ')) { |
| 45 | if (escape) { |
| 46 | switch (str[rpos]) { |
| 47 | case ' ': |
| 48 | case '%': |
| 49 | case 's': |
| 50 | case 'S': |
| 51 | break; |
| 52 | case 'G': |
| 53 | case 'V': |
| 54 | special = str[rpos]; |
| 55 | if (rpos == 1) |
| 56 | break; |
| 57 | /* Fall-through to error. */ |
| 58 | default: |
| 59 | die("Bad remote-ext placeholder '%%%c'.", |
| 60 | str[rpos]); |
| 61 | } |
| 62 | escape = 0; |
| 63 | } else |
| 64 | escape = (str[rpos] == '%'); |
| 65 | rpos++; |
| 66 | } |
| 67 | if (escape && !str[rpos]) |
| 68 | die("remote-ext command has incomplete placeholder"); |
| 69 | *next = str + rpos; |
| 70 | if (**next == ' ') |
| 71 | ++*next; /* Skip over space */ |
| 72 | |
| 73 | /* |
| 74 | * Do the actual placeholder substitution. The string will be short |
| 75 | * enough not to overflow integers. |
| 76 | */ |
| 77 | rpos = special ? 2 : 0; /* Skip first 2 bytes in specials. */ |
| 78 | escape = 0; |
| 79 | while (str[rpos] && (escape || str[rpos] != ' ')) { |
| 80 | if (escape) { |
| 81 | switch (str[rpos]) { |
| 82 | case ' ': |
| 83 | case '%': |
| 84 | strbuf_addch(&ret, str[rpos]); |
| 85 | break; |
| 86 | case 's': |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 87 | strbuf_addstr(&ret, service_noprefix); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 88 | break; |
| 89 | case 'S': |
| 90 | strbuf_addstr(&ret, service); |
| 91 | break; |
| 92 | } |
| 93 | escape = 0; |
| 94 | } else |
| 95 | switch (str[rpos]) { |
| 96 | case '%': |
| 97 | escape = 1; |
| 98 | break; |
| 99 | default: |
| 100 | strbuf_addch(&ret, str[rpos]); |
| 101 | break; |
| 102 | } |
| 103 | rpos++; |
| 104 | } |
| 105 | switch (special) { |
| 106 | case 'G': |
| 107 | git_req = strbuf_detach(&ret, NULL); |
| 108 | return NULL; |
| 109 | case 'V': |
| 110 | git_req_vhost = strbuf_detach(&ret, NULL); |
| 111 | return NULL; |
| 112 | default: |
| 113 | return strbuf_detach(&ret, NULL); |
| 114 | } |
| 115 | } |
| 116 | |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 117 | static void parse_argv(struct argv_array *out, const char *arg, const char *service) |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 118 | { |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 119 | while (*arg) { |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 120 | char *expanded = strip_escapes(arg, service, &arg); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 121 | if (expanded) |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 122 | argv_array_push(out, expanded); |
| 123 | free(expanded); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 124 | } |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void send_git_request(int stdin_fd, const char *serv, const char *repo, |
| 128 | const char *vhost) |
| 129 | { |
Jeff King | df1ed03 | 2015-09-24 17:07:27 -0400 | [diff] [blame] | 130 | if (!vhost) |
| 131 | packet_write(stdin_fd, "%s %s%c", serv, repo, 0); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 132 | else |
Jeff King | df1ed03 | 2015-09-24 17:07:27 -0400 | [diff] [blame] | 133 | packet_write(stdin_fd, "%s %s%chost=%s%c", serv, repo, 0, |
| 134 | vhost, 0); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int run_child(const char *arg, const char *service) |
| 138 | { |
| 139 | int r; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 140 | struct child_process child = CHILD_PROCESS_INIT; |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 141 | |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 142 | child.in = -1; |
| 143 | child.out = -1; |
| 144 | child.err = 0; |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 145 | parse_argv(&child.args, arg, service); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 146 | |
| 147 | if (start_command(&child) < 0) |
| 148 | die("Can't run specified command"); |
| 149 | |
| 150 | if (git_req) |
| 151 | send_git_request(child.in, service, git_req, git_req_vhost); |
| 152 | |
| 153 | r = bidirectional_transfer_loop(child.out, child.in); |
| 154 | if (!r) |
| 155 | r = finish_command(&child); |
| 156 | else |
| 157 | finish_command(&child); |
| 158 | return r; |
| 159 | } |
| 160 | |
| 161 | #define MAXCOMMAND 4096 |
| 162 | |
| 163 | static int command_loop(const char *child) |
| 164 | { |
| 165 | char buffer[MAXCOMMAND]; |
| 166 | |
| 167 | while (1) { |
Jonathan Nieder | 60a2e33 | 2011-01-15 21:49:40 -0600 | [diff] [blame] | 168 | size_t i; |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 169 | if (!fgets(buffer, MAXCOMMAND - 1, stdin)) { |
| 170 | if (ferror(stdin)) |
Li Peng | 832c0e5 | 2016-05-06 20:36:46 +0800 | [diff] [blame] | 171 | die("Command input error"); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 172 | exit(0); |
| 173 | } |
| 174 | /* Strip end of line characters. */ |
Jonathan Nieder | 60a2e33 | 2011-01-15 21:49:40 -0600 | [diff] [blame] | 175 | i = strlen(buffer); |
| 176 | while (i > 0 && isspace(buffer[i - 1])) |
| 177 | buffer[--i] = 0; |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 178 | |
| 179 | if (!strcmp(buffer, "capabilities")) { |
| 180 | printf("*connect\n\n"); |
| 181 | fflush(stdout); |
| 182 | } else if (!strncmp(buffer, "connect ", 8)) { |
| 183 | printf("\n"); |
| 184 | fflush(stdout); |
| 185 | return run_child(child, buffer + 8); |
| 186 | } else { |
| 187 | fprintf(stderr, "Bad command"); |
| 188 | return 1; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int cmd_remote_ext(int argc, const char **argv, const char *prefix) |
| 194 | { |
Ilari Liusvaara | 7851b1e | 2010-11-17 09:15:34 -0800 | [diff] [blame] | 195 | if (argc != 3) |
| 196 | die("Expected two arguments"); |
Ilari Liusvaara | 7f3eceb | 2010-10-12 19:39:43 +0300 | [diff] [blame] | 197 | |
| 198 | return command_loop(argv[2]); |
| 199 | } |