home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / group.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  8KB  |  385 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *  Portions Copyright (C) 1996 Jeff Shepherd
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22.  
  23. /* stubs for group-file access functions */
  24.  
  25. #define _KERNEL
  26. #include "ixemul.h"
  27. #include "kprintf.h"
  28. #include <stdlib.h>
  29. #include <grp.h>
  30. #include <stdio.h>
  31. #include "multiuser.h"
  32. #include <amitcp/usergroup.h>
  33.  
  34. static int grscan(), start_gr();
  35.  
  36. #define MAXGRP        200
  37. #define MAXLINELENGTH    1024
  38.  
  39. int getgroups(int gidsetlen, int *gidset)
  40. {
  41.   /* parameter check */
  42.   if (gidset == NULL || gidsetlen < 0)
  43.     {
  44.       errno = EFAULT;
  45.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  46.  
  47.       return -1;
  48.     }
  49.  
  50.   if (gidsetlen == 0)
  51.     {
  52.       errno = EINVAL;
  53.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  54.  
  55.       return -1;
  56.     }
  57.  
  58.   if (muBase)
  59.     {
  60.       /* muFS detected */
  61.       UWORD *Grps;
  62.       int i;
  63.  
  64.       /* get the information */
  65.       struct muExtOwner *me = muGetTaskExtOwner (NULL);
  66.       if (me == NULL)
  67.       {
  68.         *gidset = 0;
  69.         return 1;             /* nobody */
  70.       }
  71.  
  72.       /* store primary group */
  73.       gidset[0] = me->gid;
  74.       gidset++;
  75.       gidsetlen--;
  76.  
  77.       /* ensure enough place */
  78.       if (gidsetlen < me->NumSecGroups)
  79.       {
  80.         errno = EINVAL;
  81.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  82.  
  83.         return -1;
  84.       }
  85.  
  86.       /* slow, but have to copy from UWORD[] --> int[] */
  87.       Grps = muSecGroups (me);
  88.       for (i = me->NumSecGroups; i >= 0; i--)
  89.       gidset[i] = Grps[i];
  90.  
  91.       /* clean up */
  92.       i = me->NumSecGroups + 1;
  93.       muFreeExtOwner (me);
  94.  
  95.       return i;
  96.     }
  97.  
  98.   if (u.u_ixnetbase)
  99.     return netcall(NET_getgroups, gidsetlen, gidset);
  100.  
  101.   /* we return 1 group, group 0 (you really ARE root on the amiga:-)) */
  102.   if (gidsetlen >= 1)
  103.     {
  104.       *gidset = 0;
  105.       return 1;
  106.     }
  107.  
  108.   errno = EINVAL;
  109.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  110.   return -1;
  111. }
  112.  
  113. static struct group *
  114. GroupInfo2grp (struct muGroupInfo *GI, struct muUserInfo *UI)
  115. {
  116.   static char *members[] = { NULL };
  117.   struct group *grp = &u.u_group;
  118.  
  119.   if (GI)
  120.   {
  121.     grp->gr_name = GI->GroupID;
  122.     grp->gr_passwd = "*";
  123.     grp->gr_gid = GI->gid;
  124.     grp->gr_mem = members;        /* much work has to be done on this */
  125.  
  126.     return grp;
  127.   }
  128.   return NULL;
  129. }
  130.  
  131. struct group *getgrgid (gid_t gid)
  132. {
  133.   int rval;
  134.  
  135.   if (muBase)
  136.     {
  137.       /* active multiuser */
  138.       struct muGroupInfo *GI = u.u_GroupInfo;
  139.       struct muUserInfo *UI = u.u_UserInfo;
  140.  
  141.       GI->gid = gid;
  142.       GI = muGetGroupInfo (GI, muKeyType_gid);
  143.  
  144.       return GroupInfo2grp (GI, UI);       /* handles errors */
  145.     }
  146.   if (u.u_ixnetbase)
  147.     return (struct group *)netcall(NET_getgrgid, gid);
  148.   if (!start_gr())
  149.     return(NULL);
  150.  
  151.   rval = grscan(1, gid, NULL);
  152.   if (!u.u_grp_stayopen)
  153.     endgrent();
  154.  
  155.   return (rval ? &u.u_group : NULL);
  156. }
  157.  
  158. struct group *getgrnam (const char *name)
  159. {
  160.   int rval;
  161.  
  162.   if (muBase)
  163.     {
  164.       /* active multiuser */
  165.       struct muGroupInfo *GI = u.u_GroupInfo;
  166.       struct muUserInfo *UI = u.u_UserInfo;
  167.  
  168.       /*
  169.        * some validation checks
  170.        */
  171.  
  172.       if (name == NULL)
  173.         return NULL;
  174.  
  175.       if ((muGROUPIDSIZE - 1) < strlen (name))
  176.         return NULL;
  177.  
  178.       strcpy (GI->GroupID, name);
  179.       GI = muGetGroupInfo (GI, muKeyType_GroupID);
  180.  
  181.       return GroupInfo2grp (GI, UI);       /* handles errors */
  182.     }
  183.   if (u.u_ixnetbase)
  184.     return (struct group *)netcall(NET_getgrnam, name);
  185.  
  186.   if (!start_gr())
  187.     return NULL;
  188.  
  189.   rval = grscan(1, 0, name);
  190.   if (!u.u_grp_stayopen)
  191.     endgrent();
  192.  
  193.   return (rval ? &u.u_group : NULL);
  194. }
  195.  
  196. gid_t
  197. getgid (void)
  198. {
  199.   if (muBase)
  200.     return (muGetTaskOwner(NULL) & muMASK_GID);
  201.   if (u.u_ixnetbase)
  202.     return netcall(NET_getgid);
  203.   return 0;
  204. }
  205.  
  206. gid_t
  207. getegid (void)
  208. {
  209.   if (muBase == NULL && u.u_ixnetbase)
  210.     return netcall(NET_getegid);
  211.   return getgid ();
  212. }
  213.  
  214. struct group *
  215. getgrent(void)
  216. {
  217.   if (muBase)
  218.     {
  219.       /* active multiuser */
  220.       struct muGroupInfo *GI = u.u_fileGroupInfo;
  221.       struct muUserInfo *UI  = u.u_UserInfo;
  222.  
  223.       GI = muGetGroupInfo (GI, u.u_groupfileopen ? muKeyType_Next : muKeyType_First);
  224.       u.u_groupfileopen = TRUE;
  225.  
  226.       return GroupInfo2grp (GI, UI);       /* handles errors */
  227.     }
  228.   if (u.u_ixnetbase)
  229.     return (struct group *)netcall(NET_getgrent);
  230.  
  231.   if ((!u.u_grp_fp && !start_gr()) || !grscan(0, 0, NULL))
  232.     return NULL;
  233.   return &u.u_group;
  234. }
  235.  
  236. static int
  237. start_gr(void)
  238. {
  239.   if (u.u_grp_fp) {
  240.     syscall(SYS_rewind, u.u_grp_fp);
  241.     return 1;
  242.   }
  243.   return ((u.u_grp_fp = (FILE *)syscall(SYS_fopen, _PATH_GROUP, "r")) ? 1 : 0);
  244. }
  245.  
  246. int
  247. setgrent(void)
  248. {
  249.   if (muBase || !u.u_ixnetbase)
  250.     return setgroupent(0);
  251.   return netcall(NET_setgrent);
  252. }
  253.  
  254. int
  255. setgroupent(int stayopen)
  256. {
  257.   if (muBase)
  258.     {
  259.       u.u_groupfileopen = FALSE;
  260.       return 1;
  261.     }
  262.   if (u.u_ixnetbase)
  263.     return netcall(NET_setgroupent, stayopen);
  264.   if (!start_gr())
  265.     return 0;
  266.  
  267.   u.u_grp_stayopen = stayopen;
  268.   return 1;
  269. }
  270.  
  271. void
  272. endgrent(void)
  273. {
  274.   if (muBase)
  275.     {
  276.       setgroupent(0);
  277.       return;
  278.     }
  279.   if (u.u_ixnetbase) {
  280.     netcall(NET_endgrent);
  281.     return;
  282.   }
  283.   if (u.u_grp_fp) {
  284.     syscall(SYS_fclose, u.u_grp_fp);
  285.     u.u_grp_fp = NULL;
  286.   }
  287. }
  288.  
  289. static int
  290. grscan(int search, int gid, char *name)
  291. {
  292.   register char *cp, **m;
  293.   char *bp;
  294.   char *fgets(), *strsep(), *index();
  295.  
  296.   if (u.u_grp_line == NULL)
  297.     u.u_grp_line = malloc(MAXLINELENGTH);
  298.   if (u.u_members == NULL)
  299.     u.u_members = malloc(MAXGRP * sizeof(char *));
  300.   if (u.u_grp_line == NULL || u.u_members == NULL)
  301.     {
  302.       errno = ENOMEM;
  303.       return 0;
  304.     }
  305.   for (;;) {
  306.     if (!syscall(SYS_fgets, u.u_grp_line, MAXLINELENGTH, u.u_grp_fp))
  307.       return 0;
  308.  
  309.     bp = u.u_grp_line;
  310.     /* skip lines that are too big */
  311.     if (!index(u.u_grp_line, '\n')) {
  312.       int ch;
  313.  
  314.       while ((ch = getc(u.u_grp_fp)) != '\n' && ch != EOF) ;
  315.       continue;
  316.     }
  317.  
  318.     u.u_group.gr_name = strsep(&bp, ":\n");
  319.     if (search && name && strcmp(u.u_group.gr_name, name))
  320.       continue;
  321.  
  322.     u.u_group.gr_passwd = strsep(&bp, ":\n");
  323.     if (!(cp = strsep(&bp, ":\n")))
  324.       continue;
  325.  
  326.     u.u_group.gr_gid = atoi(cp);
  327.     if (search && name == NULL && u.u_group.gr_gid != gid)
  328.       continue;
  329.  
  330.     for (m = u.u_group.gr_mem = u.u_members;; ++m) {
  331.       if (m == &u.u_members[MAXGRP - 1]) {
  332.         *m = NULL;
  333.     break;
  334.       }
  335.       if ((*m = strsep(&bp, ", \n")) == NULL)
  336.         break;
  337.     }
  338.     return 1;
  339.   }
  340.   /* NOTREACHED */
  341. }
  342.  
  343. int
  344. setgroups (int ngroups, const int *gidset)
  345. {
  346.   if (u.u_ixnetbase && !muBase)
  347.     return netcall(NET_setgroups, ngroups, gidset);
  348.   return (ngroups >= 1) ? 0 : -1;
  349. }
  350.  
  351. int
  352. initgroups (const char *name, int basegid)
  353. {
  354.   if (u.u_ixnetbase && !muBase)
  355.     return netcall(NET_initgroups, name, basegid);
  356.   return 0;
  357. }
  358.  
  359. int
  360. setgid (gid_t gid)
  361. {
  362.   if (u.u_ixnetbase && !muBase)
  363.     return netcall(NET_setgid, gid);
  364.   /* just always succeed... */
  365.   return 0;
  366. }
  367.  
  368. int
  369. setegid (gid_t gid)
  370. {
  371.   if (u.u_ixnetbase && !muBase)
  372.     return netcall(NET_setegid, gid);
  373.   /* just always succeed... */
  374.   return 0;
  375. }
  376.  
  377. int
  378. setregid (int rgid, int egid)
  379. {
  380.   if (u.u_ixnetbase && !muBase)
  381.     return netcall(NET_setregid, rgid, egid);
  382.   /* just always succeed... */
  383.   return 0;
  384. }
  385.