move all security policy stuff out of userspace
[nestedvm.git] / src / org / ibex / nestedvm / support_aux.c
1 #include <string.h>
2 #include <sys/stat.h>
3 #include <sys/dirent.h>
4 #include <sys/types.h>
5 #include <utime.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <signal.h>
12 #include <sys/sysctl.h>
13 #include <sys/utsname.h>
14 #include <paths.h>
15
16 #include <nestedvm/socket.h>
17
18 int _syscall_set_errno(struct _reent *ptr, int err) {
19     ptr->_errno = -err;
20     return -1;
21 }
22
23 #define REENT_WRAPPER0R(f,rt) \
24     extern rt _##f##_r(struct _reent *ptr); \
25     rt f() { return _##f##_r(_REENT); }
26 #define REENT_WRAPPER0(f) REENT_WRAPPER0R(f,int)
27
28 #define REENT_WRAPPER1R(f,rt,t1) \
29     extern rt _##f##_r(struct _reent *ptr, t1 a); \
30     rt f(t1 a) { return _##f##_r(_REENT,a); }
31 #define REENT_WRAPPER1(f,t1) REENT_WRAPPER1R(f,int,t1)
32
33 #define REENT_WRAPPER2R(f,rt,t1,t2) \
34     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b); \
35     rt f(t1 a, t2 b) { return _##f##_r(_REENT,a,b); }
36 #define REENT_WRAPPER2(f,t1,t2) REENT_WRAPPER2R(f,int,t1,t2)
37
38 #define REENT_WRAPPER3R(f,rt,t1,t2,t3) \
39     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c); \
40     rt f(t1 a, t2 b, t3 c) { return _##f##_r(_REENT,a,b,c); }
41 #define REENT_WRAPPER3(f,t1,t2,t3) REENT_WRAPPER3R(f,int,t1,t2,t3)
42
43 #define REENT_WRAPPER4R(f,rt,t1,t2,t3,t4) \
44 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d); \
45 rt f(t1 a, t2 b, t3 c, t4 d) { return _##f##_r(_REENT,a,b,c,d); }
46 #define REENT_WRAPPER4(f,t1,t2,t3,t4) REENT_WRAPPER4R(f,int,t1,t2,t3,t4)
47
48 #define REENT_WRAPPER5R(f,rt,t1,t2,t3,t4,t5) \
49 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d, t5 e); \
50 rt f(t1 a, t2 b, t3 c, t4 d, t5 e) { return _##f##_r(_REENT,a,b,c,d,e); }
51 #define REENT_WRAPPER5(f,t1,t2,t3,t4,t5) REENT_WRAPPER5R(f,int,t1,t2,t3,t4,t5)
52
53 #define REENT_WRAPPER6R(f,rt,t1,t2,t3,t4,t5,t6) \
54 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d, t5 e, t6 f); \
55 rt f(t1 a, t2 b, t3 c, t4 d, t5 e, t6 f) { return _##f##_r(_REENT,a,b,c,d,e,f); }
56 #define REENT_WRAPPER6(f,t1,t2,t3,t4,t5,t6) REENT_WRAPPER6R(f,int,t1,t2,t3,t4,t5,t6)
57
58 REENT_WRAPPER2(mkdir,const char *,mode_t)
59 REENT_WRAPPER2(access,const char *,int)
60 REENT_WRAPPER1(rmdir,const char *)
61 REENT_WRAPPER1R(sysconf,long,int)
62 REENT_WRAPPER1(chdir,const char*)
63 REENT_WRAPPER2(utime,const char *,const struct utimbuf *)
64 REENT_WRAPPER1(pipe,int *)
65 REENT_WRAPPER2(dup2,int,int)
66 REENT_WRAPPER3(waitpid,pid_t,int *,int)
67 REENT_WRAPPER2R(getcwd,char *,char *,size_t)
68 REENT_WRAPPER2R(_getcwd,char *,char *,size_t)
69 REENT_WRAPPER2(symlink,const char *,const char *)
70 REENT_WRAPPER3(readlink,const char *, char *,int)
71 REENT_WRAPPER3(chown,const char *,uid_t,gid_t)
72 REENT_WRAPPER3(fchown,int,uid_t,gid_t)
73 REENT_WRAPPER3(lchown,const char *,uid_t,gid_t)
74 REENT_WRAPPER2(chmod,const char *,mode_t)
75 REENT_WRAPPER2(fchmod,int,mode_t)
76 REENT_WRAPPER2(lstat,const char *,struct stat *)
77 REENT_WRAPPER4(getdents,int, char *, size_t,long *)
78 REENT_WRAPPER1(dup,int)
79 REENT_WRAPPER2R(pathconf,long,const char *,int)
80 REENT_WRAPPER0(vfork)
81 REENT_WRAPPER1(chroot,const char *)
82 REENT_WRAPPER3(mknod,const char *,mode_t,dev_t)
83 REENT_WRAPPER2(ftruncate,int,off_t)
84 REENT_WRAPPER1(usleep,unsigned int)
85 REENT_WRAPPER2(mkfifo,const char *, mode_t)
86 REENT_WRAPPER3(klogctl,int,char*,int)
87 REENT_WRAPPER2R(realpath,char *,const char *,char *)
88 REENT_WRAPPER6(_sysctl,int *,int, void *, size_t*, void *, size_t)
89 REENT_WRAPPER6(sysctl,int *, int, void *, size_t*, void *, size_t)
90 REENT_WRAPPER2(getpriority,int,int)
91 REENT_WRAPPER3(setpriority,int,int,int)
92 REENT_WRAPPER3(connect,int,const struct sockaddr *,socklen_t)
93 REENT_WRAPPER3(socket,int,int,int)
94 REENT_WRAPPER3(_resolve_hostname,const char *,char*,size_t*)
95 REENT_WRAPPER3(_resolve_ip,int,char*,size_t)
96 REENT_WRAPPER3(accept,int,struct sockaddr *,socklen_t*)
97 REENT_WRAPPER5(getsockopt,int,int,int,void*,socklen_t*)
98 REENT_WRAPPER5(setsockopt,int,int,int,const void*,socklen_t)
99 REENT_WRAPPER3(bind,int,const struct sockaddr *,socklen_t)
100 REENT_WRAPPER2(listen,int,int)
101 REENT_WRAPPER2(shutdown,int,int)
102 REENT_WRAPPER6(sendto,int,const void*,size_t,int,const struct sockaddr*,socklen_t)
103 REENT_WRAPPER6(recvfrom,int,void*,size_t,int,struct sockaddr*,socklen_t*)
104 REENT_WRAPPER5(select,int,fd_set*,fd_set*,fd_set*,struct timeval*)
105 REENT_WRAPPER4(send,int,const void*,size_t,int)
106 REENT_WRAPPER4(recv,int,void*,size_t,int)
107 REENT_WRAPPER2(getgroups,int,gid_t*)
108 REENT_WRAPPER3(getsockname,int,struct sockaddr*,int*)
109 REENT_WRAPPER3(getpeername,int,struct sockaddr*,int*)
110 REENT_WRAPPER1(setuid,uid_t)
111 REENT_WRAPPER1(seteuid,uid_t)
112 REENT_WRAPPER1(setgid,gid_t)
113 REENT_WRAPPER1(setegid,gid_t)
114 REENT_WRAPPER2(setgroups,int,const gid_t *)
115 REENT_WRAPPER0R(setsid,pid_t)
116
117 extern int __execve_r(struct _reent *ptr, const char *path, char *const argv[], char *const envp[]);
118 int _execve(const char *path, char *const argv[], char *const envp[]) {
119     return __execve_r(_REENT,path,argv,envp);
120 }
121
122 char *_getcwd_r(struct _reent *ptr, char *buf, size_t size) {
123     if(buf != NULL) {
124         buf = __getcwd_r(ptr,buf,size);
125         return (long)buf == -1 ? NULL : buf;
126     }
127     
128     size = 256;
129     for(;;) {
130         buf = malloc(size);
131         char *ret = __getcwd_r(ptr,buf,size);
132         if((long)ret != -1) return ret;
133         free(buf);
134         size *= 2;
135         if(ptr->_errno != ERANGE) return NULL;
136     }
137 }
138
139 pid_t _wait_r(struct _reent *ptr, int *status) {
140     return _waitpid_r(ptr,-1,status,0);
141 }
142
143 long _pathconf_r(struct _reent *ptr,const char *path, int name) {
144     switch(name) {
145         default:
146             fprintf(stderr,"WARNING: pathconf: Unknown \"name\": %d\n",name);
147             ptr->_errno = EINVAL;
148             return -1;
149     }
150 }
151
152 int _sysctl_r(struct _reent *ptr, int *name, int namelen, void *oldp, size_t *oldlen, void *newp, size_t newlen) {
153     if(name[0] != CTL_USER) return _sysctl(name,namelen,oldp,oldlen,newp,newlen);
154     if(newp != NULL) { ptr->_errno = EPERM; return -1; }
155     if(namelen != 2) { ptr->_errno = EINVAL; return -1; }
156     
157     switch(name[1]) {
158         default:
159             fprintf(stderr,"WARNING: sysctl: Unknown name: %d\n",name[1]);
160             ptr->_errno = EINVAL;
161             return -1;
162     }
163 }
164
165 void sync() {
166     /* do nothing*/
167 }
168
169 int fsync(int fd) {
170     /* do nothing */
171     return 0;
172 }
173
174 char *ttyname(int fd) {
175     return isatty(fd) ? "/dev/console" : NULL;
176 }
177
178 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
179     _sig_func_ptr old;
180     _sig_func_ptr new;
181     if(act) {
182         if(act->sa_flags || act->sa_mask != ~((sigset_t)0)) { errno = EINVAL; return -1; }
183         old = signal(sig,act->sa_handler);
184     } else if(oact) {
185         old = signal(sig,SIG_DFL);
186         signal(sig,old);
187     }
188     if(oact) {
189         oact->sa_handler = old;
190         oact->sa_mask = 0;
191         oact->sa_mask = ~((sigset_t)0);
192     }
193     return 0;
194 }
195
196 int sigfillset(sigset_t *set) {
197     *set = ~((sigset_t)0);
198     return 0;
199 }
200
201 int sigemptyset(sigset_t *set) {
202     *set = (sigset_t) 0;
203     return 0;
204 }
205
206 DIR *opendir(const char *path) {
207     struct stat sb;
208     int fd;
209     DIR *dir;
210     
211     fd = open(path,O_RDONLY);
212     if(fd < 0) return NULL;
213     
214     if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
215         close(fd);
216         errno = ENOTDIR;
217         return NULL;
218     }
219     
220     dir = malloc(sizeof(*dir));
221     if(dir == NULL) {
222         close(fd);
223         errno = ENOMEM;
224         return NULL;
225     }
226     dir->dd_fd = fd;
227     dir->dd_buf = malloc(sizeof(struct dirent));
228     dir->dd_size = sizeof(struct dirent);
229     if(dir->dd_buf == NULL) {
230         close(fd);
231         free(dir);
232         return NULL;
233     }
234     dir->dd_loc = 0;
235     dir->dd_len = 0;
236     return dir;
237 }
238
239 struct dirent *readdir(DIR *dir) {
240     struct dirent *dp;
241     errno = 0;
242     if(dir->dd_loc == 0 || dir->dd_loc == dir->dd_len) {
243         dir->dd_len = getdents(dir->dd_fd,dir->dd_buf,dir->dd_size,NULL);
244         dir->dd_loc = 0;
245         if(dir->dd_len <= 0) { dir->dd_len = 0; return NULL; }
246     }
247     dp = (struct dirent*) (dir->dd_buf + dir->dd_loc);
248     if(dp->d_reclen == 0 || dp->d_reclen > dir->dd_len - dir->dd_loc) return NULL;
249     dir->dd_loc += dp->d_reclen;
250     return dp;
251 }
252
253 int closedir(DIR *dir) {
254     int fd = dir->dd_fd;
255     free(dir->dd_buf);
256     free(dir);
257     return close(fd);
258 }
259
260 /*
261  * Networking/Socket stuff
262  */
263
264 /* This should really be part of the newlib _reent structure */
265 int h_errno;
266
267 char *inet_ntoa(struct in_addr in) {
268     static char buf[18];
269     const unsigned char *p = (void*) &in;
270     snprintf(buf,sizeof(buf),"%u.%u.%u.%u",p[0],p[1],p[2],p[3]);
271     return buf;
272 }
273
274 struct servent *getservbyname(const char *name,const char *proto) {
275     return NULL;
276 }
277
278 static const char *h_errlist[] = { "No Error","Unknown host", "Host name lookup failure","Unknown server error","No address associated with name" };
279
280 const char *hstrerror(int err) {
281     if(err < 0 || err > 4) return "Unknown Error";
282     return h_errlist[err];
283 }
284
285 void herror(const char *string) {
286     fprintf(stderr,"%s: %s\n",string,hstrerror(h_errno));
287 }
288
289 extern int _resolve_hostname(const char *, char *buf, size_t *size);
290
291 struct hostent *gethostbyname(const char *hostname) {
292 #define MAX_ADDRS 256
293     static struct hostent hostent;
294     static char saved_hostname[128];
295     static char *addr_list[MAX_ADDRS+1];
296     static char addr_list_buf[MAX_ADDRS*sizeof(struct in_addr)];
297     static char *aliases[1];
298     
299     unsigned char buf[MAX_ADDRS*sizeof(struct in_addr)];
300     size_t size = sizeof(buf);
301     int err,i,n=0;
302     
303     err = _resolve_hostname(hostname,buf,&size);
304     if(err != 0) { h_errno = err; return NULL; }
305     
306     memcpy(addr_list_buf,buf,size);
307     for(i=0;i<size;i += sizeof(struct in_addr)) addr_list[n++] = &addr_list_buf[i];
308     addr_list[n] = NULL;
309     strncpy(saved_hostname,hostname,sizeof(saved_hostname));
310     aliases[0] = NULL;
311     
312     hostent.h_name = saved_hostname;
313     hostent.h_aliases = aliases;
314     hostent.h_addrtype = AF_INET;
315     hostent.h_length = sizeof(struct in_addr);
316     hostent.h_addr_list = addr_list;
317     
318     return &hostent;
319 }
320
321 /*
322  * Other People's Code 
323  */
324
325 /* FreeBSD's dirname/basename */
326
327 /* FIXME: Put these in a header */
328
329 /*
330  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
331  * All rights reserved.
332  *
333  * Redistribution and use in source and binary forms, with or without
334  * modification, are permitted provided that the following conditions
335  * are met:
336  * 1. Redistributions of source code must retain the above copyright
337  *    notice, this list of conditions and the following disclaimer.
338  * 2. Redistributions in binary form must reproduce the above copyright
339  *    notice, this list of conditions and the following disclaimer in the
340  *    documentation and/or other materials provided with the distribution.
341  * 3. The name of the author may not be used to endorse or promote products
342  *    derived from this software without specific prior written permission.
343  *
344  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
345  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
346  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
347  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
348  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
349  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
350  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
351  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
352  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
353  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
354  */
355
356 char *
357 dirname(path)
358 const char *path;
359 {
360     static char bname[MAXPATHLEN];
361     register const char *endp;
362     
363     /* Empty or NULL string gets treated as "." */
364     if (path == NULL || *path == '\0') {
365         (void)strcpy(bname, ".");
366         return(bname);
367     }
368     
369     /* Strip trailing slashes */
370     endp = path + strlen(path) - 1;
371     while (endp > path && *endp == '/')
372         endp--;
373     
374     /* Find the start of the dir */
375     while (endp > path && *endp != '/')
376         endp--;
377     
378     /* Either the dir is "/" or there are no slashes */
379     if (endp == path) {
380         (void)strcpy(bname, *endp == '/' ? "/" : ".");
381         return(bname);
382     } else {
383         do {
384             endp--;
385         } while (endp > path && *endp == '/');
386     }
387     
388     if (endp - path + 2 > sizeof(bname)) {
389         errno = ENAMETOOLONG;
390         return(NULL);
391     }
392     (void)strncpy(bname, path, endp - path + 1);
393     bname[endp - path + 1] = '\0';
394     return(bname);
395 }
396
397 char *
398 basename(path)
399 const char *path;
400 {
401     static char bname[MAXPATHLEN];
402     register const char *endp, *startp;
403     
404     /* Empty or NULL string gets treated as "." */
405     if (path == NULL || *path == '\0') {
406         (void)strcpy(bname, ".");
407         return(bname);
408     }
409     
410     /* Strip trailing slashes */
411     endp = path + strlen(path) - 1;
412     while (endp > path && *endp == '/')
413         endp--;
414     
415     /* All slashes becomes "/" */
416     if (endp == path && *endp == '/') {
417         (void)strcpy(bname, "/");
418         return(bname);
419     }
420     
421     /* Find the start of the base */
422     startp = endp;
423     while (startp > path && *(startp - 1) != '/')
424         startp--;
425     
426     if (endp - startp + 2 > sizeof(bname)) {
427         errno = ENAMETOOLONG;
428         return(NULL);
429     }
430     (void)strncpy(bname, startp, endp - startp + 1);
431     bname[endp - startp + 1] = '\0';
432     return(bname);
433 }
434
435 /* FreeBSD's uname */
436 int
437 uname(name)
438 struct utsname *name;
439 {
440         int mib[2], rval;
441         size_t len;
442         char *p;
443         int oerrno;
444     
445         rval = 0;
446     
447         mib[0] = CTL_KERN;
448         mib[1] = KERN_OSTYPE;
449         len = sizeof(name->sysname);
450         oerrno = errno;
451         if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) {
452                 if(errno == ENOMEM)
453                         errno = oerrno;
454                 else
455                         rval = -1;
456         }
457         name->sysname[sizeof(name->sysname) - 1] = '\0';
458     
459         mib[0] = CTL_KERN;
460         mib[1] = KERN_HOSTNAME;
461         len = sizeof(name->nodename);
462         oerrno = errno;
463         if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) {
464                 if(errno == ENOMEM)
465                         errno = oerrno;
466                 else
467                         rval = -1;
468         }
469         name->nodename[sizeof(name->nodename) - 1] = '\0';
470     
471         mib[0] = CTL_KERN;
472         mib[1] = KERN_OSRELEASE;
473         len = sizeof(name->release);
474         oerrno = errno;
475         if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) {
476                 if(errno == ENOMEM)
477                         errno = oerrno;
478                 else
479                         rval = -1;
480         }
481         name->release[sizeof(name->release) - 1] = '\0';
482     
483         /* The version may have newlines in it, turn them into spaces. */
484         mib[0] = CTL_KERN;
485         mib[1] = KERN_VERSION;
486         len = sizeof(name->version);
487         oerrno = errno;
488         if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) {
489                 if (errno == ENOMEM)
490                         errno = oerrno;
491                 else
492                         rval = -1;
493         }
494         name->version[sizeof(name->version) - 1] = '\0';
495         for (p = name->version; len--; ++p) {
496                 if (*p == '\n' || *p == '\t') {
497                         if (len > 1)
498                                 *p = ' ';
499                         else
500                                 *p = '\0';
501                 }
502         }
503     
504         mib[0] = CTL_HW;
505         mib[1] = HW_MACHINE;
506         len = sizeof(name->machine);
507         oerrno = errno;
508         if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) {
509                 if (errno == ENOMEM)
510                         errno = oerrno;
511                 else
512                         rval = -1;
513         }
514         name->machine[sizeof(name->machine) - 1] = '\0';
515         return (rval);
516 }
517
518 /* FreeBSD's daemon() - modified for nestedvm */
519 int
520 daemon(nochdir, noclose)
521 int nochdir, noclose;
522 {
523         int fd;
524     
525         switch (fork()) {
526         case -1:
527             return (-1);
528         case 0:
529             break;
530         default:
531             _exit(0);
532         }
533     
534         if (setsid() == -1)
535                 return (-1);
536     
537         if (!nochdir)
538                 (void)chdir("/");
539     
540         if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
541                 (void)dup2(fd, STDIN_FILENO);
542                 (void)dup2(fd, STDOUT_FILENO);
543                 (void)dup2(fd, STDERR_FILENO);
544                 if (fd > 2)
545                         (void)close(fd);
546         }
547         return (0);
548 }