SUMMARY: not-ROOT CDROM mounting

From: lombardi emanuele <lele_at_mantegna.casaccia.enea.it>
Date: Wed, 11 Mar 1998 15:50:18 +0100

Dear Gurus,
Here the SUMMARY of my last question concerning the way to
allow normal users to mount their own CD without any root
privilege.

Many people suggested to install sudo which allows authorized
users to have root privileges.

The second suggested way is to make a small program running setuid
to execute the mount command.

I prefer the 2nd way so that I can restrict the freedom of my users
to the minimum (you never know what they can do ...).

So here the C code I wrote. It is named monta which is italian
for mount. It allows both mount and umount of cdrom
Its sintax is as follows:

to mount: monta [-d dev] [-o opts] ... [-o opts] dir
to umount: monta [-d dev] -u
defaults: dev=/dev/rz4c opts=NONE

But, of course, you can easily change the default device setting it
as the first element of devs that is devs[0]

I hope this code helps people,
In any case, please, send me bugs and info and comments...

Thanks to all of you who answered my original question,
Greetings from Italy,

Emanuele

 Emanuele Lombardi
 mail: AMB-GEM-CLIM ENEA Casaccia
        I-00060 S.M. di Galeria (RM)
        ITALY
 mailto:lele_at_mantegna.casaccia.enea.it
 tel +39 6 30483366
 fax +39 6 30483591


/*
monta.c
Program to allow users to mount CDROM without the need to be root.
Users can choose among a list of availabel devices (devs) the first
of which is the default one
See the section code referring the -h switch for the usage of monta.

don't forget to chmod 4555 the executeble!

by EMANUELE LOMBARDI lele_at_mantegna.casaccia.enea.it
*/

#include <stdio.h>

main(argc,argv,en)
int argc;
char *argv[];
char *en[];
{
#define ARGS "o:ud:h"
#define NDEVS 2
#define MAXNS 20

  char *al[MAXNS] = NULL;
  char *devs[NDEVS],*dev;
  int umount,ns,i,c,okdev;
  extern int errno;

  setruid(0);

  devs[0]="/dev/rz4c"; /* default device*/
  devs[1]="/dev/gd0c";
  dev=devs[0];

  al[0] = "/usr/sbin/mount";
  al[1] = "-t";
  al[2] = "cdfs";
  al[3] = "-r" ;
  ns=4;

  umount=0;
  while (((c = getopt(argc, argv, ARGS)) != -1))
    switch (c) {
    case 'd':
      okdev=0;
      for (i=0;i<NDEVS;i++){
        if (strcmp(devs[i],optarg)==0) okdev=1;
      }
      if (okdev==0) {
        fprintf(stderr,"ERROR: device %s NOT available\n",optarg);
        for (i=0;i<NDEVS;i++){fprintf(stderr,"available: %s\n",devs[i]);}
        exit(1);}
      break;
    case 'o':
      if (ns<MAXNS) {
        al[ns]="-o";ns++;al[ns]=optarg;ns++;} else {
          fprintf(stderr,"ERROR too many options!\n");
          exit (1);}
        break;
    case 'u':
      umount=1;
      break;
    case 'h':
      fprintf(stderr,"MOUNTS/UMOUNTS CDrom (-t cdfs -r)\n");
      fprintf(stderr,"to mount: %s [-d dev] [-o opts] ... [-o opts] dir\n",argv[0]);
      fprintf(stderr,"to umount: %s [-d dev] -u\n",argv[0]);
      fprintf(stderr,"defaults: dev=%s opts=NONE\n",dev);
      exit (1);
      break;
    default :
    break;
    }
  if (umount==0) {
    al[ns]=dev;ns++;
    if (argc != optind) {al[ns]=argv[argc-1];}
    else
      {al[ns] = "/cdrom";}
  } else {
    for (i=0;i<=10;i++){al[i]=NULL;}
    al[0]="/usr/sbin/umount";
    al[1]=dev;
    ns=1;}

  for (i=0;i<=ns;i++){fprintf(stderr,"%s ",al[i]);}
  fprintf(stderr,"\n");


  if (execve(al[0],al,en) != 0) {
    fprintf(stderr,"ERROR: errno=%d",errno);
    perror (" ");
    exit(1);
  }
}
Received on Wed Mar 11 1998 - 15:47:34 NZDT

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