Subject: Re: vipw done. Now what?
To: None <netbsd-help@NetBSD.ORG>
From: James Burton <james@Snark.apana.org.au>
List: netbsd-help
Date: 01/07/1996 13:01:42
Hi Giles,
> I decided I'd post something. Here is James' script, translated to sh.

I should have tidied it up before I posted it. But it makes SO many
assumptions that I couldn't be bothered. If anybody finds it useful that's
great. Here is the rest of the stuff that it requires. For God's sake
make sure that these are root-only if you let other people onto your
system. No suid crap either.

> Perhaps, James, you could post the missing utilities (make-password,
> fix_skeleton_file, setquota) along with your set of skeleton files?

Now by memory you need a group "users" a directory /home/users and it is set
up to claim uids after 200 for new users. It will skip any pre-used uids
but I like things tidy :-) Also I have hard coded paths to other files for some reason.
Lazyness perhaps. SO look over the scripts before you use them.

James

----- fix_skeleton_file
#!/bin/csh

if ($#argv != 4) then
      echo "error: incorrect arguments"
      echo "usage: fix_skeleton_file <file> <username> <realname> <hostname>"
      exit 20
endif

set realname="$3"
set email="$2@$4"
set username="$2"
set homepage="http:\/\/$4\/~$2\/"
set host="$4"

cat $1 | sed -e "s/%real/$realname/" -e "s/%email/$email/" -e "s/%user/$username/" -e "s/%www/$homepage/" -e "s/%host/$host/" > $1.tmp
rm $1
mv $1.tmp $1

## End of File


-----setquota.c
#include <stdio.h>
#include <ufs/ufs/quota.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>

#ifndef TRUE
#define TRUE  1
#define FALSE 0
#endif

print_usage()
{
      printf("Usage: setquota [[-u] user] [-g group] [-b soft hard] [-i soft hard] -p path\n");
      printf("\n\t-b\tset soft & hard limits for blocks\n");
      printf("\t-i\tset soft & hard limits for inodes\n");
}

int userid(char *arg,int type)
{
        struct passwd *pw;

      if (isdigit(arg[0]))
              return(atoi(arg));
      else
      {       /* look up id in passwd or group file */
              pw = getpwnam(arg);
              return(pw->pw_uid);
      }
}

main(int argc, char *argv[])
{
      char *path = NULL;
      int id = 0;
      struct dqblk dq;
      int cmd_type = USRQUOTA;
      int i;
      int setbq = FALSE, setiq = FALSE;
      unsigned long int bsoft, bhard, isoft, ihard;

      for (i=1; i<argc; i++)
      {
              if (argv[i][0] == '-')
              {       /* a flag */
                      switch (argv[i][1])
                      {
                              case 'u':       /* set user id */
                                      if ((argc - i) < 2)
                                      {
                                              fprintf(stderr,"ERROR: username or id not specified\n");
                                              print_usage();
                                              exit(20);
                                      }
                                      else
                                      {
                                              cmd_type = USRQUOTA;
                                              id = userid(argv[++i],cmd_type);
                                      }
                                      break;
                              case 'g':       /* set group id */
                                      if ((argc - i) < 2)
                                      {
                                              fprintf(stderr,"ERROR: groupname or id not specified\n");
                                              print_usage();
                                              exit(20);
                                      }
                                      cmd_type = GRPQUOTA;
                                      id = userid(argv[++i],cmd_type);
                                      break;
                              case 'b':       /* set blocks soft hard */
                                      setbq = TRUE;
                                      if ((argc - i) < 3)
                                      {
                                              fprintf(stderr,"ERROR: soft and hard block limits not supplied\n");
                                              print_usage();
                                              exit(20);
                                      }
                                      bsoft = atoi(argv[++i]);
                                      bhard = atoi(argv[++i]);
                                      break;
                              case 'i':
                                      setiq = TRUE;
                                      if ((argc - i) < 3)
                                      {
                                              fprintf(stderr,"ERROR: soft and hard inode limits not supplied\n");
                                              print_usage();
                                              exit(20);
                                      }
                                      isoft = atoi(argv[++i]);
                                      ihard = atoi(argv[++i]);
                                      break;
                              case 'p':
                                      if ((argc - i) < 2)
                                      {
                                              fprintf(stderr,"ERROR: no path specified\n");
                                              print_usage();
                                              exit(20);
                                      }
                                      path = argv[++i];
                                      break;
                              default:
                                      fprintf(stderr,"ERROR: unknown flag '%c'\n",argv[i][1]);
                                      print_usage();
                                      exit(20);
                                      break;
                      }
              }
              else
              {
                      id = userid(argv[i],cmd_type);
              }
      }

      if (path == NULL)
      {
              fprintf(stderr,"ERROR: must specify path\n");
              print_usage();
              exit(20);
      }

      if (setbq || setiq)
      {       /* change the limits */
              printf("Setting quota limits...\n");
              /* load the data structure with current settings */
              if (quotactl(path,QCMD(Q_GETQUOTA,cmd_type),id,&dq) != 0)
              {
                      fprintf(stderr,"ERROR: could not get quota settings\n");
                      exit(20);
              }
              /* are we going to change the block quota limits? */
              if (setbq)
              {
                      dq.dqb_bhardlimit = bhard;
                      dq.dqb_bsoftlimit = bsoft;
              }
              /* are we going to change the inode quota limits? */
              if (setiq)
              {
                      dq.dqb_ihardlimit = ihard;
                      dq.dqb_isoftlimit = isoft;
              }
              /* attempt to make the requested changed */
              if (quotactl(path,QCMD(Q_SETQUOTA,cmd_type),id,&dq) != 0)
              {
                      fprintf(stderr,"ERROR: could not set quota limits\n");
                      exit(20);
              }
              if (quotactl(path,QCMD(Q_SYNC,cmd_type),0,NULL) != 0)
              {
                      fprintf(stderr,"ERROR: could not sync quota database\n");
              }

              /* clear so we believe the GETQUOTA results */
              dq.dqb_bhardlimit = 0;
              dq.dqb_bsoftlimit = 0;
              dq.dqb_ihardlimit = 0;
              dq.dqb_isoftlimit = 0;
      }

      /* fetch current settings */
      if (quotactl(path,QCMD(Q_GETQUOTA,cmd_type),id,&dq) != 0)
      {
              fprintf(stderr,"ERROR: could not get quota settings\n");
              exit(20);
      }
      /* print current settings */
      if (cmd_type == USRQUOTA)
              printf("UID=%d\n",id);
      else
              printf("GID=%d\n",id);
      printf("Blocks: SOFT=%d,HARD=%d\nInodes: SOFT=%d,HARD=%d\n",dq.dqb_bsoftlimit,dq.dqb_bhardlimit,dq.dqb_isoftlimit,dq.dqb_ihardlimit);


      return(0);
}


-----make-password.c
#include <stdio.h>


/* compile : cc make-password.c -o make-password -lcrypt  */

/* characters used to encode 6 bits */
char *pick = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
";
 
main(int argc, char *argv[])
{
char first[16],second[16],salt[3];
 
  /* sort out arguments */
  if (argc != 2)
  {
      fprintf(stderr,"incorrect arguments\nusage: make-password mumbo-jumbo\n");
      exit(20);
  }

  /* pick 16 chars from argv[1] */
  strncpy(first,argv[1],16);
  first[15] = '\0';

  /* pick random salt */
  srand(time(0));
  salt[0]= pick[(rand()>>10)&0x2f];
  salt[1]= pick[(rand()>>10)&0x2f];
  salt[2]='\0'; /* not necessary */
 
  /* output */
  printf("%s\n",crypt(first,salt));
  return(0);
}




-- 
James Burton, Melbourne, Australia.|Every jumbled pile of person has a
James@Snark.apana.org.au           |thinking part that wonders what the 
http://Snark.APANA.org.au/james/   |part that isn't thinking isn't thinking of