I want to make this program run on solaris machine .... what changes can i do .....

Hi guys,

 The following program is for getting credentials......

 I have some problems facing when run on solaris machine ......

Program is following :---->

#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 *);

  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");
      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]);

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

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)
  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));


To compile on soalris :

 cc -o tfcred02 tfcred02.c -lsocket -lnsl -lrt

To Run :


Am getting some errors:

They are following :---->

[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.)
tfcred02.c: In function `recv_cred':
tfcred02.c:86: error: structure has no member named `msg_control'
tfcred02.c:87: error: structure has no member named `msg_controllen'
tfcred02.c:88: error: structure has no member named `msg_flags'
tfcred02.c:95: error: structure has no member named `msg_controllen'
tfcred02.c:105: error: `SCM_CREDS' undeclared (first use in this function)
tfcred02.c:108: warning: passing arg 2 of `memcpy' makes pointer from integer without a cast

[ab1221@eagles ~/AdvNetProg/Assign3]$

Pls help me in this program..


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, I'm no programmer but setsockopt has a list of supported socket options /usr/include/sys/socket.h on Solaris and the list below doesn't appear to have your LOCAL_CREDS defined anywhere.  From my looking on the net LOCAL_CREDS seems to be a *BSD derivative and not something Solaris has.  More comments after this socket options below.

 * Option flags per-socket.
#define SO_DEBUG        0x0001          /* turn on debugging info recording */
#define SO_ACCEPTCONN   0x0002          /* socket has had listen() */
#define SO_REUSEADDR    0x0004          /* allow local address reuse */
#define SO_KEEPALIVE    0x0008          /* keep connections alive */
#define SO_DONTROUTE    0x0010          /* just use interface addresses */
#define SO_BROADCAST    0x0020          /* permit sending of broadcast msgs */
#define SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
#define SO_LINGER       0x0080          /* linger on close if data present */
#define SO_OOBINLINE    0x0100          /* leave received OOB data in line */
#define SO_DGRAM_ERRIND 0x0200          /* Application wants delayed error */

 * N.B.: The following definition is present only for compatibility
 * with release 3.0.  It will disappear in later releases.
#define SO_DONTLINGER   (~SO_LINGER)    /* ~SO_LINGER */

 * Additional options, not kept in so_options.
#define SO_SNDBUF       0x1001          /* send buffer size */
#define SO_RCVBUF       0x1002          /* receive buffer size */
#define SO_SNDLOWAT     0x1003          /* send low-water mark */
#define SO_RCVLOWAT     0x1004          /* receive low-water mark */
#define SO_SNDTIMEO     0x1005          /* send timeout */
#define SO_RCVTIMEO     0x1006          /* receive timeout */
#define SO_ERROR        0x1007          /* get error status and clear */
#define SO_TYPE         0x1008          /* get socket type */
#define SO_PROTOTYPE    0x1009          /* get/set protocol type */

As to the errors saying:

tfcred02.c: In function `recv_cred':
tfcred02.c:86: error: structure has no member named `msg_control'
tfcred02.c:87: error: structure has no member named `msg_controllen'
tfcred02.c:88: error: structure has no member named `msg_flags'
tfcred02.c:95: error: structure has no member named `msg_controllen'

If you look in /usr/include/sys/socket.h on a Solaris box, you'll see this within the msghdr structure:

if defined(_XPG4_2) || defined(_KERNEL)
        void            *msg_control;           /* ancillary data */
        socklen_t       msg_controllen;         /* ancillary data buffer len */
        int             msg_flags;              /* flags on received message */
        caddr_t         msg_accrights;  /* access rights sent/received */
        int             msg_accrightslen;
#endif  /* defined(_XPG4_2) || defined(_KERNEL) */

So you will only get the msg_control, msg_controllen, and msg_flags structure members assuming you have _XPG4_2 or _KERNEL_ defined.  From the errors you are getting, it appears you haven't defined either of these.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.