Back to index

glibc  2.9
nis_creategroup.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2000, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@suse.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 <time.h>
00021 #include <string.h>
00022 #include <rpcsvc/nis.h>
00023 
00024 nis_error
00025 nis_creategroup (const_nis_name group, unsigned int flags)
00026 {
00027   if (group != NULL && group[0] != '\0')
00028     {
00029       size_t grouplen = strlen (group);
00030       char buf[grouplen + 50];
00031       char leafbuf[grouplen + 2];
00032       char domainbuf[grouplen + 2];
00033       nis_error status;
00034       nis_result *res;
00035       char *cp, *cp2;
00036       nis_object *obj;
00037 
00038       cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
00039       cp = stpcpy (cp, ".groups_dir");
00040       cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
00041       if (cp2 != NULL && cp2[0] != '\0')
00042         {
00043          *cp++ = '.';
00044           stpcpy (cp, cp2);
00045         }
00046       else
00047        return NIS_BADNAME;
00048 
00049       obj = calloc (1, sizeof (nis_object));
00050       if (__builtin_expect (obj == NULL, 0))
00051        return NIS_NOMEMORY;
00052 
00053       obj->zo_oid.ctime = obj->zo_oid.mtime = time (NULL);
00054       obj->zo_name = strdup (leafbuf);
00055       obj->zo_owner = __nis_default_owner (NULL);
00056       obj->zo_group = __nis_default_group (NULL);
00057       obj->zo_domain = strdup (domainbuf);
00058       if (obj->zo_name == NULL || obj->zo_owner == NULL
00059          || obj->zo_group == NULL || obj->zo_domain == NULL)
00060        {
00061          free (obj->zo_group);
00062          free (obj->zo_owner);
00063          free (obj->zo_name);
00064          free (obj);
00065          return NIS_NOMEMORY;
00066        }
00067       obj->zo_access = __nis_default_access (NULL, 0);
00068       obj->zo_ttl = 60 * 60;
00069       obj->zo_data.zo_type = NIS_GROUP_OBJ;
00070       obj->zo_data.objdata_u.gr_data.gr_flags = flags;
00071       obj->zo_data.objdata_u.gr_data.gr_members.gr_members_len = 0;
00072       obj->zo_data.objdata_u.gr_data.gr_members.gr_members_val = NULL;
00073 
00074       res = nis_add (buf, obj);
00075       nis_free_object (obj);
00076       if (res == NULL)
00077        return NIS_NOMEMORY;
00078       status = NIS_RES_STATUS (res);
00079       nis_freeresult (res);
00080 
00081       return status;
00082     }
00083   return NIS_FAIL;
00084 }