ibcs2_socksys.c revision 331643
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1994, 1995 Scott Bartram
5 * Copyright (c) 1994 Arne H Juul
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/11/sys/i386/ibcs2/ibcs2_socksys.c 331643 2018-03-27 18:52:27Z dim $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/sysproto.h>
34#include <sys/jail.h>
35#include <sys/kernel.h>
36#include <sys/sysctl.h>
37
38#include <i386/ibcs2/ibcs2_socksys.h>
39#include <i386/ibcs2/ibcs2_util.h>
40
41/* Local structures */
42struct getipdomainname_args {
43        char    *ipdomainname;
44        int     len;
45};
46
47struct setipdomainname_args {
48        char    *ipdomainname;
49        int     len;
50};
51
52/* Local prototypes */
53static int ibcs2_getipdomainname(struct thread *,
54				      struct getipdomainname_args *);
55static int ibcs2_setipdomainname(struct thread *,
56				      struct setipdomainname_args *);
57
58/*
59 * iBCS2 socksys calls.
60 */
61
62int
63ibcs2_socksys(struct thread *td, struct ibcs2_socksys_args *uap)
64{
65	int error;
66	int realargs[7]; /* 1 for command, 6 for recvfrom */
67	void *passargs;
68
69	/*
70	 * SOCKET should only be legal on /dev/socksys.
71	 * GETIPDOMAINNAME should only be legal on /dev/socksys ?
72	 * The others are (and should be) only legal on sockets.
73	 */
74
75	if ((error = copyin(uap->argsp, (caddr_t)realargs, sizeof(realargs))) != 0)
76		return error;
77	DPRINTF(("ibcs2_socksys: %08x %08x %08x %08x %08x %08x %08x\n",
78	       realargs[0], realargs[1], realargs[2], realargs[3],
79	       realargs[4], realargs[5], realargs[6]));
80
81	passargs = (void *)(realargs + 1);
82	switch (realargs[0]) {
83	case SOCKSYS_ACCEPT:
84		return sys_accept(td, passargs);
85	case SOCKSYS_BIND:
86		return sys_bind(td, passargs);
87	case SOCKSYS_CONNECT:
88		return sys_connect(td, passargs);
89	case SOCKSYS_GETPEERNAME:
90		return sys_getpeername(td, passargs);
91	case SOCKSYS_GETSOCKNAME:
92		return sys_getsockname(td, passargs);
93	case SOCKSYS_GETSOCKOPT:
94		return sys_getsockopt(td, passargs);
95	case SOCKSYS_LISTEN:
96		return sys_listen(td, passargs);
97	case SOCKSYS_RECV:
98		realargs[5] = realargs[6] = 0;
99		/* FALLTHROUGH */
100	case SOCKSYS_RECVFROM:
101		return sys_recvfrom(td, passargs);
102	case SOCKSYS_SEND:
103		realargs[5] = realargs[6] = 0;
104		/* FALLTHROUGH */
105	case SOCKSYS_SENDTO:
106		return sys_sendto(td, passargs);
107	case SOCKSYS_SETSOCKOPT:
108		return sys_setsockopt(td, passargs);
109	case SOCKSYS_SHUTDOWN:
110		return sys_shutdown(td, passargs);
111	case SOCKSYS_SOCKET:
112		return sys_socket(td, passargs);
113	case SOCKSYS_SELECT:
114		return sys_select(td, passargs);
115	case SOCKSYS_GETIPDOMAIN:
116		return ibcs2_getipdomainname(td, passargs);
117	case SOCKSYS_SETIPDOMAIN:
118		return ibcs2_setipdomainname(td, passargs);
119	case SOCKSYS_ADJTIME:
120		return sys_adjtime(td, passargs);
121	case SOCKSYS_SETREUID:
122		return sys_setreuid(td, passargs);
123	case SOCKSYS_SETREGID:
124		return sys_setregid(td, passargs);
125	case SOCKSYS_GETTIME:
126		return sys_gettimeofday(td, passargs);
127	case SOCKSYS_SETTIME:
128		return sys_settimeofday(td, passargs);
129	case SOCKSYS_GETITIMER:
130		return sys_getitimer(td, passargs);
131	case SOCKSYS_SETITIMER:
132		return sys_setitimer(td, passargs);
133
134	default:
135		printf("socksys unknown %08x %08x %08x %08x %08x %08x %08x\n",
136                       realargs[0], realargs[1], realargs[2], realargs[3],
137                       realargs[4], realargs[5], realargs[6]);
138		return EINVAL;
139	}
140	/* NOTREACHED */
141}
142
143/* ARGSUSED */
144static int
145ibcs2_getipdomainname(struct thread *td, struct getipdomainname_args *uap)
146{
147	char hname[MAXHOSTNAMELEN], *dptr;
148	int len;
149
150	/* Get the domain name. */
151	getcredhostname(td->td_ucred, hname, sizeof(hname));
152
153	dptr = strchr(hname, '.');
154	if ( dptr )
155		dptr++;
156	else
157		/* Make it effectively an empty string */
158		dptr = hname + strlen(hname);
159
160	len = strlen(dptr) + 1;
161	if ((u_int)uap->len > len + 1)
162		uap->len = len + 1;
163	return (copyout((caddr_t)dptr, (caddr_t)uap->ipdomainname, uap->len));
164}
165
166/* ARGSUSED */
167static int
168ibcs2_setipdomainname(struct thread *td, struct setipdomainname_args *uap)
169{
170	char hname[MAXHOSTNAMELEN], *ptr;
171	int error, sctl[2], hlen;
172
173	/* Get the domain name */
174	getcredhostname(td->td_ucred, hname, sizeof(hname));
175
176	/* W/out a hostname a domain-name is nonsense */
177	if ( strlen(hname) == 0 )
178		return EINVAL;
179
180	/* Get the host's unqualified name (strip off the domain) */
181	ptr = strchr(hname, '.');
182	if ( ptr != NULL ) {
183		ptr++;
184		*ptr = '\0';
185	} else {
186		if (strlcat(hname, ".", sizeof(hname)) >= sizeof(hname))
187			return (EINVAL);
188	}
189
190	/* Set ptr to the end of the string so we can append to it */
191	hlen = strlen(hname);
192	ptr = hname + hlen;
193        if ((u_int)uap->len > (sizeof (hname) - hlen - 1))
194                return EINVAL;
195
196	/* Append the ipdomain to the end */
197	error = copyinstr((caddr_t)uap->ipdomainname, ptr, uap->len, NULL);
198	if (error)
199		return (error);
200
201	/* 'sethostname' with the new information */
202	sctl[0] = CTL_KERN;
203        sctl[1] = KERN_HOSTNAME;
204 	hlen = strlen(hname) + 1;
205        return (kernel_sysctl(td, sctl, 2, 0, 0, hname, hlen, 0, 0));
206}
207