Summary: Re: vquota editing via shellscript ??

From: Max Hadersbeck <max_at_cis.uni-muenchen.de>
Date: Fri, 24 Oct 1997 10:07:27 +0200

The solution is a C-Program or Perlscript :

=====================================
The shell/perl script is from :
Martyn Johnson maj_at_cl.cam.ac.uk
University of Cambridge Computer Lab
Cambridge UK

#!/bin/sh

usage="$0 [-g] filesystem user softblock hardblock softinode hardinode"

case $1 in
-g) flags=-g; shift;;
esac

if [ $# != 6 ]
then echo $usage
        exit 1
fi

fs=$1
user=$2
sb=$3
hb=$4
si=$5
hi=$6

tmp=/tmp/edq$$

echo > $tmp "#!/usr/bin/perl
\$infile = \$ARGV[0];
\$outfile = \$infile . 'new';

open (IN, \$infile) || die ('failed to open input');
open (OUT, '>' . \$outfile) || die ('failed to open output');


while(<IN>) {
        if (m!$fs\\\\b!) {
                if (/^fs/) {
                        s/blocks.*\)\$/blocks (soft = $sb, hard = $hb)
inodes (soft = $si, hard = $hi)/;
                } else {
                        s/\(soft.*\)$/(soft = $sb, hard = $hb)/;
                        print OUT;
                        \$_ = <IN>;
                        s/\(soft.*\)$/(soft = $si, hard = $hi)/;
                }
        }
        print OUT;
}

close IN;
close OUT;

system('cp ' . \$outfile . ' ' . \$infile);
unlink \$outfile;
"

chmod 700 $tmp
PATH=/bin:/usr/bin:/etc:/usr/etc:/usr/sbin
if [ -f /usr/sbin/vedquota ]
then edquota=vedquota
else edquota=edquota
fi
EDITOR=$tmp $edquota $flags $user
rm $tmp
========================================================================

The C-Program is from Richard Jackson and works very good,
Compile it with
/* To build under OSF/1 3.x: */
/* 1. cc -o setquota setquota.c */
/* 2. strip setquota */


/**********************************************************************/
/* */
/* NAME: setquota.c */
/* */
/* PROGRAMMER: Richard Jackson DATE: 950328 */
/* */
/* PURPOSE: Set the quota for a specified user on a specified */
/* filesystem. */
/* */
/* NOTES: */
/* To execute: */
/* 1. setquota username softlimit hardlimit filesystem */
/* for example, */
/* ./setquota jblow 2000 2500 /var/spool/mail */
/* */
/* To build under OSF/1 3.x: */
/* 1. cc -o setquota setquota.c */
/* 2. strip setquota */
/* */
/* Only root or authorized users can successfully run this program. */
/* Authorized users are anybody in group adm */
/* */
/* */
/* MODIFICATION HISTORY: */
/* DATE MOD NAME DESCRIPTION */
/* */
/* 950328 AAA rjackson initial version */
/* 960214 Nick Hill Changed name to setquota and add tests */
/* for authorized non root users to set */
/* quotas */
/* */
/**********************************************************************/

#include <stdio.h> /* printf() */
#include <pwd.h> /* getpwnam() */
#include <stdlib.h> /* atoi(), exit() */
#include <sys/types.h>
#include <ufs/quota.h> /* quotactl() */
#include <sys/mount.h> /* statfs() */
#include <errno.h> /* perror() */
#include <unistd.h> /* geteuid() */
#include <grp.h> /* getgrnam(),
endgrent() */
#include <string.h> /* strcpy(), strcmp() */

main (int argc, char *argv[])
{
  struct dqblk dqblk_st; /* disk quota structure */
  struct passwd *pw; /* passwd file entry structure
*/
  u_int softlimit; /* quota soft limit */
  u_int hardlimit; /* quota hard limit */
  struct statfs statfs_st; /* mounted filesystem info */
  uid_t uid; /* user id */
  char user_running[31]; /* username running the
program*/
  char progname[31]; /* program name */
  struct group *gstruct; /* group entry structure */
  int authorize=0; /* allow user to do it? 0=no,
1=yes */
  char *ptr, buffer[255];
  char **ptr2;
  
 strcpy(buffer,argv[0]);
 ptr=strrchr(buffer,'/');
 if (ptr==NULL)
   {strncpy(progname,buffer,30);
    progname[30] = '\0';
   }
 else
   {strncpy(progname,++ptr,30);
    progname[30]='\0';
   }

/*
 * make sure we have the required arguments
 */
  if (argc != 5)
  {
    fprintf(stderr, "Usage: %s username softlimit hardlimit
filesystem\n",progna
me);
    fprintf(stderr, " :\n");
    fprintf(stderr, " : soft and hard limits are in units of 1024 as
reporte
d\n");
    fprintf(stderr, " : by other Advfs utilities. Eg 204800 is
200Mb\n");
    exit(1);
  }

/*
 * this program requires root privs
 */
  if ( geteuid() != (uid_t) 0)
  {
    fprintf(stderr, "%s: you do not have authorization to use %s\n",
progname,pr
ogname);
    exit(1);
  }

/*
 * determine who is running me
 */
 
    uid = getuid();
    if ((pw = getpwuid(uid)) == (struct passwd *) NULL)
      {
       fprintf(stderr, "%s: can't find passwd entry for uid %d\n",
progname, uid
);
       exit(1);
      }
   
    strcpy(user_running,pw->pw_name);
/*
 * check user running against membership of group adm
 */
 
  if ((gstruct= getgrnam("adm")) == NULL )
    {fprintf(stderr,"%s: failed to get group entry for
authentication\n",prognam
e);
     exit(1);
    }

  ptr2=gstruct->gr_mem;
  while (ptr2[0] != NULL)
    {if (!strcmp(user_running,ptr2[0]))
       {authorize=1;
        break;
       }
     ptr2++;
    }
  endgrent();
  
  if ((authorize == 0) && strcmp(user_running,"root"))
    {fprintf(stderr,"%s: User %s not authorized to use
%s\n",progname,user_runni
ng,progname);
     exit(1);
    }
  
/*
 * does this user exist in the /etc/passwd file?
 */
  if ((pw = getpwnam(argv[1])) == (struct passwd *) NULL)
  {
    fprintf(stderr, "%s: can't find passwd entry for %s\n", argv[0],
argv[1]);
    exit(1);
  }

/*
 * check if quota's are valid for the specified filesystem
 */
  if (statfs(argv[4], &statfs_st, sizeof(struct statfs)))
  {
    fprintf(stderr, "%s: statfs() failed, errno=%d\n", argv[0], errno);
    exit(1);
  }

/*
 * Filesystem should manage quotas or be AdvFS (i.e., AdvFS always
 * manages quotas). M_QUOTA appears to not be set for AdvFS.
 */
  if (!((statfs_st.f_flags & M_QUOTA) || (statfs_st.f_type ==
MOUNT_MSFS)))
  {
    fprintf(stderr, "%s: quota is not enabled for %s filesystem\n",
         argv[0], argv[4]);
    exit(1);
  }

/*
 * fetch current values
 */
  if (quotactl(argv[4], QCMD(Q_GETQUOTA, USRQUOTA), pw->pw_uid,
&dqblk_st))
  {
    fprintf(stderr, "%s: quotactl() GETQUOTA failed, errno=%d\n",
         argv[0], errno);
    exit(1);
  }
  
  fprintf(stdout,"\n%s : Setting quotas for user %s\n",progname,
pw->pw_name);
  fprintf(stdout,"%s : Old softlimit in %s is
%d\n",progname,argv[4],dqblk_st.dq
b_bsoftlimit/2);
  fprintf(stdout,"%s : Old hardlimit in %s is
%d\n",progname,argv[4],dqblk_st.dq
b_bhardlimit/2);
  

  if((atoi(argv[2]) < 0) || (atoi(argv[3]) < 0))
    {fprintf(stderr,"%s : invalid negative value given for new soft or
hard limi
t\n",progname);
     exit(1);
    }
    
    
  softlimit = atoi(argv[2]);
  hardlimit = atoi(argv[3]);
/*
 * disk blocks (512 bytes each) times 2. vquota reports number of 1024
 * byte blocks.
 */
  dqblk_st.dqb_bsoftlimit = softlimit * 2;
  dqblk_st.dqb_bhardlimit = hardlimit * 2;
  
  fprintf(stdout,"%s : New softlimit in %s is
%d\n",progname,argv[4],dqblk_st.dq
b_bsoftlimit/2);
  fprintf(stdout,"%s : New hardlimit in %s is
%d\n\n",progname,argv[4],dqblk_st.
dqb_bhardlimit/2);


/*
 * set new quotas
 */
  if (quotactl(argv[4], QCMD(Q_SETQUOTA, USRQUOTA), pw->pw_uid,
&dqblk_st))
  {
    fprintf(stderr, "%s: quotactl() SETQUOTA failed, errno=%d\n",
         argv[0], errno);
    exit(1);
  }

  exit(0);

}
Received on Fri Oct 24 1997 - 10:33:11 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:37 NZDT