aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-10-28 02:06:01 +0100
committerubq323 <ubq323@ubq323.website>2022-10-28 02:06:01 +0100
commitfa433836c643bbd9a60ed8ccdc9451a1edd98418 (patch)
tree30054b508d903007b5545329197aa7dcb18e311f
parentf56709bfcdad99a47d52196ba971ff1d41746289 (diff)
handling of unix domain sockets
-rw-r--r--Makefile2
-rw-r--r--unix.c64
-rw-r--r--unix.h17
3 files changed, 82 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 2269f1c..9d57e9e 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ CC=cc
debug=-g -DDEBUG -DLOCAL
CFLAGS=$(MORE_CFLAGS) -Wall
-OBJS=bee.o irc.o util.o
+OBJS=bee.o irc.o util.o unix.o
all: apiobot
apiobot: $(OBJS)
diff --git a/unix.c b/unix.c
new file mode 100644
index 0000000..bbb663d
--- /dev/null
+++ b/unix.c
@@ -0,0 +1,64 @@
+#include "unix.h"
+
+#include "conf.h"
+
+static int
+sock_perms(int fd)
+{
+ // 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 (fchown(fd, -1, group.gr_gid) == -1) {
+ perror("chown");
+ return -1;
+ }
+
+ // get current permissions, so we can modify them.
+ struct stat sb;
+ if (fstat(fd, &sb) == -1) {
+ perror("stat");
+ return -1;
+ }
+ if (fchmod(fd, 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 sockname[])
+{
+ unlink(sockname);
+
+ 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, SOCKETNAME, (sizeof name.sun_path)-1);
+ if (bind(sock, (struct sockaddr *)&name, sizeof name) == -1) {
+ perror("(unix) bind");
+ return -1;
+ }
+
+ if (sock_perms(sock) == -1) {
+ fprintf(stderr,"warning: couldn't correctly set up permissions on %s\n",sockname);
+ }
+
+ return sock;
+}
+
+
+
diff --git a/unix.h b/unix.h
new file mode 100644
index 0000000..410911e
--- /dev/null
+++ b/unix.h
@@ -0,0 +1,17 @@
+#ifndef unix_h_INCLUDED
+#define unix_h_INCLUDED
+
+#include <sys/types.h>
+#include <grp.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+
+// takes filename, returns socket fd
+// or -1 on failure.
+int unix_setup(char sockname[]);
+
+#endif // unix_h_INCLUDED
+