Back to index

glibc  2.9
nis_addmember.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2004, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <string.h>
00022 #include <rpcsvc/nis.h>
00023 
00024 nis_error
00025 nis_addmember (const_nis_name member, const_nis_name group)
00026 {
00027   if (group != NULL && group[0] != '\0')
00028     {
00029       size_t grouplen = strlen (group);
00030       char buf[grouplen + 14 + NIS_MAXNAMELEN];
00031       char domainbuf[grouplen + 2];
00032       nis_result *res, *res2;
00033       nis_error status;
00034       char *cp, *cp2;
00035 
00036       cp = rawmemchr (nis_leaf_of_r (group, buf, sizeof (buf) - 1), '\0');
00037       cp = stpcpy (cp, ".groups_dir");
00038       cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
00039       if (cp2 != NULL && cp2[0] != '\0')
00040         {
00041          *cp++ = '.';
00042           stpcpy (cp, cp2);
00043         }
00044       res = nis_lookup (buf, FOLLOW_LINKS | EXPAND_NAME);
00045       if (NIS_RES_STATUS (res) != NIS_SUCCESS)
00046        {
00047          status = NIS_RES_STATUS (res);
00048          nis_freeresult (res);
00049          return status;
00050        }
00051       if (NIS_RES_NUMOBJ (res) != 1
00052          || __type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ)
00053        {
00054          nis_freeresult (res);
00055          return NIS_INVALIDOBJ;
00056        }
00057 
00058       u_int gr_members_len
00059        = NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len;
00060 
00061       nis_name *new_gr_members_val
00062        = realloc (NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val,
00063                  (gr_members_len + 1) * sizeof (nis_name));
00064       if (new_gr_members_val == NULL)
00065        goto nomem_out;
00066 
00067       NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val
00068        = new_gr_members_val;
00069 
00070       new_gr_members_val[gr_members_len] = strdup (member);
00071       if (new_gr_members_val[gr_members_len] == NULL)
00072        {
00073        nomem_out:
00074          nis_freeresult (res);
00075          return NIS_NOMEMORY;
00076        }
00077       ++NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len;
00078 
00079       /* Check the buffer bounds are not exceeded.  */
00080       assert (strlen (NIS_RES_OBJECT(res)->zo_name) + 1 < grouplen + 14);
00081       cp = stpcpy (buf, NIS_RES_OBJECT(res)->zo_name);
00082       *cp++ = '.';
00083       strncpy (cp, NIS_RES_OBJECT (res)->zo_domain, NIS_MAXNAMELEN);
00084       res2 = nis_modify (buf, NIS_RES_OBJECT (res));
00085       status = NIS_RES_STATUS (res2);
00086       nis_freeresult (res);
00087       nis_freeresult (res2);
00088 
00089       return status;
00090     }
00091   else
00092     return NIS_FAIL;
00093 }