╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
545 const dir_token = tokens.next() orelse return error.InvalidCommand; 546 547 const service: GitService = 548 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 549 .upload_pack 550 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 551 .receive_pack 552 else 553 return error.UnsupportedService; 554 555 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 556 } 557 558 const RepoAuthorization = union(enum) { allowed: Allowed, create: RepoCreator, denied, not_found }; 559 560 // what a key may do in an existing repo 561 const Allowed = union(enum) { 562 fetch, 563 push: Pusher, 564 }; 565 566 // the key holder a push acts as 567 const Pusher = struct { 568 user_id: [evt.event_id_size]u8, 569 author: evt.CommitAuthor, 570 protected_branches: []const []const u8, 571 locked_branches: []const []const u8, 572 // only owners may clear the undo history 573 owner: bool, 574 }; 575 576 // the owner of a repo a push may create 577 const RepoCreator = struct { 578 owner_id: [evt.event_id_size]u8, 579 owner: evt.CommitAuthor, 580 }; 581 582 fn authorizeRepoKey( 583 io: std.Io, 584 arena: *std.heap.ArenaAllocator, 585 admin_repo_path: []const u8, 586 users_dir: []const u8, 587 owner_name: []const u8, 588 repo_name: []const u8, 589 service: GitService, 590 fingerprint: *const [ssh.fingerprint_len]u8, 591 ) !RepoAuthorization { 592 const allocator = arena.child_allocator; 593 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 594 error.RepoNotFound => return .not_found, 595 else => |e| return e, 596 }; 597 defer admin.deinit(io, allocator); 598 599 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 600 // no viewer yet, so this carries the base role 601 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 602 if (service == .upload_pack) return .not_found; 603 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 604 if (!try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id, fingerprint)) return .denied; 605 return .{ .create = .{ .owner_id = owner_id, .owner = try commitAuthor(moment, arena, &owner_id) } }; 606 }; 607 608 const min_role: evt.Repo.Role = switch (service) { 609 .upload_pack => .read, 610 .receive_pack => .write, 611 }; 612 // any key may fetch a public repo; a push always needs a key holder 613 if (service == .upload_pack and repo.role != null) return .{ .allowed = .fetch }; 614 615 // a key can be shared, so the push acts as the holder with the highest role 616 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 617 var best: ?struct { id: [evt.event_id_size]u8, role: evt.Repo.Role } = null; 618 for (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, fingerprint)) |holder_id| { 619 const seen = (try evt.readRepoById(io, allocator, arena, users_dir, &owner_id, &repo.event_id, holder_id)) orelse return .not_found; 620 const role = seen.role orelse continue; 621 if (best) |held| if (held.role.atLeast(role)) continue; 622 best = .{ .id = holder_id, .role = role }; 623 } 624 const holder = best orelse return .denied; 625 if (!holder.role.atLeast(min_role)) return .denied; 626 return .{ 627 .allowed = switch (service) { 628 .upload_pack => .fetch, 629 .receive_pack => blk: { 630 const names = try repo.repo.event.protectedBranchNames(arena.allocator()); 631 // members change protected branches only by merging a patch 632 const owner = holder.role == .owner; 633 break :blk .{ .push = .{ 634 .user_id = holder.id, 635 .author = try commitAuthor(moment, arena, &holder.id), 636 .protected_branches = if (owner) names else &.{}, 637 .locked_branches = if (owner) &.{} else names, 638 .owner = owner, 639 } }; 640 }, 641 }, 642 }; 643 } 644 645 // an active user as a commit author 646 fn commitAuthor( 647 moment: evt.AdminDB.HashMap(.read_only), 648 arena: *std.heap.ArenaAllocator, 649 user_id: *const [evt.event_id_size]u8, 650 ) !evt.CommitAuthor { 651 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 652 return .{ .name = user.event.name, .email = user.event.email }; 653 } 654 655 // the authorized_keys line of a pubkey blob: its type, then the blob in base64 656 fn authorizedKeyLine(allocator: std.mem.Allocator, blob: []const u8) ![]const u8 { 657 var reader = std.Io.Reader.fixed(blob); 658 const key_type = try ssh.takeString(&reader, 64); 659 const encoder = std.base64.standard.Encoder; 660 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 661 @memcpy(line[0..key_type.len], key_type); 662 line[key_type.len] = ' '; 663 _ = encoder.encode(line[key_type.len + 1 ..], blob); 664 return line; 665 } 666 667 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 668 const body = "ERR haxy ssh: " ++ msg; 669 var packet: [body.len + 4]u8 = undefined; 670 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 671 try sess.writeBytes(line); 672 try sess.exit(1); 673 } 674 675 test "parseGitCommand rejects malformed input" { 676 const allocator = std.testing.allocator; 677 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 678 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 679 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 680 } 681 682 test "parseGitCommand happy paths" { 683 const allocator = std.testing.allocator; 684 685 { 686 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 687 defer parsed.deinit(allocator); 688 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 689 try std.testing.expectEqualStrings("some-repo", parsed.dir); 690 } 691 { 692 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 693 defer parsed.deinit(allocator); 694 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 695 try std.testing.expectEqualStrings("user/proj", parsed.dir); 696 } 697 { 698 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 699 defer parsed.deinit(allocator); 700 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 701 try std.testing.expectEqualStrings("repo", parsed.dir); 702 } 703 } 704