/* * call-seq: * basicsocket.getpeereid => [euid, egid] * * Returns the user and group on the peer of the UNIX socket. * The result is a two element array which contains the effective uid and the effective gid. * * Socket.unix_server_loop("/tmp/sock") {|s| * begin * euid, egid = s.getpeereid * * # Check the connected client is myself or not. * next if euid != Process.uid * * # do something about my resource. * * ensure * s.close * end * } * */ static VALUE bsock_getpeereid(VALUE self) { UNRUBBY_SOCKET_HACK; #if defined(HAVE_GETPEEREID) rb_io_t *fptr; uid_t euid; gid_t egid; GetOpenFile(self, fptr); if (getpeereid(fptr->fd, &euid, &egid) == -1) rb_sys_fail("getpeereid"); return rb_assoc_new(UIDT2NUM(euid), GIDT2NUM(egid)); #elif defined(SO_PEERCRED) /* GNU/Linux */ rb_io_t *fptr; struct ucred cred; socklen_t len = sizeof(cred); GetOpenFile(self, fptr); if (getsockopt(fptr->fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) rb_sys_fail("getsockopt(SO_PEERCRED)"); return rb_assoc_new(UIDT2NUM(cred.uid), GIDT2NUM(cred.gid)); #elif defined(HAVE_GETPEERUCRED) /* Solaris */ rb_io_t *fptr; ucred_t *uc = NULL; VALUE ret; GetOpenFile(self, fptr); if (getpeerucred(fptr->fd, &uc) == -1) rb_sys_fail("getpeerucred"); ret = rb_assoc_new(UIDT2NUM(ucred_geteuid(uc)), GIDT2NUM(ucred_getegid(uc))); ucred_free(uc); return ret; #endif }
VALUE setup_group(struct group *grp) { VALUE obj; if (!grp) errno || (errno = 61); // ENODATA eu_errno( setup_safe_str ( "Error setting up Group instance." ) ); obj = rb_obj_alloc(rb_cGroup); rb_ivar_set(obj, id_name, setup_safe_str(grp->gr_name)); rb_ivar_set(obj, id_passwd, setup_safe_str(grp->gr_passwd)); rb_ivar_set(obj, id_gid, GIDT2NUM(grp->gr_gid)); rb_iv_set(obj, "@members", setup_safe_array(grp->gr_mem)); return obj; }
static VALUE setup_group(struct group *grp) { VALUE mem; char **tbl; mem = rb_ary_new(); tbl = grp->gr_mem; while (*tbl) { rb_ary_push(mem, safe_setup_str(*tbl)); tbl++; } return rb_struct_new(sGroup, safe_setup_str(grp->gr_name), #ifdef HAVE_ST_GR_PASSWD safe_setup_str(grp->gr_passwd), #endif GIDT2NUM(grp->gr_gid), mem); }
static VALUE setup_passwd(struct passwd *pwd) { if (pwd == 0) rb_sys_fail("/etc/passwd"); return rb_struct_new(sPasswd, safe_setup_str(pwd->pw_name), #ifdef HAVE_ST_PW_PASSWD safe_setup_str(pwd->pw_passwd), #endif UIDT2NUM(pwd->pw_uid), GIDT2NUM(pwd->pw_gid), #ifdef HAVE_ST_PW_GECOS safe_setup_str(pwd->pw_gecos), #endif safe_setup_str(pwd->pw_dir), safe_setup_str(pwd->pw_shell), #ifdef HAVE_ST_PW_CHANGE INT2NUM(pwd->pw_change), #endif #ifdef HAVE_ST_PW_QUOTA INT2NUM(pwd->pw_quota), #endif #ifdef HAVE_ST_PW_AGE PW_AGE2VAL(pwd->pw_age), #endif #ifdef HAVE_ST_PW_CLASS safe_setup_str(pwd->pw_class), #endif #ifdef HAVE_ST_PW_COMMENT safe_setup_str(pwd->pw_comment), #endif #ifdef HAVE_ST_PW_EXPIRE INT2NUM(pwd->pw_expire), #endif 0 /*dummy*/ ); }