4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/ucred.h>
30 #include <sys/errno.h>
31 #include <sys/systm.h>
32 #include <sys/stream.h>
33 #include <sys/strsun.h>
34 #include <sys/stropts.h>
36 #include <sys/vnode.h>
37 #include <sys/cmn_err.h>
38 #include <sys/socket.h>
39 #include <sys/strsubr.h>
43 * Getpeerucred system call implementation.
46 getpeerucred(int fd
, void *buf
)
58 if ((fp
= getf(fd
)) == NULL
)
59 return (set_errno(EBADF
));
66 err
= fop_ioctl(vp
, _I_GETPEERCRED
, (intptr_t)&kpc
,
67 FKIOCTL
, CRED(), &rval
, NULL
);
70 struct strioctl strioc
;
72 if (vp
->v_stream
== NULL
) {
76 strioc
.ic_cmd
= _I_GETPEERCRED
;
77 strioc
.ic_timout
= INFTIM
;
78 strioc
.ic_len
= (int)sizeof (k_peercred_t
);
79 strioc
.ic_dp
= (char *)&kpc
;
81 err
= strdoioctl(vp
->v_stream
, &strioc
, FNATIVE
|FKIOCTL
,
82 STR_NOSIG
|K_TO_K
, CRED(), &rval
);
85 * Map all unexpected error codes to ENOTSUP.
106 * If someone gave us a credential, err will be 0.
108 if (kpc
.pc_cr
!= NULL
) {
111 uc
= cred2ucred(kpc
.pc_cr
, kpc
.pc_cpid
, NULL
, CRED());
115 err
= copyout(uc
, buf
, uc
->uc_size
);
117 kmem_free(uc
, uc
->uc_size
);
120 return (set_errno(EFAULT
));
124 return (set_errno(err
));
128 ucred_get(pid_t pid
, void *ubuf
)
134 uint32_t auditing
= AU_AUDITING();
136 if (pid
== P_MYID
|| pid
== curproc
->p_pid
) {
139 pid
= curproc
->p_pid
;
141 cred_t
*updcred
= NULL
;
144 return (set_errno(EINVAL
));
149 mutex_enter(&pidlock
);
153 mutex_exit(&pidlock
);
156 return (set_errno(ESRCH
));
160 * Assure that audit data in cred is up-to-date.
161 * updcred will be used or freed.
164 audit_update_context(p
, updcred
);
166 err
= priv_proc_cred_perm(CRED(), p
, &pcr
, VREAD
);
167 mutex_exit(&pidlock
);
170 return (set_errno(err
));
173 uc
= cred2ucred(pcr
, pid
, NULL
, CRED());
177 err
= copyout(uc
, ubuf
, uc
->uc_size
);
179 kmem_free(uc
, uc
->uc_size
);
182 return (set_errno(EFAULT
));
188 ucredsys(int code
, int obj
, void *buf
)
191 case UCREDSYS_UCREDGET
:
192 return (ucred_get((pid_t
)obj
, buf
));
193 case UCREDSYS_GETPEERUCRED
:
194 return (getpeerucred(obj
, buf
));
196 return (set_errno(EINVAL
));
200 #ifdef _SYSCALL32_IMPL
202 ucredsys32(int arg1
, int arg2
, caddr32_t arg3
)
204 return (ucredsys(arg1
, arg2
, (void *)(uintptr_t)arg3
));