]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/ibcs2/ibcs2_socksys.c
MFV "xmalloc: zero size" fix.
[FreeBSD/FreeBSD.git] / sys / i386 / ibcs2 / ibcs2_socksys.c
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$");
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/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/vimage.h>
38
39 #include <i386/ibcs2/ibcs2_socksys.h>
40 #include <i386/ibcs2/ibcs2_util.h>
41
42 /* Local structures */
43 struct getipdomainname_args {
44         char    *ipdomainname;
45         int     len;
46 };
47
48 struct setipdomainname_args {
49         char    *ipdomainname;
50         int     len;
51 };
52
53 /* Local prototypes */
54 static int ibcs2_getipdomainname(struct thread *,
55                                       struct getipdomainname_args *);
56 static int ibcs2_setipdomainname(struct thread *,
57                                       struct setipdomainname_args *);
58
59 /*
60  * iBCS2 socksys calls.
61  */
62
63 int
64 ibcs2_socksys(td, uap)
65         register struct thread *td;
66         register struct ibcs2_socksys_args *uap;
67 {
68         int error;
69         int realargs[7]; /* 1 for command, 6 for recvfrom */
70         void *passargs;
71
72         /*
73          * SOCKET should only be legal on /dev/socksys.
74          * GETIPDOMAINNAME should only be legal on /dev/socksys ?
75          * The others are (and should be) only legal on sockets.
76          */
77
78         if ((error = copyin(uap->argsp, (caddr_t)realargs, sizeof(realargs))) != 0)
79                 return error;
80         DPRINTF(("ibcs2_socksys: %08x %08x %08x %08x %08x %08x %08x\n",
81                realargs[0], realargs[1], realargs[2], realargs[3], 
82                realargs[4], realargs[5], realargs[6]));
83
84         passargs = (void *)(realargs + 1);
85         switch (realargs[0]) {
86         case SOCKSYS_ACCEPT:
87                 return accept(td, passargs);
88         case SOCKSYS_BIND:
89                 return bind(td, passargs);
90         case SOCKSYS_CONNECT:
91                 return connect(td, passargs);
92         case SOCKSYS_GETPEERNAME:
93                 return getpeername(td, passargs);
94         case SOCKSYS_GETSOCKNAME:
95                 return getsockname(td, passargs);
96         case SOCKSYS_GETSOCKOPT:
97                 return getsockopt(td, passargs);
98         case SOCKSYS_LISTEN:
99                 return listen(td, passargs);
100         case SOCKSYS_RECV:
101                 realargs[5] = realargs[6] = 0;
102                 /* FALLTHROUGH */
103         case SOCKSYS_RECVFROM:
104                 return recvfrom(td, passargs);
105         case SOCKSYS_SEND:
106                 realargs[5] = realargs[6] = 0;
107                 /* FALLTHROUGH */
108         case SOCKSYS_SENDTO:
109                 return sendto(td, passargs);
110         case SOCKSYS_SETSOCKOPT:
111                 return setsockopt(td, passargs);
112         case SOCKSYS_SHUTDOWN:
113                 return shutdown(td, passargs);
114         case SOCKSYS_SOCKET:
115                 return socket(td, passargs);
116         case SOCKSYS_SELECT:
117                 return select(td, passargs);
118         case SOCKSYS_GETIPDOMAIN:
119                 return ibcs2_getipdomainname(td, passargs);
120         case SOCKSYS_SETIPDOMAIN:
121                 return ibcs2_setipdomainname(td, passargs);
122         case SOCKSYS_ADJTIME:
123                 return adjtime(td, passargs);
124         case SOCKSYS_SETREUID:
125                 return setreuid(td, passargs);
126         case SOCKSYS_SETREGID:
127                 return setregid(td, passargs);
128         case SOCKSYS_GETTIME:
129                 return gettimeofday(td, passargs);
130         case SOCKSYS_SETTIME:
131                 return settimeofday(td, passargs);
132         case SOCKSYS_GETITIMER:
133                 return getitimer(td, passargs);
134         case SOCKSYS_SETITIMER:
135                 return setitimer(td, passargs);
136
137         default:
138                 printf("socksys unknown %08x %08x %08x %08x %08x %08x %08x\n",
139                        realargs[0], realargs[1], realargs[2], realargs[3], 
140                        realargs[4], realargs[5], realargs[6]);
141                 return EINVAL;
142         }
143         /* NOTREACHED */
144 }
145
146 /* ARGSUSED */
147 static int
148 ibcs2_getipdomainname(td, uap)
149         struct thread *td;
150         struct getipdomainname_args *uap;
151 {
152         char hname[MAXHOSTNAMELEN], *dptr;
153         int len;
154
155         /* Get the domain name */
156         getcredhostname(td->td_ucred, hname, sizeof(hname));
157
158         dptr = index(hname, '.');
159         if ( dptr )
160                 dptr++;
161         else
162                 /* Make it effectively an empty string */
163                 dptr = hname + strlen(hname);
164         
165         len = strlen(dptr) + 1;
166         if ((u_int)uap->len > len + 1)
167                 uap->len = len + 1;
168         return (copyout((caddr_t)dptr, (caddr_t)uap->ipdomainname, uap->len));
169 }
170
171 /* ARGSUSED */
172 static int
173 ibcs2_setipdomainname(td, uap)
174         struct thread *td;
175         struct setipdomainname_args *uap;
176 {
177         char hname[MAXHOSTNAMELEN], *ptr;
178         int error, sctl[2], hlen;
179
180         /* W/out a hostname a domain-name is nonsense */
181         mtx_lock(&hostname_mtx);
182         if ( strlen(V_hostname) == 0 ) {
183                 mtx_unlock(&hostname_mtx);
184                 return EINVAL;
185         }
186
187         /* Get the host's unqualified name (strip off the domain) */
188         snprintf(hname, sizeof(hname), "%s", V_hostname);
189         mtx_unlock(&hostname_mtx);
190         ptr = index(hname, '.');
191         if ( ptr != NULL ) {
192                 ptr++;
193                 *ptr = '\0';
194         } else {
195                 if (strlcat(hname, ".", sizeof(hname)) >= sizeof(hname))
196                         return (EINVAL);
197         }
198
199         /* Set ptr to the end of the string so we can append to it */
200         hlen = strlen(hname);
201         ptr = hname + hlen;
202         if ((u_int)uap->len > (sizeof (hname) - hlen - 1))
203                 return EINVAL;
204
205         /* Append the ipdomain to the end */
206         error = copyinstr((caddr_t)uap->ipdomainname, ptr, uap->len, NULL);
207         if (error)
208                 return (error);
209
210         /* 'sethostname' with the new information */
211         sctl[0] = CTL_KERN;
212         sctl[1] = KERN_HOSTNAME;
213         hlen = strlen(hname) + 1;
214         return (kernel_sysctl(td, sctl, 2, 0, 0, hname, hlen, 0, 0));
215 }