From 8808689fe340bec6e90ab13dd502292b0579cf1f Mon Sep 17 00:00:00 2001 From: Michael Krelin Date: Sat, 08 Dec 2012 21:19:17 +0000 Subject: initial osx port Signed-off-by: Michael Krelin --- (limited to 'biportal') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main (int argc, const char * argv[]) { + if(geteuid()) return fprintf(stderr, "unprivileged\n"), printf("%d",EPERM), 1; + if(argc==1) { + struct stat st; + if(stat(*argv, &st)) return fprintf(stderr, "failed to self-stat\n"), printf("%d",errno), 2; + if(st.st_uid!=0 && chown(*argv, 0, 0)) return fprintf(stderr, "failed to self-chown\n"), printf("%d",errno), 3; + if((!(st.st_mode&S_ISUID)) && chmod(*argv, S_ISUID|0555)) return fprintf(stderr, "failed to self-chmod\n"), printf("%d",errno), 4; + return printf("0"),0; + } + if(argc==2 && !strcmp(argv[1],"-k")) { + pid_t p = fork(); + if(p<0) return fprintf(stderr, "failed to fork\n"), printf("%d",errno), 5; + if(!p) { + if(setuid(0)) return fprintf(stderr,"failed to setuid\n"), printf("%d",errno),8; + execl("/bin/launchctl","/bin/launchctl", "remove", "com.apple.tftpd", NULL); + exit(errno); + } + int r, wp; + while((wp=waitpid(p,&r,0))<0 && errno==EINTR); + if(p!=wp) return fprintf(stderr,"failed to wait for launchctl"), 6; + if(!WIFEXITED(r)) return fprintf(stderr,"launchctl failed"), 7; + return fprintf(stderr,"launchctl terminated with %d",WEXITSTATUS(r)), r; + } + if(argc!=4) return fprintf(stderr, "Usage: %s s h p\n",*argv), 10; + struct sockaddr_in sin; memset(&sin,0,sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = inet_addr(argv[2]); + sin.sin_port = htons(strtol(argv[3],NULL,0)); + int rc; + printf("%d", + rc=(bind( (int)strtol(argv[1],NULL,0), (struct sockaddr*)&sin,sizeof(sin) )<0) + ? errno : 0); + return rc; +} + + -- cgit v0.9.0.2