有没有办法让UNIX域套接字侦听器只接受来自某个用户的连接(chmod / chown不适用于抽象套接字afaik),换句话说,获取传入连接的uid(在
Linux上)?
在Linux上使用抽象unix套接字的Dbus有一个函数GetConnectionUnixUser,polkit用它来确定调用者.所以我想dbus-daemon必须有办法做到这一点.有谁知道它是如何工作的?
解决方法
检查对等凭据的最简单方法是使用
为套接字袜做这个:
SO_PEERCRED
.
为套接字袜做这个:
int len; struct ucred ucred; len = sizeof(struct ucred); if (getsockopt(sock,SOL_SOCKET,SO_PEERCRED,&ucred,&len) == -1) // check errno printf("Credentials from SO_PEERCRED: pid=%ld,euid=%ld,egid=%ld\n",(long) ucred.pid,(long) ucred.uid,(long) ucred.gid);
06001
从tlpi example. PostgreSQL有一些其他unices的变种.