╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
451 inline else => |kind| { 452 var repo = try rp.Repo(.xit, any_repo_opts.toRepoOptsWithHash(kind)).open(io, allocator, .{ .path = repo_path }); 453 defer repo.deinit(io, allocator); 454 // stateless since the advertisement already went out 455 try push.receivePackAndConsume(repo.self_repo_opts, io, allocator, &repo, reader, writer, .{ .protocol_version = protocol_version, .is_stateless = true }, creator.owner, handler.server, handler.err, sess); 456 }, 457 } 458 } 459 460 // serve a patch draft: anyone may fetch it, only its author may push to it 461 fn runForkSession( 462 handler: *const SessionHandler, 463 sess: *ssh.SessionCtx, 464 fork_path: []const u8, 465 service: GitService, 466 protocol_version: xit.net_server_common.ProtocolVersion, 467 reader: *std.Io.Reader, 468 writer: *std.Io.Writer, 469 ) !void { 470 const allocator = sess.conn.allocator; 471 const io = sess.conn.io; 472 473 const route = fork.parseRoute(fork_path) orelse return writeError(sess, "invalid fork path"); 474 // the route names the forker and the target repo 475 const forker_repo = evt.parseOwnerRepoPath(route.identity) orelse return writeError(sess, "repo path must be <forker>:<repo>"); 476 477 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = handler.admin_repo_path }); 478 defer admin.deinit(io, allocator); 479 var admin_arena = std.heap.ArenaAllocator.init(allocator); 480 defer admin_arena.deinit(); 481 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 482 const fork_id = try evt.parseEventId(&route.id); 483 const forker_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, forker_repo.owner)) orelse 484 return writeError(sess, "patch draft not found"); 485 const fork_record = (try evt.readForkById(io, allocator, &admin_arena, handler.server.users_dir, &forker_id, &fork_id)) orelse 486 return writeError(sess, "patch draft not found"); 487 if (fork_record.removed) return writeError(sess, "patch draft not found"); 488 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &admin_arena, &forker_id)) orelse 489 return writeError(sess, "invalid patch draft"); 490 if (user.removed) return writeError(sess, "invalid patch draft"); 491 if (service == .receive_pack and !try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &admin_arena, &forker_id, &sess.fingerprint)) 492 return writeError(sess, "unauthorized: this SSH key is not registered to the patch author"); 493 494 const target_owner_id = try evt.parseEventId(&fork_record.event.repo_user_id); 495 const target_id = try evt.parseEventId(&fork_record.event.repo_id); 496 const target = (try evt.readRepoById(io, allocator, &admin_arena, handler.server.users_dir, &target_owner_id, &target_id, forker_id)) orelse 497 return writeError(sess, "repo not found"); 498 // patches that are off take their drafts with them 499 const patch_role = target.repo.event.patch_role orelse return writeError(sess, "patch draft not found"); 500 if (service == .receive_pack) { 501 const role = target.role orelse return writeError(sess, "repo not found"); 502 if (!role.atLeast(patch_role)) return writeError(sess, "unauthorized: your role in this repo may not write patches"); 503 } 504 505 const draft_path = try fork.forkPath(allocator, handler.server.users_dir, &forker_id, &fork_id); 506 defer allocator.free(draft_path); 507 var draft = rp.AnyRepo(.xit, any_repo_opts).open(io, allocator, .{ .path = draft_path }) catch 508 return writeError(sess, "patch draft not found"); 509 defer draft.deinit(io, allocator); 510 if (service == .upload_pack) { 511 var sideband = progress.Sideband{ .writer = writer, .sess = sess }; 512 switch (draft) { 513 inline else => |*repo| try repo.uploadPack(io, allocator, reader, writer, .{ .protocol_version = protocol_version }, &sideband), 514 } 515 } else { 516 if (!std.mem.eql(u8, target.repo.event.name, forker_repo.name)) return writeError(sess, "patch draft belongs to another repo"); 517 const target_path = try evt.repoPath(allocator, handler.server.users_dir, &target_owner_id, &target_id); 518 defer allocator.free(target_path); 519 const author: evt.CommitAuthor = .{ .name = user.event.name, .email = user.event.email }; 520 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 521 switch (draft) { 522 inline else => |*repo| { 523 var target_repo = rp.Repo(.xit, repo.self_repo_opts).open(io, allocator, .{ .path = target_path }) catch 524 return writeError(sess, "repo not found or has the wrong hash"); 525 defer target_repo.deinit(io, allocator); 526 try push.receiveFork(repo.self_repo_opts, io, allocator, repo, &target_repo, handler.server, &route.id, author, timestamp, reader, writer, handler.err, sess); 527 }, 528 } 529 } 530 } 531 532 fn parseGitCommand(allocator: std.mem.Allocator, command: []const u8) !ParsedGitCommand { 533 var tokens = try std.process.Args.IteratorGeneral(.{ .single_quotes = true }).init(allocator, command); 534 defer tokens.deinit(); 535 536 const service_token = tokens.next() orelse return error.InvalidCommand; 537 const dir_token = tokens.next() orelse return error.InvalidCommand; 538 539 const service: GitService = 540 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 541 .upload_pack 542 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 543 .receive_pack 544 else 545 return error.UnsupportedService; 546 547 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 548 } 549 550 const RepoAuthorization = union(enum) { allowed: ?evt.CommitAuthor, create: RepoCreator, denied, not_found }; 551 552 // the owner of a repo a push may create 553 const RepoCreator = struct { 554 owner_id: [evt.event_id_size]u8, 555 owner: evt.CommitAuthor, 556 }; 557 558 fn authorizeRepoKey( 559 io: std.Io, 560 arena: *std.heap.ArenaAllocator, 561 admin_repo_path: []const u8, 562 users_dir: []const u8, 563 owner_name: []const u8, 564 repo_name: []const u8, 565 service: GitService, 566 fingerprint: *const [ssh.fingerprint_len]u8, 567 ) !RepoAuthorization { 568 const allocator = arena.child_allocator; 569 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 570 error.RepoNotFound => return .not_found, 571 else => |e| return e, 572 }; 573 defer admin.deinit(io, allocator); 574 575 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 576 // no viewer yet, so this carries the base role 577 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 578 if (service == .upload_pack) return .not_found; 579 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 580 if (!try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id, fingerprint)) return .denied; 581 return .{ .create = .{ .owner_id = owner_id, .owner = try commitAuthor(moment, arena, &owner_id) } }; 582 }; 583 584 const min_role: evt.Repo.Role = switch (service) { 585 .upload_pack => .read, 586 .receive_pack => .write, 587 }; 588 // skip the key lookups when the base role is enough 589 if (repo.role) |base| { 590 if (base.atLeast(min_role)) return .{ .allowed = null }; 591 } 592 593 // a key can be shared, so a holder whose role falls short doesn't end the search 594 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 595 for (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, fingerprint)) |holder_id| { 596 const seen = (try evt.readRepoById(io, allocator, arena, users_dir, &owner_id, &repo.event_id, holder_id)) orelse return .not_found; 597 const role = seen.role orelse continue; 598 if (role.atLeast(min_role)) return .{ .allowed = try commitAuthor(moment, arena, &holder_id) }; 599 } 600 return .denied; 601 } 602 603 // an active user as a commit author 604 fn commitAuthor( 605 moment: evt.AdminDB.HashMap(.read_only), 606 arena: *std.heap.ArenaAllocator, 607 user_id: *const [evt.event_id_size]u8, 608 ) !evt.CommitAuthor { 609 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 610 return .{ .name = user.event.name, .email = user.event.email }; 611 } 612 613 // the authorized_keys line of a pubkey blob: its type, then the blob in base64 614 fn authorizedKeyLine(allocator: std.mem.Allocator, blob: []const u8) ![]const u8 { 615 var reader = std.Io.Reader.fixed(blob); 616 const key_type = try ssh.takeString(&reader, 64); 617 const encoder = std.base64.standard.Encoder; 618 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 619 @memcpy(line[0..key_type.len], key_type); 620 line[key_type.len] = ' '; 621 _ = encoder.encode(line[key_type.len + 1 ..], blob); 622 return line; 623 } 624 625 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 626 const body = "ERR haxy ssh: " ++ msg; 627 var packet: [body.len + 4]u8 = undefined; 628 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 629 try sess.writeBytes(line); 630 try sess.exit(1); 631 } 632 633 test "parseGitCommand rejects malformed input" { 634 const allocator = std.testing.allocator; 635 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 636 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 637 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 638 } 639 640 test "parseGitCommand happy paths" { 641 const allocator = std.testing.allocator; 642 643 { 644 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 645 defer parsed.deinit(allocator); 646 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 647 try std.testing.expectEqualStrings("some-repo", parsed.dir); 648 } 649 { 650 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 651 defer parsed.deinit(allocator); 652 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 653 try std.testing.expectEqualStrings("user/proj", parsed.dir); 654 } 655 { 656 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 657 defer parsed.deinit(allocator); 658 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 659 try std.testing.expectEqualStrings("repo", parsed.dir); 660 } 661 } 662