Back to index

glibc  2.9
nis_removemember.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997,1998,1999,2004,2005,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_removemember (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 = stpcpy (cp, ".");
00042           stpcpy (cp, cp2);
00043         }
00044       res = nis_lookup (buf, FOLLOW_LINKS | EXPAND_NAME);
00045       if (res == NULL)
00046        return NIS_NOMEMORY;
00047       if (NIS_RES_STATUS (res) != NIS_SUCCESS)
00048         {
00049          status = NIS_RES_STATUS (res);
00050          nis_freeresult (res);
00051           return status;
00052         }
00053 
00054       if (NIS_RES_NUMOBJ (res) != 1
00055          || __type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ)
00056        {
00057          nis_freeresult (res);
00058          return NIS_INVALIDOBJ;
00059        }
00060 
00061       nis_name *gr_members_val
00062        = NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_val;
00063       u_int gr_members_len
00064        = NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len;
00065 
00066       u_int j = 0;
00067       for (u_int i = 0; i < gr_members_len; ++i)
00068        if (strcmp (gr_members_val[i], member) != 0)
00069          gr_members_val[j++] = gr_members_val[i];
00070        else
00071          free (gr_members_val[i]);
00072 
00073       /* There is no need to reallocate the gr_members_val array.  We
00074         just adjust the size to match the number of strings still in
00075         it.  Yes, xdr_array will use mem_free with a size parameter
00076         but this is mapped to a simple free call which determines the
00077         size of the block by itself.  */
00078       NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len = j;
00079 
00080       cp = stpcpy (buf, NIS_RES_OBJECT (res)->zo_name);
00081       *cp++ = '.';
00082       strncpy (cp, NIS_RES_OBJECT (res)->zo_domain, NIS_MAXNAMELEN);
00083       res2 = nis_modify (buf, NIS_RES_OBJECT (res));
00084       status = NIS_RES_STATUS (res2);
00085       nis_freeresult (res);
00086       nis_freeresult (res2);
00087 
00088       return status;
00089     }
00090   else
00091     return NIS_FAIL;
00092 }