diff options
author | ubq323 <ubq323@ubq323.website> | 2022-01-13 18:29:01 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2022-01-13 18:29:57 +0000 |
commit | d9ce6714cd63132ce9cdf0c9702815e8d135693f (patch) | |
tree | d85ee996c5deb394639d8be9e4c0077e85abf071 | |
parent | 894e7d9aa5e9d50a2c714ad3eb38dc0056d0f6ae (diff) |
correctly set group and permissions on socket on startup
-rw-r--r-- | thing.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -19,10 +19,12 @@ #define REALNAME ",flappy fly bird man?" #define CHANNEL "#a" +#define SOCKETGROUP "apionet" #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <grp.h> #include <errno.h> #include <string.h> #include <sys/types.h> @@ -183,6 +185,43 @@ int unix_setup() { perror("(unix) bind"); return -1; } + + // set group and permissions on socket + long buflen = sysconf(_SC_GETGR_R_SIZE_MAX); + buflen = (buflen == -1) ? 1024 : buflen; + char *buf = malloc(buflen); + struct group grp; + struct group *res; + errno = 0; + // get group gid from name + getgrnam_r(SOCKETGROUP,&grp,buf,buflen,&res); + if (res == NULL) { + if (errno != 0) + perror("getgrnam_r"); + else + fprintf(stderr,"group %s does not exist",SOCKETGROUP); + free(buf); + return -1; + } + free(buf); + + // set socket's group + if (chown(SOCKETNAME,-1,grp.gr_gid) == -1) { + perror("chown"); + return -1; + } + + struct stat sb; + if (stat(SOCKETNAME,&sb) == -1) { + perror("stat"); + return -1; + } + if (chmod(SOCKETNAME,sb.st_mode|S_IWGRP) == -1) { + perror("chmod"); + return -1; + } + + return sock; } |