ibcs2_socksys.c revision 164033
1/*- 2 * Copyright (c) 1994, 1995 Scott Bartram 3 * Copyright (c) 1994 Arne H Juul 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. The name of the author may not be used to endorse or promote products 12 * derived from this software without specific prior written permission 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/i386/ibcs2/ibcs2_socksys.c 164033 2006-11-06 13:42:10Z rwatson $"); 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/sysproto.h> 32#include <sys/jail.h> 33#include <sys/kernel.h> 34#include <sys/sysctl.h> 35 36#include <i386/ibcs2/ibcs2_socksys.h> 37#include <i386/ibcs2/ibcs2_util.h> 38 39/* Local structures */ 40struct getipdomainname_args { 41 char *ipdomainname; 42 int len; 43}; 44 45struct setipdomainname_args { 46 char *ipdomainname; 47 int len; 48}; 49 50/* Local prototypes */ 51static int ibcs2_getipdomainname(struct thread *, 52 struct getipdomainname_args *); 53static int ibcs2_setipdomainname(struct thread *, 54 struct setipdomainname_args *); 55 56/* 57 * iBCS2 socksys calls. 58 */ 59 60int 61ibcs2_socksys(td, uap) 62 register struct thread *td; 63 register 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 accept(td, passargs); 85 case SOCKSYS_BIND: 86 return bind(td, passargs); 87 case SOCKSYS_CONNECT: 88 return connect(td, passargs); 89 case SOCKSYS_GETPEERNAME: 90 return getpeername(td, passargs); 91 case SOCKSYS_GETSOCKNAME: 92 return getsockname(td, passargs); 93 case SOCKSYS_GETSOCKOPT: 94 return getsockopt(td, passargs); 95 case SOCKSYS_LISTEN: 96 return listen(td, passargs); 97 case SOCKSYS_RECV: 98 realargs[5] = realargs[6] = 0; 99 /* FALLTHROUGH */ 100 case SOCKSYS_RECVFROM: 101 return recvfrom(td, passargs); 102 case SOCKSYS_SEND: 103 realargs[5] = realargs[6] = 0; 104 /* FALLTHROUGH */ 105 case SOCKSYS_SENDTO: 106 return sendto(td, passargs); 107 case SOCKSYS_SETSOCKOPT: 108 return setsockopt(td, passargs); 109 case SOCKSYS_SHUTDOWN: 110 return shutdown(td, passargs); 111 case SOCKSYS_SOCKET: 112 return socket(td, passargs); 113 case SOCKSYS_SELECT: 114 return 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 adjtime(td, passargs); 121 case SOCKSYS_SETREUID: 122 return setreuid(td, passargs); 123 case SOCKSYS_SETREGID: 124 return setregid(td, passargs); 125 case SOCKSYS_GETTIME: 126 return gettimeofday(td, passargs); 127 case SOCKSYS_SETTIME: 128 return settimeofday(td, passargs); 129 case SOCKSYS_GETITIMER: 130 return getitimer(td, passargs); 131 case SOCKSYS_SETITIMER: 132 return 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(td, uap) 146 struct thread *td; 147 struct getipdomainname_args *uap; 148{ 149 char hname[MAXHOSTNAMELEN], *dptr; 150 int len; 151 152 /* Get the domain name */ 153 getcredhostname(td->td_ucred, hname, sizeof(hname)); 154 155 dptr = index(hname, '.'); 156 if ( dptr ) 157 dptr++; 158 else 159 /* Make it effectively an empty string */ 160 dptr = hname + strlen(hname); 161 162 len = strlen(dptr) + 1; 163 if ((u_int)uap->len > len + 1) 164 uap->len = len + 1; 165 return (copyout((caddr_t)dptr, (caddr_t)uap->ipdomainname, uap->len)); 166} 167 168/* ARGSUSED */ 169static int 170ibcs2_setipdomainname(td, uap) 171 struct thread *td; 172 struct setipdomainname_args *uap; 173{ 174 char hname[MAXHOSTNAMELEN], *ptr; 175 int error, sctl[2], hlen; 176 177 /* W/out a hostname a domain-name is nonsense */ 178 if ( strlen(hostname) == 0 ) 179 return EINVAL; 180 181 /* Get the host's unqualified name (strip off the domain) */ 182 snprintf(hname, sizeof(hname), "%s", hostname); 183 ptr = index(hname, '.'); 184 if ( ptr != NULL ) { 185 ptr++; 186 *ptr = '\0'; 187 } else { 188 if (strlcat(hname, ".", sizeof(hname)) >= sizeof(hname)) 189 return (EINVAL); 190 } 191 192 /* Set ptr to the end of the string so we can append to it */ 193 hlen = strlen(hname); 194 ptr = hname + hlen; 195 if ((u_int)uap->len > (sizeof (hname) - hlen - 1)) 196 return EINVAL; 197 198 /* Append the ipdomain to the end */ 199 error = copyinstr((caddr_t)uap->ipdomainname, ptr, uap->len, NULL); 200 if (error) 201 return (error); 202 203 /* 'sethostname' with the new information */ 204 sctl[0] = CTL_KERN; 205 sctl[1] = KERN_HOSTNAME; 206 hlen = strlen(hname) + 1; 207 return (kernel_sysctl(td, sctl, 2, 0, 0, hname, hlen, 0, 0)); 208} 209