0

I have 2 different aplications running in 2 different users in linux. I want them to be connected by unix sockets and, as a unix domain socket is known by a pathname, the 2 applications need to share the same path and same the socket file that is created. The problem here is that when binding the socket in the Server, everything is fine but, when trying to connect from the 2nd application the error "access denied" appears.

Here is the code I am using for the server, who does create the socket file.

  int main() {
    struct sockaddr_un addr;
    char buf[100];
    int fd,cl,rc;

    if (argc > 1) socket_path=argv[1];

    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
      perror("socket error");
      exit(-1);
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    if (*socket_path.c_str() == '\0') {
      *addr.sun_path = '\0';
      strncpy(addr.sun_path+1, socket_path.c_str()+1, sizeof(addr.sun_path)-2);
    } else {
      strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path)-1);
      unlink(socket_path.c_str());
    }

    if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
      perror("bind error");
      exit(-1);
    }

    if (listen(fd, 5) == -1) {
      perror("listen error");
      exit(-1);
    }

     return 0;
}
Samuel
  • 816
  • 1
  • 7
  • 13
  • 2
    Look at the socket file itself. Its ownership and permissions mean exactly the same thing as the ownership and permissions on any other file. You probably need to give the socket file permissions to "other". You can do that with chmod. – Sam Varshavchik Apr 29 '20 at 12:06
  • 1
    Note that relying on socket file permissions to control who can connect to it is non-portable; see *Pathname socket ownership and permissions* in the [Linux manpage](http://man7.org/linux/man-pages/man7/unix.7.html). – Shawn Apr 29 '20 at 17:16

1 Answers1

0

I have solved the problem just changing permissions with chmod function as @Sam Varshavchik suggested.

As both users belong to the same group, I have used S_IRWXG to set all permisions for the group.

chmod(socket_path.c_str(), S_IRWXG);
Samuel
  • 816
  • 1
  • 7
  • 13