Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

About LOCAL_CREDS option ......

Posted on 2004-08-07
4
Medium Priority
?
255 Views
Last Modified: 2010-04-21
Hi guys

For credentials in unix domain sockets, we use LOCAL_CREDS option set using setsockopt(). But I want to make this code work on solaris . what should i replace it with in order to work...


the syntax is :

setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));

what should i do to make it workon solaris machine ..

Full code is :-->

#include   <stdio.h>
#include   <stdlib.h>
#include   <sys/socket.h>
#include   <errno.h>
#include   <sys/types.h>
#include   <sys/uio.h>
#include   <signal.h>
#include   <time.h>
#include   <sys/types.h>
#include   <stdlib.h>
#include   <pthread.h>
#include   <sys/param.h>
#include   <sys/cred.h>


ssize_t recv_cred(int, void *, size_t, struct cred *);

main()
{
  int             fd[2], on, n;
  char            buf[100];
  struct cred    cred;

  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
    perror("Socketpair Error");

  /* must set the socket option on the *receiving* socket */
  on = 1;
  setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));

  write(fd[0], "hello, world\n", 13);

  if ( (n = recv_cred(fd[1], buf, sizeof(buf), &cred)) < 0)
    perror("Recv_Cred Error");

  else if (n == 0)
    perror("Recv_Cred, Unexpected EOF");

  buf[n] = 0;                     /* null terminate */
  printf("data: %s", buf);

 if (cred.cr_ngroups == 0)
    printf("[No CREDENTIALS Returned]\n");

  else
    {
      printf(" Real User ID = %d\n", cred.cr_ruid);
      printf(" Real Group ID = %d\n", cred.cr_rgid);
      //      printf(" Login Name = %-*s\n", MAXLOGNAME, cred.cr_login);
      printf(" Effective User ID = %d\n", cred.cr_uid);
      printf(" Effective Group ID = %d\n", cred.cr_gid);
      printf(" %d Supplementary Groups:", cred.cr_ngroups - 1);

      for (n = 1; n < cred.cr_ngroups; n++)   /* [0] is the egid */
        printf(" %d", cred.cr_groups[n]);

      printf("\n");
    }

  exit(0);

}
#define CONTROL_LEN     (sizeof(struct cmsghdr) + sizeof(struct cred))


ssize_t
recv_cred(int fd, void *ptr, size_t nbytes, struct cred *credptr)
{
  struct msghdr   msg;
  struct iovec    iov[1];
  char            control[CONTROL_LEN + 20];
  int             n;

  msg.msg_name = NULL;
  msg.msg_namelen = 0;

  iov[0].iov_base = ptr;
  iov[0].iov_len = nbytes;

  msg.msg_iov = iov;
  msg.msg_iovlen = 1;

  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);
 msg.msg_flags = 0;

  if ( (n = recvmsg(fd, &msg, 0)) < 0)
    return(n);

  credptr->cr_ngroups = 0;       /* indicates no credentials returned */

  if (credptr && msg.msg_controllen > 0)
    {
      struct cmsghdr  *cmptr = (struct cmsghdr *) control;

      if (cmptr->cmsg_len != sizeof(struct cmsghdr) + sizeof(struct cred))
        err_quit("control length = %d", cmptr->cmsg_len);

      if (cmptr->cmsg_level != SOL_SOCKET)
        err_quit("control level != SOL_SOCKET");

      if (cmptr->cmsg_type != SCM_CREDS)
        err_quit("control type != SCM_CREDS");

      memcpy(credptr, CMSG_DATA(cmptr), sizeof(struct cred));
    }

  return(n);
}


Am getting few errors when i compile this on solaris 9 machine ...

i want to get rid of that LOCAL_CREDS error and SCM_CREDS error..

can u help me pls .... or else can u tell me how i can extract credentials like user id group id of the peer process...

bye
aman

0
Comment
Question by:amankhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 11747602
> Am getting few errors when i compile this on solaris 9 machine ...
please post errors
0
 

Author Comment

by:amankhan
ID: 11750658
[ab1221@eagles ~/AdvNetProg/Assign3]$ cc -o tfcred02 tfcred02.c -lsocket -lnsl -lrt
tfcred02.c: In function `main':
tfcred02.c:30: error: `LOCAL_CREDS' undeclared (first use in this function)
tfcred02.c:30: error: (Each undeclared identifier is reported only once
tfcred02.c:30: error: for each function it appears in.)
 [ab1221@eagles ~/AdvNetProg/Assign3]$
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11751113
AFAIK Solaris does not have these socket options
0
 

Author Comment

by:amankhan
ID: 11759662
Then how can i make it work....
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question