-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv_accept.c
64 lines (54 loc) · 1.08 KB
/
serv_accept.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include"apue.h"
#include<sys/socket.h>
#include<sys/un.h>
#include<time.h>
#include<errno.h>
#define STALE 30
int serv_accept(int listenfd,uid_t *uidptr){
int clifd,err,rval;
socklen_t len;
time_t staletime;
struct sockaddr_un un;
struct stat statbuf;
char *name;
if((name=malloc(sizeof(un.sun_path+1)))==NULL)
return (-1);
len=sizeof(un);
if((clifd=accept(listenfd,(struct sockaddr *)(&un),&len))<0){
free(name);
return (-2);
}
len-=offsetof(struct sockaddr_un,sun_path);
memcpy(name,un.sun_path,len);
name[len]=0;
if(stat(name,&statbuf)<0){
rval=-3;
goto errout;
}
#ifdef S_ISSOCK
if(S_ISSOCK(statbuf.st_mode)==0){
rval=-4;
goto errout;
}
#endif
if((statbuf.st_mode&(S_IRWXG|S_IRWXO))||(statbuf.st_mode&S_IRWXU)!=S_IRWXU){
rval=-5;
goto errout;
}
staletime=time(NULL)-STALE;
if(statbuf.st_atime<staletime||statbuf.st_ctime<staletime||statbuf.st_mtime<staletime){
rval=-6;
goto errout;
}
if(uidptr!=NULL)
*uidptr=statbuf.st_uid;
unlink(name);
free(name);
return(clifd);
errout:
err=errno;
close(clifd);
free(name);
errno=err;
return (rval);
}