╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
631 if (best) |held| if (held.role.atLeast(role)) continue; 632 best = .{ .id = holder_id, .role = role }; 633 } 634 const holder = best orelse return .denied; 635 if (!holder.role.atLeast(min_role)) return .denied; 636 return .{ 637 .allowed = switch (service) { 638 .upload_pack => .fetch, 639 .receive_pack => blk: { 640 const names = try repo.repo.event.protectedBranchNames(arena.allocator()); 641 // members change protected branches only by merging a patch 642 const owner = holder.role == .owner; 643 break :blk .{ .push = .{ 644 .user_id = holder.id, 645 .author = try commitAuthor(moment, arena, &holder.id), 646 .protected_branches = if (owner) names else &.{}, 647 .locked_branches = if (owner) &.{} else names, 648 .owner = owner, 649 } }; 650 }, 651 }, 652 }; 653 } 654 655 // an active user as a commit author 656 fn commitAuthor( 657 moment: evt.AdminDB.HashMap(.read_only), 658 arena: *std.heap.ArenaAllocator, 659 user_id: *const [evt.event_id_size]u8, 660 ) !evt.CommitAuthor { 661 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 662 return .{ .name = user.event.name, .email = user.event.email }; 663 } 664 665 // the authorized_keys line of a pubkey blob: its type, then the blob in base64 666 fn authorizedKeyLine(allocator: std.mem.Allocator, blob: []const u8) ![]const u8 { 667 var reader = std.Io.Reader.fixed(blob); 668 const key_type = try ssh.takeString(&reader, 64); 669 const encoder = std.base64.standard.Encoder; 670 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 671 @memcpy(line[0..key_type.len], key_type); 672 line[key_type.len] = ' '; 673 _ = encoder.encode(line[key_type.len + 1 ..], blob); 674 return line; 675 } 676 677 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 678 const body = "ERR haxy ssh: " ++ msg; 679 var packet: [body.len + 4]u8 = undefined; 680 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 681 try sess.writeBytes(line); 682 try sess.exit(1); 683 } 684 685 // a refusal naming the key the client presented. a key on no account gets its 686 // own message, since clients send whichever key they offer first. 687 fn writeKeyError(handler: *const SessionHandler, sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 688 const io = sess.conn.io; 689 const allocator = sess.conn.allocator; 690 const known = known: { 691 var arena = std.heap.ArenaAllocator.init(allocator); 692 defer arena.deinit(); 693 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = handler.admin_repo_path }); 694 defer admin.deinit(io, allocator); 695 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 696 break :known (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &sess.fingerprint)).len > 0; 697 }; 698 if (known) return writeFingerprintError(sess, msg); 699 return writeFingerprintError(sess, "unauthorized: this SSH key isn't added to any haxy account. if you have several keys, your SSH client may be offering a different one first"); 700 } 701 702 // an error line ending with the presented key's fingerprint 703 fn writeFingerprintError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 704 const prefix = "ERR haxy ssh: " ++ msg ++ " (key "; 705 var packet: [4 + prefix.len + ssh.fingerprint_len + 1]u8 = undefined; 706 const line = try std.fmt.bufPrint(&packet, "{x:0>4}" ++ prefix ++ "{s})", .{ packet.len, &sess.fingerprint }); 707 try sess.writeBytes(line); 708 try sess.exit(1); 709 } 710 711 test "parseGitCommand rejects malformed input" { 712 const allocator = std.testing.allocator; 713 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 714 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 715 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 716 } 717 718 test "parseGitCommand happy paths" { 719 const allocator = std.testing.allocator; 720 721 { 722 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 723 defer parsed.deinit(allocator); 724 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 725 try std.testing.expectEqualStrings("some-repo", parsed.dir); 726 } 727 { 728 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 729 defer parsed.deinit(allocator); 730 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 731 try std.testing.expectEqualStrings("user/proj", parsed.dir); 732 } 733 { 734 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 735 defer parsed.deinit(allocator); 736 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 737 try std.testing.expectEqualStrings("repo", parsed.dir); 738 } 739 } 740