Back to index

glibc  2.9
Defines | Functions | Variables
nisplus-initgroups.c File Reference
#include <atomic.h>
#include <nss.h>
#include <grp.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <bits/libc-lock.h>
#include <rpcsvc/nis.h>
#include "nss-nisplus.h"
#include "nisplus-parser.h"
#include <libnsl.h>
#include <nis_intern.h>
#include <nis_xdr.h>

Go to the source code of this file.

Defines

#define NISOBJVAL(col, obj)   ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
#define NISOBJLEN(col, obj)   ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)

Functions

enum nss_status _nss_grp_create_tablename (int *errnop)
enum nss_status _nss_nisplus_initgroups_dyn (const char *user, gid_t group, long int *start, long int *size, gid_t **groupsp, long int limit, int *errnop)

Variables

nis_name grp_tablename_val attribute_hidden

Define Documentation

#define NISOBJLEN (   col,
  obj 
)    ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)

Definition at line 38 of file nisplus-initgroups.c.

#define NISOBJVAL (   col,
  obj 
)    ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)

Definition at line 35 of file nisplus-initgroups.c.


Function Documentation

Definition at line 53 of file nisplus-grp.c.

{
  if (grp_tablename_val == NULL)
    {
      const char *local_dir = nis_local_directory ();
      size_t local_dir_len = strlen (local_dir);
      static const char prefix[] = "group.org_dir.";

      char *p = malloc (sizeof (prefix) + local_dir_len);
      if (p == NULL)
       {
         *errnop = errno;
         return NSS_STATUS_TRYAGAIN;
       }

      memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);

      grp_tablename_len = sizeof (prefix) - 1 + local_dir_len;

      atomic_write_barrier ();

      if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL))
       {
         /* Another thread already installed the value.  */
         free (p);
         grp_tablename_len = strlen (grp_tablename_val);
       }
    }

  return NSS_STATUS_SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

enum nss_status _nss_nisplus_initgroups_dyn ( const char *  user,
gid_t  group,
long int start,
long int size,
gid_t **  groupsp,
long int  limit,
int errnop 
)

Definition at line 47 of file nisplus-initgroups.c.

{
  if (grp_tablename_val == NULL)
    {
      enum nss_status status = _nss_grp_create_tablename (errnop);

      if (status != NSS_STATUS_SUCCESS)
       return status;
    }

  nis_result *result;
  char buf[strlen (user) + 12 + grp_tablename_len];

  snprintf (buf, sizeof (buf), "[members=%s],%s", user, grp_tablename_val);

  result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | ALL_RESULTS, NULL, NULL);

  if (result == NULL)
    {
      *errnop = ENOMEM;
      return NSS_STATUS_TRYAGAIN;
    }

  if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
    {
      enum nss_status status = niserr2nss (result->status);

      nis_freeresult (result);
      return status;
    }

  if (NIS_RES_NUMOBJ (result) == 0)
    {
    errout:
      nis_freeresult (result);
      return NSS_STATUS_NOTFOUND;
    }

  gid_t *groups = *groupsp;
  nis_object *obj = NIS_RES_OBJECT (result);
  for (unsigned int cnt = 0; cnt < NIS_RES_NUMOBJ (result); ++cnt, ++obj)
    {
      if (__type_of (obj) != NIS_ENTRY_OBJ
         || strcmp (obj->EN_data.en_type, "group_tbl") != 0
         || obj->EN_data.en_cols.en_cols_len < 4)
       continue;

      char *numstr = NISOBJVAL (2, obj);
      size_t len = NISOBJLEN (2, obj);
      if (len == 0 || numstr[0] == '\0')
       continue;

      gid_t gid;
      char *endp;
      if (__builtin_expect (numstr[len - 1] != '\0', 0))
       {
         char numstrbuf[len + 1];
         memcpy (numstrbuf, numstr, len);
         numstrbuf[len] = '\0';
         gid = strtoul (numstrbuf, &endp, 10);
         if (*endp)
           continue;
       }
      else
       {
         gid = strtoul (numstr, &endp, 10);
         if (*endp)
           continue;
       }

      if (gid == group)
       continue;

      /* Insert this group.  */
      if (*start == *size)
       {
         /* Need a bigger buffer.  */
         long int newsize;

         if (limit > 0 && *size == limit)
           /* We reached the maximum.  */
           break;

         if (limit <= 0)
           newsize = 2 * *size;
         else
           newsize = MIN (limit, 2 * *size);

         gid_t *newgroups = realloc (groups, newsize * sizeof (*groups));
         if (newgroups == NULL)
           goto errout;
         *groupsp = groups = newgroups;
         *size = newsize;
       }

      groups[*start] = gid;
      *start += 1;
    }

  nis_freeresult (result);
  return NSS_STATUS_SUCCESS;
}

Here is the call graph for this function:


Variable Documentation

size_t grp_tablename_len attribute_hidden

Definition at line 42 of file nisplus-initgroups.c.