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