Coverage Report

Created: 2026-03-31 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/connect.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "config.h"
5
#include "environment.h"
6
#include "gettext.h"
7
#include "hex.h"
8
#include "pkt-line.h"
9
#include "quote.h"
10
#include "refs.h"
11
#include "run-command.h"
12
#include "remote.h"
13
#include "connect.h"
14
#include "url.h"
15
#include "string-list.h"
16
#include "oid-array.h"
17
#include "path.h"
18
#include "transport.h"
19
#include "trace2.h"
20
#include "strbuf.h"
21
#include "version.h"
22
#include "protocol.h"
23
#include "alias.h"
24
#include "bundle-uri.h"
25
#include "promisor-remote.h"
26
27
static char *server_capabilities_v1;
28
static struct strvec server_capabilities_v2 = STRVEC_INIT;
29
static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
30
31
static int check_ref(const char *name, unsigned int flags)
32
0
{
33
0
  if (!flags)
34
0
    return 1;
35
36
0
  if (!skip_prefix(name, "refs/", &name))
37
0
    return 0;
38
39
  /* REF_NORMAL means that we don't want the magic fake tag refs */
40
0
  if ((flags & REF_NORMAL) && check_refname_format(name,
41
0
               REFNAME_ALLOW_ONELEVEL))
42
0
    return 0;
43
44
  /* REF_BRANCHES means that we want regular branch heads */
45
0
  if ((flags & REF_BRANCHES) && starts_with(name, "heads/"))
46
0
    return 1;
47
48
  /* REF_TAGS means that we want tags */
49
0
  if ((flags & REF_TAGS) && starts_with(name, "tags/"))
50
0
    return 1;
51
52
  /* All type bits clear means that we are ok with anything */
53
0
  return !(flags & ~REF_NORMAL);
54
0
}
55
56
int check_ref_type(const struct ref *ref, int flags)
57
0
{
58
0
  return check_ref(ref->name, flags);
59
0
}
60
61
static NORETURN void die_initial_contact(int unexpected)
62
0
{
63
  /*
64
   * A hang-up after seeing some response from the other end
65
   * means that it is unexpected, as we know the other end is
66
   * willing to talk to us.  A hang-up before seeing any
67
   * response does not necessarily mean an ACL problem, though.
68
   */
69
0
  if (unexpected)
70
0
    die(_("the remote end hung up upon initial contact"));
71
0
  else
72
0
    die(_("Could not read from remote repository.\n\n"
73
0
          "Please make sure you have the correct access rights\n"
74
0
          "and the repository exists."));
75
0
}
76
77
/* Checks if the server supports the capability 'c' */
78
int server_supports_v2(const char *c)
79
0
{
80
0
  size_t i;
81
82
0
  for (i = 0; i < server_capabilities_v2.nr; i++) {
83
0
    const char *out;
84
0
    if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
85
0
        (!*out || *out == '='))
86
0
      return 1;
87
0
  }
88
0
  return 0;
89
0
}
90
91
void ensure_server_supports_v2(const char *c)
92
0
{
93
0
  if (!server_supports_v2(c))
94
0
    die(_("server doesn't support '%s'"), c);
95
0
}
96
97
int server_feature_v2(const char *c, const char **v)
98
0
{
99
0
  size_t i;
100
101
0
  for (i = 0; i < server_capabilities_v2.nr; i++) {
102
0
    const char *out;
103
0
    if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
104
0
        (*out == '=')) {
105
0
      *v = out + 1;
106
0
      return 1;
107
0
    }
108
0
  }
109
0
  return 0;
110
0
}
111
112
int server_supports_feature(const char *c, const char *feature,
113
          int die_on_error)
114
0
{
115
0
  size_t i;
116
117
0
  for (i = 0; i < server_capabilities_v2.nr; i++) {
118
0
    const char *out;
119
0
    if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
120
0
        (!*out || *(out++) == '=')) {
121
0
      if (parse_feature_request(out, feature))
122
0
        return 1;
123
0
      else
124
0
        break;
125
0
    }
126
0
  }
127
128
0
  if (die_on_error)
129
0
    die(_("server doesn't support feature '%s'"), feature);
130
131
0
  return 0;
132
0
}
133
134
static void process_capabilities_v2(struct packet_reader *reader)
135
0
{
136
0
  while (packet_reader_read(reader) == PACKET_READ_NORMAL)
137
0
    strvec_push(&server_capabilities_v2, reader->line);
138
139
0
  if (reader->status != PACKET_READ_FLUSH)
140
0
    die(_("expected flush after capabilities"));
141
0
}
142
143
enum protocol_version discover_version(struct packet_reader *reader)
144
0
{
145
0
  enum protocol_version version = protocol_unknown_version;
146
147
  /*
148
   * Peek the first line of the server's response to
149
   * determine the protocol version the server is speaking.
150
   */
151
0
  switch (packet_reader_peek(reader)) {
152
0
  case PACKET_READ_EOF:
153
0
    die_initial_contact(0);
154
0
  case PACKET_READ_FLUSH:
155
0
  case PACKET_READ_DELIM:
156
0
  case PACKET_READ_RESPONSE_END:
157
0
    version = protocol_v0;
158
0
    break;
159
0
  case PACKET_READ_NORMAL:
160
0
    version = determine_protocol_version_client(reader->line);
161
0
    break;
162
0
  }
163
164
0
  switch (version) {
165
0
  case protocol_v2:
166
0
    process_capabilities_v2(reader);
167
0
    break;
168
0
  case protocol_v1:
169
    /* Read the peeked version line */
170
0
    packet_reader_read(reader);
171
0
    break;
172
0
  case protocol_v0:
173
0
    break;
174
0
  case protocol_unknown_version:
175
0
    BUG("unknown protocol version");
176
0
  }
177
178
0
  trace2_data_intmax("transfer", NULL, "negotiated-version", version);
179
180
0
  return version;
181
0
}
182
183
static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
184
0
{
185
0
  char *sym, *target;
186
0
  struct string_list_item *item;
187
188
0
  if (!len)
189
0
    return; /* just "symref" */
190
  /* e.g. "symref=HEAD:refs/heads/master" */
191
0
  sym = xmemdupz(val, len);
192
0
  target = strchr(sym, ':');
193
0
  if (!target)
194
    /* just "symref=something" */
195
0
    goto reject;
196
0
  *(target++) = '\0';
197
0
  if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
198
0
      check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
199
    /* "symref=bogus:pair */
200
0
    goto reject;
201
0
  item = string_list_append_nodup(symref, sym);
202
0
  item->util = target;
203
0
  return;
204
0
reject:
205
0
  free(sym);
206
0
  return;
207
0
}
208
209
static void annotate_refs_with_symref_info(struct ref *ref)
210
0
{
211
0
  struct string_list symref = STRING_LIST_INIT_DUP;
212
0
  size_t offset = 0;
213
214
0
  while (1) {
215
0
    size_t len;
216
0
    const char *val;
217
218
0
    val = next_server_feature_value("symref", &len, &offset);
219
0
    if (!val)
220
0
      break;
221
0
    parse_one_symref_info(&symref, val, len);
222
0
  }
223
0
  string_list_sort(&symref);
224
225
0
  for (; ref; ref = ref->next) {
226
0
    struct string_list_item *item;
227
0
    item = string_list_lookup(&symref, ref->name);
228
0
    if (!item)
229
0
      continue;
230
0
    ref->symref = xstrdup((char *)item->util);
231
0
  }
232
0
  string_list_clear(&symref, 0);
233
0
}
234
235
static void process_capabilities(struct packet_reader *reader, size_t *linelen)
236
0
{
237
0
  const char *feat_val;
238
0
  size_t feat_len;
239
0
  const char *line = reader->line;
240
0
  size_t nul_location = strlen(line);
241
0
  if (nul_location == *linelen)
242
0
    return;
243
244
0
  free(server_capabilities_v1);
245
0
  server_capabilities_v1 = xstrdup(line + nul_location + 1);
246
0
  *linelen = nul_location;
247
248
0
  feat_val = server_feature_value("object-format", &feat_len);
249
0
  if (feat_val) {
250
0
    char *hash_name = xstrndup(feat_val, feat_len);
251
0
    int hash_algo = hash_algo_by_name(hash_name);
252
0
    if (hash_algo != GIT_HASH_UNKNOWN)
253
0
      reader->hash_algo = &hash_algos[hash_algo];
254
0
    free(hash_name);
255
0
  } else {
256
0
    reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY];
257
0
  }
258
0
}
259
260
static int process_dummy_ref(const struct packet_reader *reader)
261
0
{
262
0
  const char *line = reader->line;
263
0
  struct object_id oid;
264
0
  const char *name;
265
266
0
  if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
267
0
    return 0;
268
0
  if (*name != ' ')
269
0
    return 0;
270
0
  name++;
271
272
0
  return oideq(reader->hash_algo->null_oid, &oid) &&
273
0
    !strcmp(name, "capabilities^{}");
274
0
}
275
276
static void check_no_capabilities(const char *line, size_t len)
277
0
{
278
0
  if (strlen(line) != len)
279
0
    warning(_("ignoring capabilities after first line '%s'"),
280
0
      line + strlen(line));
281
0
}
282
283
static int process_ref(const struct packet_reader *reader, size_t len,
284
           struct ref ***list, unsigned int flags,
285
           struct oid_array *extra_have)
286
0
{
287
0
  const char *line = reader->line;
288
0
  struct object_id old_oid;
289
0
  const char *name;
290
291
0
  if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
292
0
    return 0;
293
0
  if (*name != ' ')
294
0
    return 0;
295
0
  name++;
296
297
0
  if (extra_have && !strcmp(name, ".have")) {
298
0
    oid_array_append(extra_have, &old_oid);
299
0
  } else if (!strcmp(name, "capabilities^{}")) {
300
0
    die(_("protocol error: unexpected capabilities^{}"));
301
0
  } else if (check_ref(name, flags)) {
302
0
    struct ref *ref = alloc_ref(name);
303
0
    oidcpy(&ref->old_oid, &old_oid);
304
0
    **list = ref;
305
0
    *list = &ref->next;
306
0
  }
307
0
  check_no_capabilities(line, len);
308
0
  return 1;
309
0
}
310
311
static int process_shallow(const struct packet_reader *reader, size_t len,
312
         struct oid_array *shallow_points)
313
0
{
314
0
  const char *line = reader->line;
315
0
  const char *arg;
316
0
  struct object_id old_oid;
317
318
0
  if (!skip_prefix(line, "shallow ", &arg))
319
0
    return 0;
320
321
0
  if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
322
0
    die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
323
0
  if (!shallow_points)
324
0
    die(_("repository on the other end cannot be shallow"));
325
0
  oid_array_append(shallow_points, &old_oid);
326
0
  check_no_capabilities(line, len);
327
0
  return 1;
328
0
}
329
330
enum get_remote_heads_state {
331
  EXPECTING_FIRST_REF = 0,
332
  EXPECTING_REF,
333
  EXPECTING_SHALLOW,
334
  EXPECTING_DONE,
335
};
336
337
/*
338
 * Read all the refs from the other end
339
 */
340
struct ref **get_remote_heads(struct packet_reader *reader,
341
            struct ref **list, unsigned int flags,
342
            struct oid_array *extra_have,
343
            struct oid_array *shallow_points)
344
0
{
345
0
  struct ref **orig_list = list;
346
0
  size_t len = 0;
347
0
  enum get_remote_heads_state state = EXPECTING_FIRST_REF;
348
349
0
  *list = NULL;
350
351
0
  while (state != EXPECTING_DONE) {
352
0
    switch (packet_reader_read(reader)) {
353
0
    case PACKET_READ_EOF:
354
0
      die_initial_contact(1);
355
0
    case PACKET_READ_NORMAL:
356
0
      len = reader->pktlen;
357
0
      break;
358
0
    case PACKET_READ_FLUSH:
359
0
      state = EXPECTING_DONE;
360
0
      break;
361
0
    case PACKET_READ_DELIM:
362
0
    case PACKET_READ_RESPONSE_END:
363
0
      die(_("invalid packet"));
364
0
    }
365
366
0
    switch (state) {
367
0
    case EXPECTING_FIRST_REF:
368
0
      process_capabilities(reader, &len);
369
0
      if (process_dummy_ref(reader)) {
370
0
        state = EXPECTING_SHALLOW;
371
0
        break;
372
0
      }
373
0
      state = EXPECTING_REF;
374
      /* fallthrough */
375
0
    case EXPECTING_REF:
376
0
      if (process_ref(reader, len, &list, flags, extra_have))
377
0
        break;
378
0
      state = EXPECTING_SHALLOW;
379
      /* fallthrough */
380
0
    case EXPECTING_SHALLOW:
381
0
      if (process_shallow(reader, len, shallow_points))
382
0
        break;
383
0
      die(_("protocol error: unexpected '%s'"), reader->line);
384
0
    case EXPECTING_DONE:
385
0
      break;
386
0
    }
387
0
  }
388
389
0
  annotate_refs_with_symref_info(*orig_list);
390
391
0
  return list;
392
0
}
393
394
/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
395
static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
396
        const char **unborn_head_target)
397
0
{
398
0
  int ret = 1;
399
0
  size_t i = 0;
400
0
  struct object_id old_oid;
401
0
  struct ref *ref;
402
0
  struct string_list line_sections = STRING_LIST_INIT_DUP;
403
0
  const char *end;
404
0
  const char *line = reader->line;
405
406
  /*
407
   * Ref lines have a number of fields which are space deliminated.  The
408
   * first field is the OID of the ref.  The second field is the ref
409
   * name.  Subsequent fields (symref-target and peeled) are optional and
410
   * don't have a particular order.
411
   */
412
0
  if (string_list_split(&line_sections, line, " ", -1) < 2) {
413
0
    ret = 0;
414
0
    goto out;
415
0
  }
416
417
0
  if (!strcmp("unborn", line_sections.items[i].string)) {
418
0
    i++;
419
0
    if (unborn_head_target &&
420
0
        !strcmp("HEAD", line_sections.items[i++].string)) {
421
      /*
422
       * Look for the symref target (if any). If found,
423
       * return it to the caller.
424
       */
425
0
      for (; i < line_sections.nr; i++) {
426
0
        const char *arg = line_sections.items[i].string;
427
428
0
        if (skip_prefix(arg, "symref-target:", &arg)) {
429
0
          *unborn_head_target = xstrdup(arg);
430
0
          break;
431
0
        }
432
0
      }
433
0
    }
434
0
    goto out;
435
0
  }
436
0
  if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
437
0
      *end) {
438
0
    ret = 0;
439
0
    goto out;
440
0
  }
441
442
0
  ref = alloc_ref(line_sections.items[i++].string);
443
444
0
  memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
445
0
  **list = ref;
446
0
  *list = &ref->next;
447
448
0
  for (; i < line_sections.nr; i++) {
449
0
    const char *arg = line_sections.items[i].string;
450
0
    if (skip_prefix(arg, "symref-target:", &arg))
451
0
      ref->symref = xstrdup(arg);
452
453
0
    if (skip_prefix(arg, "peeled:", &arg)) {
454
0
      struct object_id peeled_oid;
455
0
      char *peeled_name;
456
0
      struct ref *peeled;
457
0
      if (parse_oid_hex_algop(arg, &peeled_oid, &end,
458
0
            reader->hash_algo) || *end) {
459
0
        ret = 0;
460
0
        goto out;
461
0
      }
462
463
0
      peeled_name = xstrfmt("%s^{}", ref->name);
464
0
      peeled = alloc_ref(peeled_name);
465
466
0
      memcpy(peeled->old_oid.hash, peeled_oid.hash,
467
0
             reader->hash_algo->rawsz);
468
0
      **list = peeled;
469
0
      *list = &peeled->next;
470
471
0
      free(peeled_name);
472
0
    }
473
0
  }
474
475
0
out:
476
0
  string_list_clear(&line_sections, 0);
477
0
  return ret;
478
0
}
479
480
void check_stateless_delimiter(int stateless_rpc,
481
            struct packet_reader *reader,
482
            const char *error)
483
0
{
484
0
  if (!stateless_rpc)
485
0
    return; /* not in stateless mode, no delimiter expected */
486
0
  if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
487
0
    die("%s", error);
488
0
}
489
490
static void send_capabilities(int fd_out, struct packet_reader *reader)
491
0
{
492
0
  const char *hash_name;
493
0
  const char *promisor_remote_info;
494
495
0
  if (server_supports_v2("agent"))
496
0
    packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
497
498
0
  if (server_feature_v2("object-format", &hash_name)) {
499
0
    int hash_algo = hash_algo_by_name(hash_name);
500
0
    if (hash_algo == GIT_HASH_UNKNOWN)
501
0
      die(_("unknown object format '%s' specified by server"), hash_name);
502
0
    reader->hash_algo = &hash_algos[hash_algo];
503
0
    packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
504
0
  } else {
505
0
    reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY];
506
0
  }
507
0
  if (server_feature_v2("promisor-remote", &promisor_remote_info)) {
508
0
    char *reply;
509
0
    promisor_remote_reply(promisor_remote_info, &reply);
510
0
    if (reply) {
511
0
      packet_write_fmt(fd_out, "promisor-remote=%s", reply);
512
0
      free(reply);
513
0
    }
514
0
  }
515
0
}
516
517
int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
518
        struct bundle_list *bundles, int stateless_rpc)
519
0
{
520
0
  int line_nr = 1;
521
522
  /* Assert bundle-uri support */
523
0
  ensure_server_supports_v2("bundle-uri");
524
525
  /* (Re-)send capabilities */
526
0
  send_capabilities(fd_out, reader);
527
528
  /* Send command */
529
0
  packet_write_fmt(fd_out, "command=bundle-uri\n");
530
0
  packet_delim(fd_out);
531
532
0
  packet_flush(fd_out);
533
534
  /* Process response from server */
535
0
  while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
536
0
    const char *line = reader->line;
537
0
    line_nr++;
538
539
0
    if (!bundle_uri_parse_line(bundles, line))
540
0
      continue;
541
542
0
    return error(_("error on bundle-uri response line %d: %s"),
543
0
           line_nr, line);
544
0
  }
545
546
0
  if (reader->status != PACKET_READ_FLUSH)
547
0
    return error(_("expected flush after bundle-uri listing"));
548
549
  /*
550
   * Might die(), but obscure enough that that's OK, e.g. in
551
   * serve.c we'll call BUG() on its equivalent (the
552
   * PACKET_READ_RESPONSE_END check).
553
   */
554
0
  check_stateless_delimiter(stateless_rpc, reader,
555
0
          _("expected response end packet after ref listing"));
556
557
0
  return 0;
558
0
}
559
560
struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
561
           struct ref **list, int for_push,
562
           struct transport_ls_refs_options *transport_options,
563
           const struct string_list *server_options,
564
           int stateless_rpc)
565
0
{
566
0
  size_t i;
567
0
  struct strvec *ref_prefixes = transport_options ?
568
0
    &transport_options->ref_prefixes : NULL;
569
0
  const char **unborn_head_target = transport_options ?
570
0
    &transport_options->unborn_head_target : NULL;
571
0
  *list = NULL;
572
573
0
  ensure_server_supports_v2("ls-refs");
574
0
  packet_write_fmt(fd_out, "command=ls-refs\n");
575
576
  /* Send capabilities */
577
0
  send_capabilities(fd_out, reader);
578
579
0
  if (server_options && server_options->nr) {
580
0
    ensure_server_supports_v2("server-option");
581
0
    for (i = 0; i < server_options->nr; i++)
582
0
      packet_write_fmt(fd_out, "server-option=%s",
583
0
           server_options->items[i].string);
584
0
  }
585
586
0
  packet_delim(fd_out);
587
  /* When pushing we don't want to request the peeled tags */
588
0
  if (!for_push)
589
0
    packet_write_fmt(fd_out, "peel\n");
590
0
  packet_write_fmt(fd_out, "symrefs\n");
591
0
  if (server_supports_feature("ls-refs", "unborn", 0))
592
0
    packet_write_fmt(fd_out, "unborn\n");
593
0
  for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
594
0
    packet_write_fmt(fd_out, "ref-prefix %s\n",
595
0
         ref_prefixes->v[i]);
596
0
  }
597
0
  packet_flush(fd_out);
598
599
  /* Process response from server */
600
0
  while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
601
0
    if (!process_ref_v2(reader, &list, unborn_head_target))
602
0
      die(_("invalid ls-refs response: %s"), reader->line);
603
0
  }
604
605
0
  if (reader->status != PACKET_READ_FLUSH)
606
0
    die(_("expected flush after ref listing"));
607
608
0
  check_stateless_delimiter(stateless_rpc, reader,
609
0
          _("expected response end packet after ref listing"));
610
611
0
  return list;
612
0
}
613
614
const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
615
0
{
616
0
  const char *orig_start = feature_list;
617
0
  size_t len;
618
619
0
  if (!feature_list)
620
0
    return NULL;
621
622
0
  len = strlen(feature);
623
0
  if (offset)
624
0
    feature_list += *offset;
625
0
  while (*feature_list) {
626
0
    const char *found = strstr(feature_list, feature);
627
0
    if (!found)
628
0
      return NULL;
629
0
    if (feature_list == found || isspace(found[-1])) {
630
0
      const char *value = found + len;
631
      /* feature with no value (e.g., "thin-pack") */
632
0
      if (!*value || isspace(*value)) {
633
0
        if (lenp)
634
0
          *lenp = 0;
635
0
        if (offset)
636
0
          *offset = found + len - orig_start;
637
0
        return value;
638
0
      }
639
      /* feature with a value (e.g., "agent=git/1.2.3-Linux") */
640
0
      else if (*value == '=') {
641
0
        size_t end;
642
643
0
        value++;
644
0
        end = strcspn(value, " \t\n");
645
0
        if (lenp)
646
0
          *lenp = end;
647
0
        if (offset)
648
0
          *offset = value + end - orig_start;
649
0
        return value;
650
0
      }
651
      /*
652
       * otherwise we matched a substring of another feature;
653
       * keep looking
654
       */
655
0
    }
656
0
    feature_list = found + 1;
657
0
  }
658
0
  return NULL;
659
0
}
660
661
int server_supports_hash(const char *desired, int *feature_supported)
662
0
{
663
0
  size_t offset = 0;
664
0
  size_t len;
665
0
  const char *hash;
666
667
0
  hash = next_server_feature_value("object-format", &len, &offset);
668
0
  if (feature_supported)
669
0
    *feature_supported = !!hash;
670
0
  if (!hash) {
671
0
    hash = hash_algos[GIT_HASH_SHA1_LEGACY].name;
672
0
    len = strlen(hash);
673
0
  }
674
0
  while (hash) {
675
0
    if (!xstrncmpz(desired, hash, len))
676
0
      return 1;
677
678
0
    hash = next_server_feature_value("object-format", &len, &offset);
679
0
  }
680
0
  return 0;
681
0
}
682
683
int parse_feature_request(const char *feature_list, const char *feature)
684
0
{
685
0
  return !!parse_feature_value(feature_list, feature, NULL, NULL);
686
0
}
687
688
static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
689
0
{
690
0
  return parse_feature_value(server_capabilities_v1, feature, len, offset);
691
0
}
692
693
const char *server_feature_value(const char *feature, size_t *len)
694
0
{
695
0
  return parse_feature_value(server_capabilities_v1, feature, len, NULL);
696
0
}
697
698
int server_supports(const char *feature)
699
0
{
700
0
  return !!server_feature_value(feature, NULL);
701
0
}
702
703
enum protocol {
704
  PROTO_LOCAL = 1,
705
  PROTO_FILE,
706
  PROTO_SSH,
707
  PROTO_GIT
708
};
709
710
int url_is_local_not_ssh(const char *url)
711
0
{
712
0
  const char *colon = strchr(url, ':');
713
0
  const char *slash = strchr(url, '/');
714
0
  return !colon || (slash && slash < colon) ||
715
0
    (has_dos_drive_prefix(url) && is_valid_path(url));
716
0
}
717
718
static const char *prot_name(enum protocol protocol)
719
0
{
720
0
  switch (protocol) {
721
0
    case PROTO_LOCAL:
722
0
    case PROTO_FILE:
723
0
      return "file";
724
0
    case PROTO_SSH:
725
0
      return "ssh";
726
0
    case PROTO_GIT:
727
0
      return "git";
728
0
    default:
729
0
      return "unknown protocol";
730
0
  }
731
0
}
732
733
static enum protocol get_protocol(const char *name)
734
0
{
735
0
  if (!strcmp(name, "ssh"))
736
0
    return PROTO_SSH;
737
0
  if (!strcmp(name, "git"))
738
0
    return PROTO_GIT;
739
0
  if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
740
0
    return PROTO_SSH;
741
0
  if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
742
0
    return PROTO_SSH;
743
0
  if (!strcmp(name, "file"))
744
0
    return PROTO_FILE;
745
0
  die(_("protocol '%s' is not supported"), name);
746
0
}
747
748
static char *host_end(char **hoststart, int removebrackets)
749
0
{
750
0
  char *host = *hoststart;
751
0
  char *end;
752
0
  char *start = strstr(host, "@[");
753
0
  if (start)
754
0
    start++; /* Jump over '@' */
755
0
  else
756
0
    start = host;
757
0
  if (start[0] == '[') {
758
0
    end = strchr(start + 1, ']');
759
0
    if (end) {
760
0
      if (removebrackets) {
761
0
        *end = 0;
762
0
        memmove(start, start + 1, end - start);
763
0
        end++;
764
0
      }
765
0
    } else
766
0
      end = host;
767
0
  } else
768
0
    end = host;
769
0
  return end;
770
0
}
771
772
0
#define STR_(s) # s
773
0
#define STR(s)  STR_(s)
774
775
static void get_host_and_port(char **host, const char **port)
776
0
{
777
0
  char *colon, *end;
778
0
  end = host_end(host, 1);
779
0
  colon = strchr(end, ':');
780
0
  if (colon) {
781
0
    long portnr = strtol(colon + 1, &end, 10);
782
0
    if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
783
0
      *colon = 0;
784
0
      *port = colon + 1;
785
0
    } else if (!colon[1]) {
786
0
      *colon = 0;
787
0
    }
788
0
  }
789
0
}
790
791
static void enable_keepalive(int sockfd)
792
0
{
793
0
  int ka = 1;
794
795
0
  if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
796
0
    error_errno(_("unable to set SO_KEEPALIVE on socket"));
797
0
}
798
799
#ifndef NO_IPV6
800
801
static const char *ai_name(const struct addrinfo *ai)
802
0
{
803
0
  static char addr[NI_MAXHOST];
804
0
  if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
805
0
      NI_NUMERICHOST) != 0)
806
0
    xsnprintf(addr, sizeof(addr), "(unknown)");
807
808
0
  return addr;
809
0
}
810
811
/*
812
 * Returns a connected socket() fd, or else die()s.
813
 */
814
static int git_tcp_connect_sock(char *host, int flags)
815
0
{
816
0
  struct strbuf error_message = STRBUF_INIT;
817
0
  int sockfd = -1;
818
0
  const char *port = STR(DEFAULT_GIT_PORT);
819
0
  struct addrinfo hints, *ai0, *ai;
820
0
  int gai;
821
0
  int cnt = 0;
822
823
0
  get_host_and_port(&host, &port);
824
0
  if (!*port)
825
0
    port = "<none>";
826
827
0
  memset(&hints, 0, sizeof(hints));
828
0
  if (flags & CONNECT_IPV4)
829
0
    hints.ai_family = AF_INET;
830
0
  else if (flags & CONNECT_IPV6)
831
0
    hints.ai_family = AF_INET6;
832
0
  hints.ai_socktype = SOCK_STREAM;
833
0
  hints.ai_protocol = IPPROTO_TCP;
834
835
0
  if (flags & CONNECT_VERBOSE)
836
0
    fprintf(stderr, _("Looking up %s ... "), host);
837
838
0
  gai = getaddrinfo(host, port, &hints, &ai);
839
0
  if (gai)
840
0
    die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
841
842
0
  if (flags & CONNECT_VERBOSE)
843
    /* TRANSLATORS: this is the end of "Looking up %s ... " */
844
0
    fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
845
846
0
  for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
847
0
    sockfd = socket(ai->ai_family,
848
0
        ai->ai_socktype, ai->ai_protocol);
849
0
    if ((sockfd < 0) ||
850
0
        (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
851
0
      strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
852
0
            host, cnt, ai_name(ai), strerror(errno));
853
0
      if (0 <= sockfd)
854
0
        close(sockfd);
855
0
      sockfd = -1;
856
0
      continue;
857
0
    }
858
0
    if (flags & CONNECT_VERBOSE)
859
0
      fprintf(stderr, "%s ", ai_name(ai));
860
0
    break;
861
0
  }
862
863
0
  freeaddrinfo(ai0);
864
865
0
  if (sockfd < 0)
866
0
    die(_("unable to connect to %s:\n%s"), host, error_message.buf);
867
868
0
  enable_keepalive(sockfd);
869
870
0
  if (flags & CONNECT_VERBOSE)
871
    /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
872
0
    fprintf_ln(stderr, _("done."));
873
874
0
  strbuf_release(&error_message);
875
876
0
  return sockfd;
877
0
}
878
879
#else /* NO_IPV6 */
880
881
/*
882
 * Returns a connected socket() fd, or else die()s.
883
 */
884
static int git_tcp_connect_sock(char *host, int flags)
885
{
886
  struct strbuf error_message = STRBUF_INIT;
887
  int sockfd = -1;
888
  const char *port = STR(DEFAULT_GIT_PORT);
889
  char *ep;
890
  struct hostent *he;
891
  struct sockaddr_in sa;
892
  char **ap;
893
  unsigned int nport;
894
  int cnt;
895
896
  get_host_and_port(&host, &port);
897
898
  if (flags & CONNECT_VERBOSE)
899
    fprintf(stderr, _("Looking up %s ... "), host);
900
901
  he = gethostbyname(host);
902
  if (!he)
903
    die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
904
  nport = strtoul(port, &ep, 10);
905
  if ( ep == port || *ep ) {
906
    /* Not numeric */
907
    struct servent *se = getservbyname(port,"tcp");
908
    if ( !se )
909
      die(_("unknown port %s"), port);
910
    nport = se->s_port;
911
  }
912
913
  if (flags & CONNECT_VERBOSE)
914
    /* TRANSLATORS: this is the end of "Looking up %s ... " */
915
    fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
916
917
  for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
918
    memset(&sa, 0, sizeof sa);
919
    sa.sin_family = he->h_addrtype;
920
    sa.sin_port = htons(nport);
921
    memcpy(&sa.sin_addr, *ap, he->h_length);
922
923
    sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
924
    if ((sockfd < 0) ||
925
        connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
926
      strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
927
        host,
928
        cnt,
929
        inet_ntoa(*(struct in_addr *)&sa.sin_addr),
930
        strerror(errno));
931
      if (0 <= sockfd)
932
        close(sockfd);
933
      sockfd = -1;
934
      continue;
935
    }
936
    if (flags & CONNECT_VERBOSE)
937
      fprintf(stderr, "%s ",
938
        inet_ntoa(*(struct in_addr *)&sa.sin_addr));
939
    break;
940
  }
941
942
  if (sockfd < 0)
943
    die(_("unable to connect to %s:\n%s"), host, error_message.buf);
944
945
  enable_keepalive(sockfd);
946
947
  if (flags & CONNECT_VERBOSE)
948
    /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
949
    fprintf_ln(stderr, _("done."));
950
951
  return sockfd;
952
}
953
954
#endif /* NO_IPV6 */
955
956
957
/*
958
 * Dummy child_process returned by git_connect() if the transport protocol
959
 * does not need fork(2).
960
 */
961
static struct child_process no_fork = CHILD_PROCESS_INIT;
962
963
int git_connection_is_socket(struct child_process *conn)
964
0
{
965
0
  return conn == &no_fork;
966
0
}
967
968
static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
969
0
{
970
0
  int sockfd = git_tcp_connect_sock(host, flags);
971
972
0
  fd[0] = sockfd;
973
0
  fd[1] = dup(sockfd);
974
975
0
  return &no_fork;
976
0
}
977
978
979
static char *git_proxy_command;
980
981
static int git_proxy_command_options(const char *var, const char *value,
982
    const struct config_context *ctx, void *cb)
983
0
{
984
0
  if (!strcmp(var, "core.gitproxy")) {
985
0
    const char *for_pos;
986
0
    int matchlen = -1;
987
0
    int hostlen;
988
0
    const char *rhost_name = cb;
989
0
    int rhost_len = strlen(rhost_name);
990
991
0
    if (git_proxy_command)
992
0
      return 0;
993
0
    if (!value)
994
0
      return config_error_nonbool(var);
995
    /* [core]
996
     * ;# matches www.kernel.org as well
997
     * gitproxy = netcatter-1 for kernel.org
998
     * gitproxy = netcatter-2 for sample.xz
999
     * gitproxy = netcatter-default
1000
     */
1001
0
    for_pos = strstr(value, " for ");
1002
0
    if (!for_pos)
1003
      /* matches everybody */
1004
0
      matchlen = strlen(value);
1005
0
    else {
1006
0
      hostlen = strlen(for_pos + 5);
1007
0
      if (rhost_len < hostlen)
1008
0
        matchlen = -1;
1009
0
      else if (!strncmp(for_pos + 5,
1010
0
            rhost_name + rhost_len - hostlen,
1011
0
            hostlen) &&
1012
0
         ((rhost_len == hostlen) ||
1013
0
          rhost_name[rhost_len - hostlen -1] == '.'))
1014
0
        matchlen = for_pos - value;
1015
0
      else
1016
0
        matchlen = -1;
1017
0
    }
1018
0
    if (0 <= matchlen) {
1019
      /* core.gitproxy = none for kernel.org */
1020
0
      if (matchlen == 4 &&
1021
0
          !memcmp(value, "none", 4))
1022
0
        matchlen = 0;
1023
0
      git_proxy_command = xmemdupz(value, matchlen);
1024
0
    }
1025
0
    return 0;
1026
0
  }
1027
1028
0
  return git_default_config(var, value, ctx, cb);
1029
0
}
1030
1031
static int git_use_proxy(const char *host)
1032
0
{
1033
0
  git_proxy_command = getenv("GIT_PROXY_COMMAND");
1034
0
  repo_config(the_repository, git_proxy_command_options, (void*)host);
1035
0
  return (git_proxy_command && *git_proxy_command);
1036
0
}
1037
1038
static struct child_process *git_proxy_connect(int fd[2], char *host)
1039
0
{
1040
0
  const char *port = STR(DEFAULT_GIT_PORT);
1041
0
  struct child_process *proxy;
1042
1043
0
  get_host_and_port(&host, &port);
1044
1045
0
  if (looks_like_command_line_option(host))
1046
0
    die(_("strange hostname '%s' blocked"), host);
1047
0
  if (looks_like_command_line_option(port))
1048
0
    die(_("strange port '%s' blocked"), port);
1049
1050
0
  proxy = xmalloc(sizeof(*proxy));
1051
0
  child_process_init(proxy);
1052
0
  strvec_push(&proxy->args, git_proxy_command);
1053
0
  strvec_push(&proxy->args, host);
1054
0
  strvec_push(&proxy->args, port);
1055
0
  proxy->in = -1;
1056
0
  proxy->out = -1;
1057
0
  if (start_command(proxy))
1058
0
    die(_("cannot start proxy %s"), git_proxy_command);
1059
0
  fd[0] = proxy->out; /* read from proxy stdout */
1060
0
  fd[1] = proxy->in;  /* write to proxy stdin */
1061
0
  return proxy;
1062
0
}
1063
1064
static char *get_port(char *host)
1065
0
{
1066
0
  char *end;
1067
0
  char *p = strchr(host, ':');
1068
1069
0
  if (p) {
1070
0
    long port = strtol(p + 1, &end, 10);
1071
0
    if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
1072
0
      *p = '\0';
1073
0
      return p+1;
1074
0
    }
1075
0
  }
1076
1077
0
  return NULL;
1078
0
}
1079
1080
/*
1081
 * Extract protocol and relevant parts from the specified connection URL.
1082
 * The caller must free() the returned strings.
1083
 */
1084
static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
1085
               char **ret_path)
1086
0
{
1087
0
  char *url;
1088
0
  char *host, *path;
1089
0
  char *end;
1090
0
  int separator = '/';
1091
0
  enum protocol protocol = PROTO_LOCAL;
1092
1093
0
  if (is_url(url_orig))
1094
0
    url = url_decode(url_orig);
1095
0
  else
1096
0
    url = xstrdup(url_orig);
1097
1098
0
  host = strstr(url, "://");
1099
0
  if (host) {
1100
0
    *host = '\0';
1101
0
    protocol = get_protocol(url);
1102
0
    host += 3;
1103
0
  } else {
1104
0
    host = url;
1105
0
    if (!url_is_local_not_ssh(url)) {
1106
0
      protocol = PROTO_SSH;
1107
0
      separator = ':';
1108
0
    }
1109
0
  }
1110
1111
  /*
1112
   * Don't do destructive transforms as protocol code does
1113
   * '[]' unwrapping in get_host_and_port()
1114
   */
1115
0
  end = host_end(&host, 0);
1116
1117
0
  if (protocol == PROTO_LOCAL)
1118
0
    path = end;
1119
0
  else if (protocol == PROTO_FILE && *host != '/' &&
1120
0
     !has_dos_drive_prefix(host) &&
1121
0
     offset_1st_component(host - 2) > 1)
1122
0
    path = host - 2; /* include the leading "//" */
1123
0
  else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
1124
0
    path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1125
0
  else
1126
0
    path = strchr(end, separator);
1127
1128
0
  if (!path || !*path)
1129
0
    die(_("no path specified; see 'git help pull' for valid url syntax"));
1130
1131
  /*
1132
   * null-terminate hostname and point path to ~ for URL's like this:
1133
   *    ssh://host.xz/~user/repo
1134
   */
1135
1136
0
  end = path; /* Need to \0 terminate host here */
1137
0
  if (separator == ':')
1138
0
    path++; /* path starts after ':' */
1139
0
  if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
1140
0
    if (path[1] == '~')
1141
0
      path++;
1142
0
  }
1143
1144
0
  path = xstrdup(path);
1145
0
  *end = '\0';
1146
1147
0
  *ret_host = xstrdup(host);
1148
0
  *ret_path = path;
1149
0
  free(url);
1150
0
  return protocol;
1151
0
}
1152
1153
static const char *get_ssh_command(void)
1154
0
{
1155
0
  const char *ssh;
1156
1157
0
  if ((ssh = getenv("GIT_SSH_COMMAND")))
1158
0
    return ssh;
1159
1160
0
  if (!repo_config_get_string_tmp(the_repository, "core.sshcommand", &ssh))
1161
0
    return ssh;
1162
1163
0
  return NULL;
1164
0
}
1165
1166
enum ssh_variant {
1167
  VARIANT_AUTO,
1168
  VARIANT_SIMPLE,
1169
  VARIANT_SSH,
1170
  VARIANT_PLINK,
1171
  VARIANT_PUTTY,
1172
  VARIANT_TORTOISEPLINK,
1173
};
1174
1175
static void override_ssh_variant(enum ssh_variant *ssh_variant)
1176
0
{
1177
0
  const char *variant = getenv("GIT_SSH_VARIANT");
1178
1179
0
  if (!variant && repo_config_get_string_tmp(the_repository, "ssh.variant", &variant))
1180
0
    return;
1181
1182
0
  if (!strcmp(variant, "auto"))
1183
0
    *ssh_variant = VARIANT_AUTO;
1184
0
  else if (!strcmp(variant, "plink"))
1185
0
    *ssh_variant = VARIANT_PLINK;
1186
0
  else if (!strcmp(variant, "putty"))
1187
0
    *ssh_variant = VARIANT_PUTTY;
1188
0
  else if (!strcmp(variant, "tortoiseplink"))
1189
0
    *ssh_variant = VARIANT_TORTOISEPLINK;
1190
0
  else if (!strcmp(variant, "simple"))
1191
0
    *ssh_variant = VARIANT_SIMPLE;
1192
0
  else
1193
0
    *ssh_variant = VARIANT_SSH;
1194
0
}
1195
1196
static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1197
                int is_cmdline)
1198
0
{
1199
0
  enum ssh_variant ssh_variant = VARIANT_AUTO;
1200
0
  const char *variant;
1201
0
  char *p = NULL;
1202
1203
0
  override_ssh_variant(&ssh_variant);
1204
1205
0
  if (ssh_variant != VARIANT_AUTO)
1206
0
    return ssh_variant;
1207
1208
0
  if (!is_cmdline) {
1209
0
    p = xstrdup(ssh_command);
1210
0
    variant = basename(p);
1211
0
  } else {
1212
0
    const char **ssh_argv;
1213
1214
0
    p = xstrdup(ssh_command);
1215
0
    if (split_cmdline(p, &ssh_argv) > 0) {
1216
0
      variant = basename((char *)ssh_argv[0]);
1217
      /*
1218
       * At this point, variant points into the buffer
1219
       * referenced by p, hence we do not need ssh_argv
1220
       * any longer.
1221
       */
1222
0
      free(ssh_argv);
1223
0
    } else {
1224
0
      free(p);
1225
0
      return ssh_variant;
1226
0
    }
1227
0
  }
1228
1229
0
  if (!strcasecmp(variant, "ssh") ||
1230
0
      !strcasecmp(variant, "ssh.exe"))
1231
0
    ssh_variant = VARIANT_SSH;
1232
0
  else if (!strcasecmp(variant, "plink") ||
1233
0
     !strcasecmp(variant, "plink.exe"))
1234
0
    ssh_variant = VARIANT_PLINK;
1235
0
  else if (!strcasecmp(variant, "tortoiseplink") ||
1236
0
     !strcasecmp(variant, "tortoiseplink.exe"))
1237
0
    ssh_variant = VARIANT_TORTOISEPLINK;
1238
1239
0
  free(p);
1240
0
  return ssh_variant;
1241
0
}
1242
1243
/*
1244
 * Open a connection using Git's native protocol.
1245
 *
1246
 * The caller is responsible for freeing hostandport, but this function may
1247
 * modify it (for example, to truncate it to remove the port part).
1248
 */
1249
static struct child_process *git_connect_git(int fd[2], char *hostandport,
1250
               const char *path, const char *prog,
1251
               enum protocol_version version,
1252
               int flags)
1253
0
{
1254
0
  struct child_process *conn;
1255
0
  struct strbuf request = STRBUF_INIT;
1256
  /*
1257
   * Set up virtual host information based on where we will
1258
   * connect, unless the user has overridden us in
1259
   * the environment.
1260
   */
1261
0
  char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1262
0
  if (target_host)
1263
0
    target_host = xstrdup(target_host);
1264
0
  else
1265
0
    target_host = xstrdup(hostandport);
1266
1267
0
  transport_check_allowed("git");
1268
0
  if (strchr(target_host, '\n') || strchr(path, '\n'))
1269
0
    die(_("newline is forbidden in git:// hosts and repo paths"));
1270
1271
  /*
1272
   * These underlying connection commands die() if they
1273
   * cannot connect.
1274
   */
1275
0
  if (git_use_proxy(hostandport))
1276
0
    conn = git_proxy_connect(fd, hostandport);
1277
0
  else
1278
0
    conn = git_tcp_connect(fd, hostandport, flags);
1279
  /*
1280
   * Separate original protocol components prog and path
1281
   * from extended host header with a NUL byte.
1282
   *
1283
   * Note: Do not add any other headers here!  Doing so
1284
   * will cause older git-daemon servers to crash.
1285
   */
1286
0
  strbuf_addf(&request,
1287
0
        "%s %s%chost=%s%c",
1288
0
        prog, path, 0,
1289
0
        target_host, 0);
1290
1291
  /* If using a new version put that stuff here after a second null byte */
1292
0
  if (version > 0) {
1293
0
    strbuf_addch(&request, '\0');
1294
0
    strbuf_addf(&request, "version=%d%c",
1295
0
          version, '\0');
1296
0
  }
1297
1298
0
  packet_write(fd[1], request.buf, request.len);
1299
1300
0
  free(target_host);
1301
0
  strbuf_release(&request);
1302
0
  return conn;
1303
0
}
1304
1305
/*
1306
 * Append the appropriate environment variables to `env` and options to
1307
 * `args` for running ssh in Git's SSH-tunneled transport.
1308
 */
1309
static void push_ssh_options(struct strvec *args, struct strvec *env,
1310
           enum ssh_variant variant, const char *port,
1311
           enum protocol_version version, int flags)
1312
0
{
1313
0
  if (variant == VARIANT_SSH &&
1314
0
      version > 0) {
1315
0
    strvec_push(args, "-o");
1316
0
    strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1317
0
    strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1318
0
           version);
1319
0
  }
1320
1321
0
  if (flags & CONNECT_IPV4) {
1322
0
    switch (variant) {
1323
0
    case VARIANT_AUTO:
1324
0
      BUG("VARIANT_AUTO passed to push_ssh_options");
1325
0
    case VARIANT_SIMPLE:
1326
0
      die(_("ssh variant 'simple' does not support -4"));
1327
0
    case VARIANT_SSH:
1328
0
    case VARIANT_PLINK:
1329
0
    case VARIANT_PUTTY:
1330
0
    case VARIANT_TORTOISEPLINK:
1331
0
      strvec_push(args, "-4");
1332
0
    }
1333
0
  } else if (flags & CONNECT_IPV6) {
1334
0
    switch (variant) {
1335
0
    case VARIANT_AUTO:
1336
0
      BUG("VARIANT_AUTO passed to push_ssh_options");
1337
0
    case VARIANT_SIMPLE:
1338
0
      die(_("ssh variant 'simple' does not support -6"));
1339
0
    case VARIANT_SSH:
1340
0
    case VARIANT_PLINK:
1341
0
    case VARIANT_PUTTY:
1342
0
    case VARIANT_TORTOISEPLINK:
1343
0
      strvec_push(args, "-6");
1344
0
    }
1345
0
  }
1346
1347
0
  if (variant == VARIANT_TORTOISEPLINK)
1348
0
    strvec_push(args, "-batch");
1349
1350
0
  if (port) {
1351
0
    switch (variant) {
1352
0
    case VARIANT_AUTO:
1353
0
      BUG("VARIANT_AUTO passed to push_ssh_options");
1354
0
    case VARIANT_SIMPLE:
1355
0
      die(_("ssh variant 'simple' does not support setting port"));
1356
0
    case VARIANT_SSH:
1357
0
      strvec_push(args, "-p");
1358
0
      break;
1359
0
    case VARIANT_PLINK:
1360
0
    case VARIANT_PUTTY:
1361
0
    case VARIANT_TORTOISEPLINK:
1362
0
      strvec_push(args, "-P");
1363
0
    }
1364
1365
0
    strvec_push(args, port);
1366
0
  }
1367
0
}
1368
1369
/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1370
static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1371
        const char *port, enum protocol_version version,
1372
        int flags)
1373
0
{
1374
0
  const char *ssh;
1375
0
  enum ssh_variant variant;
1376
1377
0
  if (looks_like_command_line_option(ssh_host))
1378
0
    die(_("strange hostname '%s' blocked"), ssh_host);
1379
1380
0
  ssh = get_ssh_command();
1381
0
  if (ssh) {
1382
0
    variant = determine_ssh_variant(ssh, 1);
1383
0
  } else {
1384
    /*
1385
     * GIT_SSH is the no-shell version of
1386
     * GIT_SSH_COMMAND (and must remain so for
1387
     * historical compatibility).
1388
     */
1389
0
    conn->use_shell = 0;
1390
1391
0
    ssh = getenv("GIT_SSH");
1392
0
    if (!ssh)
1393
0
      ssh = "ssh";
1394
0
    variant = determine_ssh_variant(ssh, 0);
1395
0
  }
1396
1397
0
  if (variant == VARIANT_AUTO) {
1398
0
    struct child_process detect = CHILD_PROCESS_INIT;
1399
1400
0
    detect.use_shell = conn->use_shell;
1401
0
    detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1402
1403
0
    strvec_push(&detect.args, ssh);
1404
0
    strvec_push(&detect.args, "-G");
1405
0
    push_ssh_options(&detect.args, &detect.env,
1406
0
         VARIANT_SSH, port, version, flags);
1407
0
    strvec_push(&detect.args, ssh_host);
1408
1409
0
    variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1410
0
  }
1411
1412
0
  strvec_push(&conn->args, ssh);
1413
0
  push_ssh_options(&conn->args, &conn->env, variant, port, version,
1414
0
       flags);
1415
0
  strvec_push(&conn->args, ssh_host);
1416
0
}
1417
1418
/*
1419
 * This returns the dummy child_process `no_fork` if the transport protocol
1420
 * does not need fork(2), or a struct child_process object if it does.  Once
1421
 * done, finish the connection with finish_connect() with the value returned
1422
 * from this function (it is safe to call finish_connect() with NULL to
1423
 * support the former case).
1424
 *
1425
 * If it returns, the connect is successful; it just dies on errors (this
1426
 * will hopefully be changed in a libification effort, to return NULL when
1427
 * the connection failed).
1428
 */
1429
struct child_process *git_connect(int fd[2], const char *url,
1430
          const char *name,
1431
          const char *prog, int flags)
1432
0
{
1433
0
  char *hostandport, *path;
1434
0
  struct child_process *conn;
1435
0
  enum protocol protocol;
1436
0
  enum protocol_version version = get_protocol_version_config();
1437
1438
  /*
1439
   * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1440
   * to perform any operation that doesn't involve upload-pack (i.e., a
1441
   * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1442
   * to do anything else (like push or remote archive) via v2.
1443
   */
1444
0
  if (version == protocol_v2 && strcmp("git-upload-pack", name))
1445
0
    version = protocol_v0;
1446
1447
  /* Without this we cannot rely on waitpid() to tell
1448
   * what happened to our children.
1449
   */
1450
0
  signal(SIGCHLD, SIG_DFL);
1451
1452
0
  protocol = parse_connect_url(url, &hostandport, &path);
1453
0
  if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1454
0
    printf("Diag: url=%s\n", url ? url : "NULL");
1455
0
    printf("Diag: protocol=%s\n", prot_name(protocol));
1456
0
    printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1457
0
    printf("Diag: path=%s\n", path ? path : "NULL");
1458
0
    conn = NULL;
1459
0
  } else if (protocol == PROTO_GIT) {
1460
0
    conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1461
0
    conn->trace2_child_class = "transport/git";
1462
0
  } else {
1463
0
    struct strbuf cmd = STRBUF_INIT;
1464
0
    const char *const *var;
1465
1466
0
    conn = xmalloc(sizeof(*conn));
1467
0
    child_process_init(conn);
1468
1469
0
    if (looks_like_command_line_option(path))
1470
0
      die(_("strange pathname '%s' blocked"), path);
1471
1472
0
    strbuf_addstr(&cmd, prog);
1473
0
    strbuf_addch(&cmd, ' ');
1474
0
    sq_quote_buf(&cmd, path);
1475
1476
    /* remove repo-local variables from the environment */
1477
0
    for (var = local_repo_env; *var; var++)
1478
0
      strvec_push(&conn->env, *var);
1479
1480
0
    conn->use_shell = 1;
1481
0
    conn->in = conn->out = -1;
1482
0
    if (protocol == PROTO_SSH) {
1483
0
      char *ssh_host = hostandport;
1484
0
      const char *port = NULL;
1485
0
      transport_check_allowed("ssh");
1486
0
      get_host_and_port(&ssh_host, &port);
1487
1488
0
      if (!port)
1489
0
        port = get_port(ssh_host);
1490
1491
0
      if (flags & CONNECT_DIAG_URL) {
1492
0
        printf("Diag: url=%s\n", url ? url : "NULL");
1493
0
        printf("Diag: protocol=%s\n", prot_name(protocol));
1494
0
        printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1495
0
        printf("Diag: port=%s\n", port ? port : "NONE");
1496
0
        printf("Diag: path=%s\n", path ? path : "NULL");
1497
1498
0
        free(hostandport);
1499
0
        free(path);
1500
0
        child_process_clear(conn);
1501
0
        free(conn);
1502
0
        strbuf_release(&cmd);
1503
0
        return NULL;
1504
0
      }
1505
0
      conn->trace2_child_class = "transport/ssh";
1506
0
      fill_ssh_args(conn, ssh_host, port, version, flags);
1507
0
    } else {
1508
0
      transport_check_allowed("file");
1509
0
      conn->trace2_child_class = "transport/file";
1510
0
      if (version > 0) {
1511
0
        strvec_pushf(&conn->env,
1512
0
               GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1513
0
               version);
1514
0
      }
1515
0
    }
1516
0
    strvec_push(&conn->args, cmd.buf);
1517
1518
0
    if (start_command(conn))
1519
0
      die(_("unable to fork"));
1520
1521
0
    fd[0] = conn->out; /* read from child's stdout */
1522
0
    fd[1] = conn->in;  /* write to child's stdin */
1523
0
    strbuf_release(&cmd);
1524
0
  }
1525
0
  free(hostandport);
1526
0
  free(path);
1527
0
  return conn;
1528
0
}
1529
1530
int finish_connect(struct child_process *conn)
1531
0
{
1532
0
  int code;
1533
0
  if (!conn || git_connection_is_socket(conn))
1534
0
    return 0;
1535
1536
0
  code = finish_command(conn);
1537
0
  free(conn);
1538
0
  return code;
1539
0
}