╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
337 var any_repo = rp.AnyRepo(.xit, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |err| switch (err) { 338 error.RepoNotFound => return false, 339 else => |e| return e, 340 }; 341 defer any_repo.deinit(io, allocator); 342 343 switch (any_repo) { 344 inline else => |*repo| try servePack(repo.self_repo_opts, repo, sess, io, allocator, service), 345 } 346 return true; 347 } 348 349 fn createRepo( 350 comptime repo_opts: rp.RepoOpts(.xit), 351 io: std.Io, 352 allocator: std.mem.Allocator, 353 repo_path: []const u8, 354 ) !rp.Repo(.xit, repo_opts) { 355 var repo = try rp.Repo(.xit, repo_opts).init(io, allocator, .{ .path = repo_path }); 356 errdefer repo.deinit(io, allocator); 357 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 358 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 359 return repo; 360 } 361 362 // wrap the channel I/O in std.Io.Reader/Writer adapters so the repo's pack 363 // functions can consume them like any other stream, then run the requested 364 // service 365 fn servePack( 366 comptime repo_opts: rp.RepoOpts(.xit), 367 repo: *rp.Repo(.xit, repo_opts), 368 sess: *ssh.SessionCtx, 369 io: std.Io, 370 allocator: std.mem.Allocator, 371 service: GitService, 372 ) !void { 373 var session_reader_buf: [4096]u8 = undefined; 374 var session_writer_buf: [4096]u8 = undefined; 375 var session_reader = ssh.SessionReader.init(sess, &session_reader_buf); 376 var session_writer = ssh.SessionWriter.init(sess, &session_writer_buf); 377 378 switch (service) { 379 .upload_pack => try repo.uploadPack(io, allocator, &session_reader.interface, &session_writer.interface, .{}), 380 .receive_pack => try repo.receivePack(io, allocator, &session_reader.interface, &session_writer.interface, .{}), 381 } 382 383 // flush whatever the pack op buffered into our writer adapter 384 session_writer.interface.flush() catch {}; 385 } 386 387 fn parseGitCommand(allocator: std.mem.Allocator, command: []const u8) !ParsedGitCommand { 388 var tokens = try std.process.Args.IteratorGeneral(.{ .single_quotes = true }).init(allocator, command); 389 defer tokens.deinit(); 390 391 const service_token = tokens.next() orelse return error.InvalidCommand; 392 const dir_token = tokens.next() orelse return error.InvalidCommand; 393 394 const service: GitService = 395 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 396 .upload_pack 397 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 398 .receive_pack 399 else 400 return error.UnsupportedService; 401 402 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 403 } 404 405 // true if `fingerprint` matches one of the named user's registered SSH keys 406 fn isKeyAuthorized( 407 io: std.Io, 408 allocator: std.mem.Allocator, 409 admin_repo_path: []const u8, 410 owner_name: []const u8, 411 fingerprint: *const [ssh.fingerprint_len]u8, 412 ) !bool { 413 var arena = std.heap.ArenaAllocator.init(allocator); 414 defer arena.deinit(); 415 416 const user = (try evt.User.readByName(io, allocator, admin_repo_path, &arena, owner_name)) orelse return false; 417 418 var it = std.mem.splitScalar(u8, user.ssh_keys, '\n'); 419 while (it.next()) |line| { 420 const fp = fingerprintOfAuthorizedKey(line) orelse continue; 421 if (std.mem.eql(u8, &fp, fingerprint)) return true; 422 } 423 return false; 424 } 425 426 // the SHA256 fingerprint of one authorized_keys line, or null if the line is 427 // blank or not a parseable "<type> <base64> [comment]" entry 428 fn fingerprintOfAuthorizedKey(line: []const u8) ?[ssh.fingerprint_len]u8 { 429 var it = std.mem.tokenizeAny(u8, line, " \t\r"); 430 _ = it.next() orelse return null; // key type 431 const blob_b64 = it.next() orelse return null; 432 433 const decoder = std.base64.standard.Decoder; 434 const blob_len = decoder.calcSizeForSlice(blob_b64) catch return null; 435 var blob_buf: [4096]u8 = undefined; 436 if (blob_len > blob_buf.len) return null; 437 decoder.decode(blob_buf[0..blob_len], blob_b64) catch return null; 438 439 return ssh.formatFingerprint(blob_buf[0..blob_len]); 440 } 441 442 // write an error to the client's stderr and exit non-zero. stderr (not stdout) 443 // because the git exec sessions that call this carry the pack protocol on 444 // stdout, where stray text breaks the client's pkt-line parser. 445 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 446 try sess.writeStderr("haxy ssh: " ++ msg ++ "\n"); 447 try sess.exit(1); 448 } 449 450 test "parseGitCommand rejects malformed input" { 451 const allocator = std.testing.allocator; 452 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 453 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 454 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 455 } 456 457 test "parseGitCommand happy paths" { 458 const allocator = std.testing.allocator; 459 460 { 461 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 462 defer parsed.deinit(allocator); 463 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 464 try std.testing.expectEqualStrings("some-repo", parsed.dir); 465 } 466 { 467 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 468 defer parsed.deinit(allocator); 469 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 470 try std.testing.expectEqualStrings("user/proj", parsed.dir); 471 } 472 { 473 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 474 defer parsed.deinit(allocator); 475 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 476 try std.testing.expectEqualStrings("repo", parsed.dir); 477 } 478 } 479