home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devs / netinfo / passwdunit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  8.4 KB  |  298 lines

  1. RCS_ID_C="$Id: passwdunit.c,v 3.2 1994/05/19 04:29:29 ppessi Exp $";
  2. /*
  3.  * passwdunit.c --- unit for passwd entries
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Wed Sep 15 01:25:26 1993 ppessi
  13.  * Last modified: Thu May 19 04:01:50 1994 ppessi
  14.  */
  15.  
  16. #include "entries.h"
  17. #include <string.h>
  18. #include <assert.h>
  19.  
  20. static void *CopyEntToPasswd(struct NetInfoReq *req, struct Ent *e);
  21. static struct Ent *CopyPasswdToEnt(BASE, struct NetInfoReq *req);
  22. static struct Ent *ParsePasswd(BASE, register UBYTE *p);
  23. static void GetMembers(BASE, struct NetInfoReq *req, struct NetInfoMap *nim);
  24. static int PrintPasswd(BASE, BPTR file, struct Ent *e);
  25.  
  26. const static struct MapMethods passwd_methods[1] = 
  27. {
  28.   {
  29.     ParsePasswd,
  30.     PrintPasswd,
  31.     CopyEntToPasswd,
  32.     CopyPasswdToEnt,
  33.     EntCleanup,
  34.     NULL,
  35.     EntHandleNotify,
  36.   },
  37. };
  38.  
  39. /*
  40.  * Allocate and initialize passwd map
  41.  */
  42. struct NetInfoMap *InitPasswdMap(BASE)
  43. {
  44.   struct NetInfoMap *nim = AllocVec(sizeof(*nim), MEMF_CLEAR|MEMF_PUBLIC);
  45.  
  46.   if (nim) {
  47.     nim->nim_Methods = passwd_methods;
  48.     nim->nim_Name = "passwd";
  49.     nim->nim_Filename = _PATH_PASSWD;
  50.   }
  51.  
  52.   return nim;
  53. }
  54.  
  55. /*
  56.  * Copy an PasswdEnt structure to passwd structure 
  57.  */
  58. static void *CopyEntToPasswd(struct NetInfoReq *req, struct Ent *e)
  59. {
  60.   struct PasswdEnt *pe = (struct PasswdEnt *)e;
  61.   struct NetInfoPasswd *pw = req->io_Data;
  62.   UBYTE  *to = (UBYTE *)(pw + 1);
  63.  
  64.   ULONG size = req->io_Length;
  65.   ULONG actual = sizeof(*pw) + pe->pe_tlen;
  66.  
  67.   req->io_Actual = actual;
  68.   
  69.   if (size < actual) {
  70.     return NULL;
  71.   }
  72.  
  73.   /* copy strings */
  74.   to = stpcopy(pw->pw_name   = to, pe->pe_passwd->pw_name);
  75.   to = stpcopy(pw->pw_passwd = to, pe->pe_passwd->pw_passwd);
  76.   to = stpcopy(pw->pw_gecos  = to, pe->pe_passwd->pw_gecos);
  77.   to = stpcopy(pw->pw_dir    = to, pe->pe_passwd->pw_dir);
  78.   to = stpcopy(pw->pw_shell  = to, pe->pe_passwd->pw_shell);
  79.  
  80.   assert(to == (UBYTE *)req->io_Data + actual);
  81.  
  82.   /* .. and ids */
  83.   pw->pw_uid = pe->pe_passwd->pw_uid;
  84.   pw->pw_gid = pe->pe_passwd->pw_gid;
  85.  
  86.   return pw;
  87. }
  88.  
  89. /*
  90.  * Copy an passwd structure into a internal entry
  91.  */
  92. static struct Ent *CopyPasswdToEnt(BASE, struct NetInfoReq *req)
  93. {
  94.   struct NetInfoPasswd *pw = (struct NetInfoPasswd*)req->io_Data;
  95.   struct PasswdEnt *pe;
  96.   UBYTE *to;
  97.   ULONG txtlen;
  98.  
  99.   /*
  100.    * These cause EFAULT
  101.    */
  102.   if (pw == NULL || (1 & (LONG)pw) != 0 ||
  103.       pw->pw_name == NULL || 
  104.       pw->pw_passwd == NULL ||
  105.       pw->pw_gecos == NULL ||
  106.       pw->pw_dir == NULL ||
  107.       pw->pw_shell == NULL) {
  108.     req->io_Error = IOERR_BADADDRESS;
  109.     return NULL;
  110.   }
  111.  
  112.   txtlen = strlen(pw->pw_name) + strlen(pw->pw_passwd) + strlen(pw->pw_gecos) +
  113.     strlen(pw->pw_dir) + strlen(pw->pw_shell) + 5;
  114.  
  115.   pe = AllocVec(sizeof(*pe) + txtlen, MEMF_CLEAR);
  116.   if (pe == NULL) {
  117.     req->io_Error = NIERR_NOMEM;
  118.     return NULL;
  119.   }
  120.  
  121.   pe->pe_node.ln_Type = ENT_PASSWD;
  122.   pe->pe_node.ln_Pri  = ENT_CHANGED;
  123.   pe->pe_tlen = txtlen;
  124.   pe->pe_passwd->pw_uid = pw->pw_uid;
  125.   pe->pe_passwd->pw_gid = pw->pw_gid;
  126.  
  127.   to = (UBYTE *)(pe + 1);
  128.   to = stpcopy(pe->pe_passwd->pw_name   = to, pw->pw_name);
  129.   to = stpcopy(pe->pe_passwd->pw_passwd = to, pw->pw_passwd);
  130.   to = stpcopy(pe->pe_passwd->pw_gecos  = to, pw->pw_gecos);
  131.   to = stpcopy(pe->pe_passwd->pw_dir    = to, pw->pw_dir);
  132.   to = stpcopy(pe->pe_passwd->pw_shell  = to, pw->pw_shell);
  133.  
  134.   assert(to == pe->pe_passwd->pw_name + txtlen);
  135.  
  136.   return (struct Ent *)pe;
  137. }
  138.  
  139. /****** netinfo.device/passwd **********************************************
  140.  
  141.    NAME
  142.         passwd - format of the password file
  143.  
  144.    DESCRIPTION
  145.  
  146.         The passwd files are files consisting of newline separated records,
  147.         one per user, containing seven bar (`|') separated fields.  These
  148.         fields are as follows:
  149.  
  150.             name      User's login name.
  151.  
  152.             password  User's encrypted password.
  153.  
  154.             uid       User's id.
  155.  
  156.             gid       User's login group id.
  157.  
  158.             gecos     General information about the user.
  159.  
  160.             home_dir  User's home directory.
  161.  
  162.             shell     User's login shell.
  163.  
  164.         The name field is the login used to access the computer account, and
  165.         the uid field is the number associated with it.  They should both be
  166.         unique across the system (and often across a group of systems) since
  167.         they control file access.
  168.  
  169.         While it is possible to have multiple entries with identical login
  170.         names and/or identical user id's, it is usually a mistake to do so.
  171.         Routines that manipulate these files will often return only one of
  172.         the multiple entries, and that one by random selection.
  173.  
  174.         The login name must never begin with a hyphen (`-'); also, it is
  175.         strongly suggested that neither upper-case characters or dots (`.')
  176.         be part of the name, as this tends to confuse mailers. No field may
  177.         contain a bar (`|') as this has been used to separate the fields in
  178.         the user database.
  179.  
  180.         The password field is the encrypted form of the password. The actual
  181.         format is not restricted by netinfo.device, but usergroup.library
  182.         uses same format as Version 7 UNIX (POSIX) or 4.4BSD.  The format
  183.         used by MultiUser 1.5 or AS225r2ß is also possible, but its use is
  184.         strongly discouraged.
  185.  
  186.         If the password field is empty, no password will be required to gain
  187.         access to the machine.  This is almost invariably a mistake. Because
  188.         these files contain the encrypted user passwords, they should not be
  189.         readable by anyone without appropriate privileges.
  190.  
  191.         The group field is the group that the user will be placed in upon
  192.         login.
  193.  
  194.         The gecos field normally contains comma (`,') separated subfields as
  195.         follows:
  196.  
  197.             name           user's full name
  198.             office         user's office number
  199.             wphone         user's work phone number
  200.             hphone         user's home phone number
  201.  
  202.         This information is used by the finger program.
  203.  
  204.         The user's home directory is the full DOS path name where the user
  205.         will be placed on login.
  206.  
  207.         The shell field is the command interpreter the user prefers. If
  208.         there is nothing in the shell field, the CLI is assumed.
  209.  
  210.    FILES
  211.         AmiTCP:db/passwd
  212.  
  213.    SEE ALSO
  214.         group
  215.  
  216.    COPYRIGHT
  217.         Copyright 1980, 1991 The Regents of the University of California.
  218.         Copyright 1993, 1994 AmiTCP/IP-Group, <AmiTCP-Group@hut.fi>, 
  219.         Helsinki University of Technology, Helsinki
  220.  
  221.    HISTORY
  222.         A passwd file format appeared in Version 6 AT&T UNIX.
  223.  
  224.         The used format is basically the same as in the Multiuser Library
  225.         1.3 and AS225r2 use, except the password field.
  226.  
  227. ****************************************************************************
  228. */
  229.  
  230. /* 
  231.  * Parse a passwd database entry
  232.  */
  233. static struct Ent *ParsePasswd(BASE, register UBYTE *p)
  234. {
  235.   int i;
  236.   UBYTE *to, *np = p, *field[PASSWDFIELDS];
  237.   ULONG txtlen;
  238.   LONG uid, gid;
  239.   struct PasswdEnt *pe;
  240.  
  241.   for (i = 0;
  242.        i < PASSWDFIELDS && (field[i++] = strsep(&np, "|\n")) && np && *np;)
  243.     ;
  244.  
  245.   assert(np != NULL);
  246.   if (i < PASSWDFIELDS || !np) {
  247.     return NULL;
  248.   }
  249.  
  250.   if ((i = StrToLong(field[2], &uid)) <= 0 || field[2][i] != '\0')
  251.     return NULL;
  252.   if ((i = StrToLong(field[3], &gid)) <= 0 || field[3][i] != '\0')
  253.     return NULL;
  254.  
  255.   txtlen = np - field[0] - (field[4] - field[2]);
  256.   pe = AllocVec(sizeof(*pe) + txtlen, MEMF_CLEAR);
  257.   if (!pe)
  258.     return NULL;
  259.  
  260.   pe->pe_node.ln_Type = ENT_PASSWD;
  261.   pe->pe_tlen = txtlen;
  262.   pe->pe_passwd->pw_uid = uid;
  263.   pe->pe_passwd->pw_gid = gid;
  264.  
  265.   to = (UBYTE *)(pe + 1);
  266.   to = stpcopy(pe->pe_passwd->pw_name   = to, field[0]);
  267.   to = stpcopy(pe->pe_passwd->pw_passwd = to, field[1]);
  268.   to = stpcopy(pe->pe_passwd->pw_gecos  = to, field[4]);
  269.   to = stpcopy(pe->pe_passwd->pw_dir    = to, field[5]);
  270.   to = stpcopy(pe->pe_passwd->pw_shell  = to, field[6]);
  271.  
  272.   assert(to == pe->pe_passwd->pw_name + txtlen);
  273.  
  274.   return (struct Ent *)pe;
  275. }
  276.  
  277. /*
  278.  * Print out an passwd entry
  279.  */
  280. static int PrintPasswd(BASE, BPTR file, struct Ent *e)
  281. {
  282.   struct PasswdEnt *pe = (struct PasswdEnt *)e;
  283.  
  284.   if (VFPrintf(file, "%s|%s|%ld|%ld|%s|%s|%s\n", (LONG*)pe->pe_passwd) == -1)
  285.     return NIERR_ACCESS;
  286.   else
  287.     return 0;
  288. }
  289.  
  290. /*
  291.  * Find all UIDs in the named group
  292.  */
  293. static void GetMembers(BASE, struct NetInfoReq *req, struct NetInfoMap *nim)
  294. {
  295.   req->io_Error = NIERR_NOTFOUND;
  296.   TermIO(req);
  297. }
  298.