KIHARA Hideto
deton****@m1*****
2004年 2月 29日 (日) 12:48:08 JST
Sun, Feb 29, 2004 at 12:56:04AM +0900 において KIHARA Hideto さん曰く: > LOCAL_CREDSを使う場合は、相手から何かデータが送られてこないと駄目なので、 > 接続後にクライアントからリクエストを送って、 > サーバからダミーのバイトを送ってもらうようにしています。 すみません、これだとクライアントがリクエストを送る前に、 サーバからコマンドが送られてくるとうまく動かないですね。 接続前にsetsockoptしておいて、 接続時にサーバからダミーデータを送るように修正したパッチを送ります。 -- 木原 英人 / KIHARA, Hideto / deton****@m1***** http://www1.interq.or.jp/~deton/ diff -urp uim-0.3.0.1-credential/uim/bsd-getpeereid.c uim-0.3.0.1/uim/bsd-getpeereid.c --- uim-0.3.0.1-credential/uim/bsd-getpeereid.c 2004-02-28 14:22:35.000000000 +0900 +++ uim-0.3.0.1/uim/bsd-getpeereid.c 2004-02-29 12:18:45.000000000 +0900 @@ -17,6 +17,8 @@ #include "config.h" #include <sys/types.h> #include <sys/socket.h> +#include <sys/un.h> +#include <sys/param.h> #if !defined(HAVE_GETPEEREID) @@ -34,6 +36,70 @@ getpeereid(int s, uid_t *euid, gid_t *gi return (0); } +#elif defined(LOCAL_CREDS) /* NetBSD */ +int +getpeereid(int s, uid_t *euid, gid_t *gid) +{ +/* Credentials structure */ +#ifdef __NetBSD__ /* XXX: should use autoconf */ +#define HAVE_STRUCT_SOCKCRED +#endif +#if defined(HAVE_STRUCT_CMSGCRED) + typedef struct cmsgcred Cred; + +#define cruid cmcred_euid +#define crgid cmcred_groups[0] +#elif defined(HAVE_STRUCT_FCRED) + typedef struct fcred Cred; + +#define cruid fc_uid +#define crgid fc_gid +#elif defined(HAVE_STRUCT_SOCKCRED) + typedef struct sockcred Cred; + +#define cruid sc_euid +#define crgid sc_egid +#endif + Cred *cred; + + /* Compute size without padding */ + char cmsgmem[CMSG_SPACE(sizeof(Cred))]; /* for NetBSD */ + + /* Point to start of first structure */ + struct cmsghdr *cmsg = (struct cmsghdr *)cmsgmem; + + struct iovec iov; + char buf; + struct msghdr msg; + + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = (char *)cmsg; + msg.msg_controllen = sizeof(cmsgmem); + memset(cmsg, 0, sizeof(cmsgmem)); + + /* + * The one character which is received here is not meaningful; its + * purposes is only to make sure that recvmsg() blocks long enough for + * the other side to send its credentials. + */ + iov.iov_base = &buf; + iov.iov_len = 1; + + if (recvmsg(s, &msg, 0) < 0 || + cmsg->cmsg_len < sizeof(cmsgmem) || + cmsg->cmsg_type != SCM_CREDS) + { + return -1; + } + + cred = (Cred *)CMSG_DATA(cmsg); + *euid = cred->cruid; + *gid = cred->crgid; + + return 0; +} #else int getpeereid(int s, uid_t *euid, gid_t *gid) diff -urp uim-0.3.0.1-credential/uim/uim-helper-client.c uim-0.3.0.1/uim/uim-helper-client.c --- uim-0.3.0.1-credential/uim/uim-helper-client.c 2004-02-28 14:22:36.000000000 +0900 +++ uim-0.3.0.1/uim/uim-helper-client.c 2004-02-29 12:23:25.000000000 +0900 @@ -56,7 +56,15 @@ int uim_helper_init_client_fd(void (*dis perror("fail to create socket"); return -1; } - + +#ifdef LOCAL_CREDS /* for NetBSD */ + /* Set the socket to receive credentials on the next message */ + { + int on = 1; + setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on)); + } +#endif + if(connect(fd, (struct sockaddr *)&server,sizeof(server)) == -1){ int serv_pid = 0; FILE *serv_r = NULL, *serv_w = NULL; diff -urp uim-0.3.0.1-credential/uim/uim-helper-server.c uim-0.3.0.1/uim/uim-helper-server.c --- uim-0.3.0.1-credential/uim/uim-helper-server.c 2004-02-22 00:24:41.000000000 +0900 +++ uim-0.3.0.1/uim/uim-helper-server.c 2004-02-29 12:17:18.000000000 +0900 @@ -192,6 +192,12 @@ uim_helper_server_process_connection(int continue; } cl->fd = new_fd; +#ifdef LOCAL_CREDS /* for NetBSD */ + { + char buf[1] = { '\0' }; + write(cl->fd, buf, 1); + } +#endif /*write(cl->fd, "OK\n", 3);*/ /* fprintf(stderr,"accept new fd:%d\n",new_fd);*/