17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 1998-2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/stream.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
34*a154f012SAlexander Stetsenko #include <procfs.h>
35*a154f012SAlexander Stetsenko #include <ucred.h>
36*a154f012SAlexander Stetsenko #include <sys/ucred.h>
377c478bd9Sstevel@tonic-gate #include "libproc.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static int
get_sock_peer_name(struct ps_prochandle * Pr,int syscall,int sock,struct sockaddr * name,socklen_t * namelen)407c478bd9Sstevel@tonic-gate get_sock_peer_name(struct ps_prochandle *Pr,
417c478bd9Sstevel@tonic-gate int syscall, int sock, struct sockaddr *name, socklen_t *namelen)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate sysret_t rval; /* return value from get{sock|peer}name() */
447c478bd9Sstevel@tonic-gate argdes_t argd[4]; /* arg descriptors for get{sock|peer}name() */
457c478bd9Sstevel@tonic-gate argdes_t *adp;
467c478bd9Sstevel@tonic-gate int error;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate adp = &argd[0]; /* sock argument */
497c478bd9Sstevel@tonic-gate adp->arg_value = sock;
507c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
517c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
527c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
537c478bd9Sstevel@tonic-gate adp->arg_size = 0;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate adp++; /* name argument */
567c478bd9Sstevel@tonic-gate adp->arg_value = 0;
577c478bd9Sstevel@tonic-gate adp->arg_object = name;
587c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
597c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT;
607c478bd9Sstevel@tonic-gate adp->arg_size = *namelen;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate adp++; /* namelen argument */
637c478bd9Sstevel@tonic-gate adp->arg_value = 0;
647c478bd9Sstevel@tonic-gate adp->arg_object = namelen;
657c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
667c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INOUT;
677c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (*namelen);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate adp++; /* version argument */
707c478bd9Sstevel@tonic-gate adp->arg_value = SOV_DEFAULT;
717c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
727c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
737c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
747c478bd9Sstevel@tonic-gate adp->arg_size = 0;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate error = Psyscall(Pr, &rval, syscall, 4, &argd[0]);
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate if (error) {
797c478bd9Sstevel@tonic-gate errno = (error > 0)? error : ENOSYS;
807c478bd9Sstevel@tonic-gate return (-1);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate return (0);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* libc system call interface */
867c478bd9Sstevel@tonic-gate extern int _so_getsockname(int, struct sockaddr *, socklen_t *, int);
877c478bd9Sstevel@tonic-gate extern int _so_getpeername(int, struct sockaddr *, socklen_t *, int);
887c478bd9Sstevel@tonic-gate extern int _so_getsockopt(int, int, int, void *, int *);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * getsockname() system call -- executed by subject process
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate int
pr_getsockname(struct ps_prochandle * Pr,int sock,struct sockaddr * name,socklen_t * namelen)947c478bd9Sstevel@tonic-gate pr_getsockname(struct ps_prochandle *Pr,
957c478bd9Sstevel@tonic-gate int sock, struct sockaddr *name, socklen_t *namelen)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate if (Pr == NULL) /* no subject process */
987c478bd9Sstevel@tonic-gate return (_so_getsockname(sock, name, namelen, SOV_DEFAULT));
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate return (get_sock_peer_name(Pr, SYS_getsockname, sock, name, namelen));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * getpeername() system call -- executed by subject process
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate int
pr_getpeername(struct ps_prochandle * Pr,int sock,struct sockaddr * name,socklen_t * namelen)1077c478bd9Sstevel@tonic-gate pr_getpeername(struct ps_prochandle *Pr,
1087c478bd9Sstevel@tonic-gate int sock, struct sockaddr *name, socklen_t *namelen)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate if (Pr == NULL) /* no subject process */
1117c478bd9Sstevel@tonic-gate return (_so_getpeername(sock, name, namelen, SOV_DEFAULT));
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate return (get_sock_peer_name(Pr, SYS_getpeername, sock, name, namelen));
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate int
pr_getsockopt(struct ps_prochandle * Pr,int sock,int level,int optname,void * optval,int * optlen)1177c478bd9Sstevel@tonic-gate pr_getsockopt(struct ps_prochandle *Pr,
1187c478bd9Sstevel@tonic-gate int sock, int level, int optname, void *optval, int *optlen)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate sysret_t rval; /* return value from getsockopt() */
1217c478bd9Sstevel@tonic-gate argdes_t argd[5]; /* arg descriptors for getsockopt() */
1227c478bd9Sstevel@tonic-gate argdes_t *adp;
1237c478bd9Sstevel@tonic-gate int error;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate if (Pr == NULL) /* no subject process */
1267c478bd9Sstevel@tonic-gate return (_so_getsockopt(sock, level, optname, optval, optlen));
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate adp = &argd[0]; /* sock argument */
1297c478bd9Sstevel@tonic-gate adp->arg_value = sock;
1307c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
1317c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
1327c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
1337c478bd9Sstevel@tonic-gate adp->arg_size = 0;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate adp++; /* level argument */
1367c478bd9Sstevel@tonic-gate adp->arg_value = level;
1377c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
1387c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
1397c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
1407c478bd9Sstevel@tonic-gate adp->arg_size = 0;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate adp++; /* optname argument */
1437c478bd9Sstevel@tonic-gate adp->arg_value = optname;
1447c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
1457c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
1467c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
1477c478bd9Sstevel@tonic-gate adp->arg_size = 0;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate adp++; /* optval argument */
1507c478bd9Sstevel@tonic-gate adp->arg_value = 0;
1517c478bd9Sstevel@tonic-gate adp->arg_object = optval;
1527c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
1537c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT;
1547c478bd9Sstevel@tonic-gate adp->arg_size = optlen == NULL ? 0 : *optlen;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate adp++; /* optlen argument */
1577c478bd9Sstevel@tonic-gate adp->arg_value = 0;
1587c478bd9Sstevel@tonic-gate adp->arg_object = optlen;
1597c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
1607c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INOUT;
1617c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (*optlen);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate error = Psyscall(Pr, &rval, SYS_getsockopt, 5, &argd[0]);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (error) {
1667c478bd9Sstevel@tonic-gate errno = (error > 0)? error : ENOSYS;
1677c478bd9Sstevel@tonic-gate return (-1);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate return (0);
1707c478bd9Sstevel@tonic-gate }
171*a154f012SAlexander Stetsenko
172*a154f012SAlexander Stetsenko /*
173*a154f012SAlexander Stetsenko * getpeerucred() system call -- executed by subject process
174*a154f012SAlexander Stetsenko */
175*a154f012SAlexander Stetsenko int
pr_getpeerucred(struct ps_prochandle * Pr,int fd,ucred_t ** ucp)176*a154f012SAlexander Stetsenko pr_getpeerucred(struct ps_prochandle *Pr, int fd, ucred_t **ucp)
177*a154f012SAlexander Stetsenko {
178*a154f012SAlexander Stetsenko sysret_t rval; /* return value from getpeerucred() */
179*a154f012SAlexander Stetsenko argdes_t argd[3]; /* arg descriptors for getpeerucred() */
180*a154f012SAlexander Stetsenko argdes_t *adp;
181*a154f012SAlexander Stetsenko int error;
182*a154f012SAlexander Stetsenko ucred_t *uc = *ucp;
183*a154f012SAlexander Stetsenko
184*a154f012SAlexander Stetsenko if (Pr == NULL) /* no subject process */
185*a154f012SAlexander Stetsenko return (getpeerucred(fd, ucp));
186*a154f012SAlexander Stetsenko
187*a154f012SAlexander Stetsenko if (uc == NULL) {
188*a154f012SAlexander Stetsenko uc = _ucred_alloc();
189*a154f012SAlexander Stetsenko if (uc == NULL)
190*a154f012SAlexander Stetsenko return (-1);
191*a154f012SAlexander Stetsenko }
192*a154f012SAlexander Stetsenko
193*a154f012SAlexander Stetsenko adp = &argd[0]; /* code argument */
194*a154f012SAlexander Stetsenko adp->arg_value = UCREDSYS_GETPEERUCRED;
195*a154f012SAlexander Stetsenko adp->arg_object = NULL;
196*a154f012SAlexander Stetsenko adp->arg_type = AT_BYVAL;
197*a154f012SAlexander Stetsenko adp->arg_inout = AI_INPUT;
198*a154f012SAlexander Stetsenko adp->arg_size = 0;
199*a154f012SAlexander Stetsenko
200*a154f012SAlexander Stetsenko adp++; /* fd argument */
201*a154f012SAlexander Stetsenko adp->arg_value = fd;
202*a154f012SAlexander Stetsenko adp->arg_object = NULL;
203*a154f012SAlexander Stetsenko adp->arg_type = AT_BYVAL;
204*a154f012SAlexander Stetsenko adp->arg_inout = AI_INPUT;
205*a154f012SAlexander Stetsenko adp->arg_size = 0;
206*a154f012SAlexander Stetsenko
207*a154f012SAlexander Stetsenko adp++; /* ucred argument */
208*a154f012SAlexander Stetsenko adp->arg_value = 0;
209*a154f012SAlexander Stetsenko adp->arg_object = uc;
210*a154f012SAlexander Stetsenko adp->arg_type = AT_BYREF;
211*a154f012SAlexander Stetsenko adp->arg_inout = AI_OUTPUT;
212*a154f012SAlexander Stetsenko adp->arg_size = ucred_size();
213*a154f012SAlexander Stetsenko
214*a154f012SAlexander Stetsenko error = Psyscall(Pr, &rval, SYS_ucredsys, 3, &argd[0]);
215*a154f012SAlexander Stetsenko
216*a154f012SAlexander Stetsenko if (error) {
217*a154f012SAlexander Stetsenko errno = (error > 0)? error : ENOSYS;
218*a154f012SAlexander Stetsenko if (*ucp == NULL)
219*a154f012SAlexander Stetsenko ucred_free(uc);
220*a154f012SAlexander Stetsenko
221*a154f012SAlexander Stetsenko return (-1);
222*a154f012SAlexander Stetsenko }
223*a154f012SAlexander Stetsenko *ucp = uc;
224*a154f012SAlexander Stetsenko return (0);
225*a154f012SAlexander Stetsenko }
226