summaryrefslogtreecommitdiffabout
path: root/biportal/main.c
Unidiff
Diffstat (limited to 'biportal/main.c') (more/less context) (ignore whitespace changes)
-rw-r--r--biportal/main.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/biportal/main.c b/biportal/main.c
new file mode 100644
index 0000000..57085d7
--- a/dev/null
+++ b/biportal/main.c
@@ -0,0 +1,46 @@
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <arpa/inet.h>
8#include <errno.h>
9#include <sys/stat.h>
10
11int main (int argc, const char * argv[]) {
12 if(geteuid()) return fprintf(stderr, "unprivileged\n"), printf("%d",EPERM), 1;
13 if(argc==1) {
14 struct stat st;
15 if(stat(*argv, &st)) return fprintf(stderr, "failed to self-stat\n"), printf("%d",errno), 2;
16 if(st.st_uid!=0 && chown(*argv, 0, 0)) return fprintf(stderr, "failed to self-chown\n"), printf("%d",errno), 3;
17 if((!(st.st_mode&S_ISUID)) && chmod(*argv, S_ISUID|0555)) return fprintf(stderr, "failed to self-chmod\n"), printf("%d",errno), 4;
18 return printf("0"),0;
19 }
20 if(argc==2 && !strcmp(argv[1],"-k")) {
21 pid_t p = fork();
22 if(p<0) return fprintf(stderr, "failed to fork\n"), printf("%d",errno), 5;
23 if(!p) {
24 if(setuid(0)) return fprintf(stderr,"failed to setuid\n"), printf("%d",errno),8;
25 execl("/bin/launchctl","/bin/launchctl", "remove", "com.apple.tftpd", NULL);
26 exit(errno);
27 }
28 int r, wp;
29 while((wp=waitpid(p,&r,0))<0 && errno==EINTR);
30 if(p!=wp) return fprintf(stderr,"failed to wait for launchctl"), 6;
31 if(!WIFEXITED(r)) return fprintf(stderr,"launchctl failed"), 7;
32 return fprintf(stderr,"launchctl terminated with %d",WEXITSTATUS(r)), r;
33 }
34 if(argc!=4) return fprintf(stderr, "Usage: %s s h p\n",*argv), 10;
35 struct sockaddr_in sin; memset(&sin,0,sizeof(sin));
36 sin.sin_family = AF_INET;
37 sin.sin_addr.s_addr = inet_addr(argv[2]);
38 sin.sin_port = htons(strtol(argv[3],NULL,0));
39 int rc;
40 printf("%d",
41 rc=(bind( (int)strtol(argv[1],NULL,0), (struct sockaddr*)&sin,sizeof(sin) )<0)
42 ? errno : 0);
43 return rc;
44}
45
46