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 "unix.h"
#include "conf.h"
static int
sock_perms(char fname[])
{
// first get group by name
struct group *group;
if ((group = getgrnam(SOCKETGROUP)) == NULL) {
fprintf(stderr,"no such group: %s\n",SOCKETGROUP);
return -1;
}
// set group on socket
if (chown(fname, -1, group->gr_gid) == -1) {
perror("chown");
return -1;
}
// get current permissions, so we can modify them.
struct stat sb;
if (stat(fname, &sb) == -1) {
perror("stat");
return -1;
}
if (chmod(fname, sb.st_mode|S_IWGRP) == -1) {
perror("chmod");
return -1;
}
return 0;
}
// takes filename, returns socket fd
// or -1 or whatever
int
unix_setup(char filename[])
{
unlink(filename);
int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock == -1) {
perror("(unix) socket");
return -1;
}
struct sockaddr_un name;
memset(&name, 0, sizeof name);
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename, (sizeof name.sun_path)-1);
if (bind(sock, (struct sockaddr *)&name, sizeof name) == -1) {
perror("(unix) bind");
return -1;
}
if (sock_perms(filename) == -1) {
fprintf(stderr,"warning: couldn't correctly set up permissions on %s\n",filename);
}
return sock;
}
|