2012-03-27 5 views
4

UNIXドメインソケットリスナーが特定のユーザからの接続のみを受け入れる方法はありますか(chmod/chownは抽象ソケットafaikでは機能しません)。または別の言葉では、(Linuxの)着信接続のuidを取得しますか?UNIXソケット接続のもう一方の端のuidを取得する方法はありますか?

Linux上で抽象的なUnixソケットを使用するDbusには、ポケットベルが呼び出し元を決定するために使用するGetConnectionUnixUserという機能があります。ですから、私はdbus-daemonにそれを行う方法がなければならないと思います。誰でもその仕組みを知っていますか?

答えて

5

ピアクレデンシャルを確認する最も簡単な方法は、SO_PEERCREDです。 tlpiexampleから

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); 
SO_PEERCRED 
      Return the credentials of the foreign process connected to 
      this socket. This is possible only for connected AF_UNIX 
      stream sockets and AF_UNIX stream and datagram socket pairs 
      created using socketpair(2); see unix(7). The returned 
      credentials are those that were in effect at the time of the 
      call to connect(2) or socketpair(2). The argument is a ucred 
      structure; define the _GNU_SOURCE feature test macro to obtain 
      the definition of that structure from <sys/socket.h>. This 
      socket option is read-only. 

:ソケットsockのためにこれを行うには 。 PostgreSQLには他のユニックスのいくつかの亜種があります。

+0

実際これは私が今までの違いを気付かなかったのに、 。 :P – yuyichao

+0

'struct ucred'を定義するためにヘッダファイルを' 'にする前に' #define _GNU_SOURCE'する必要があるLinuxシステムがあることにも言及する価値があります。 – mormegil

4

はい - この操作は、FDの受け渡しとともに、SCM_CREDENTIALSタイプの補助メッセージでサポートされています。関連するコールはman 7 unixに記載されています。

+0

THX = D – yuyichao

+0

'SCM_CREDENTIALS'を使うと、メッセージはカーネルによってチェックされますが、依然としてクライアントがメッセージを送信する必要があります。サーバー側でのみこれを行う方法はありますか? – yuyichao

+0

いいえ。私が知る限り、他の人がそれを送ることなく資格を得る方法はありません。 – duskwuff

関連する問題