Skip to content

Commit

Permalink
Make daemon auth code a little safer with TcpAddr struct
Browse files Browse the repository at this point in the history
No more assuming one field is Some because the other is Some.
  • Loading branch information
Ericson2314 committed Feb 2, 2023
1 parent 400197e commit 33e95b1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/nix/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ bool matchUser(const std::string & user, const std::string & group, const String
}


struct TcpAddr {
std::string ip;
std::string port;
};

struct PeerInfo
{
std::optional<pid_t> pid;
std::optional<uid_t> uid;
std::optional<gid_t> gid;
std::optional<std::string> ip;
std::optional<std::string> port;
std::optional<TcpAddr> tcpAddr;
};


Expand Down Expand Up @@ -145,8 +149,10 @@ static PeerInfo getPeerInfo(int fd)
char host[1024];
char serv[128];
if (getnameinfo((sockaddr *) &addr, addrlen, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
peer.ip = std::string(host);
peer.port = std::string(serv);
peer.tcpAddr = {
.ip = std::string(host),
.port = std::string(serv),
};
}
}

Expand Down Expand Up @@ -194,8 +200,8 @@ static void authConnection(FdSource & from, FdSink & to)

printInfo(
"accepted connection from %s%s",
peer.ip
? fmt("%s:%s", *peer.ip, *peer.port)
peer.tcpAddr
? fmt("%s:%s", peer.tcpAddr->ip, peer.tcpAddr->port)
: peer.pid && peer.uid
? fmt("pid %s, user %s", std::to_string(*peer.pid), user)
: "<unknown>",
Expand Down

0 comments on commit 33e95b1

Please sign in to comment.