* Re: CDROM mounting
1998-04-03 5:00 CDROM mounting Russell D Wilton
@ 1998-04-06 4:00 ` Paul Szabo
0 siblings, 0 replies; 2+ messages in thread
From: Paul Szabo @ 1998-04-06 4:00 UTC (permalink / raw)
To: wilton
[-- Attachment #1: Type: text/plain, Size: 628 bytes --]
Russell D Wilton wrote:
> The person actually using the workstation does not have root
> privileges. He wants to be able to mount a CDROM without my
> assistance.
I use the following C program, which is installed setuid root. (The
compiled
program is in /usr/local/bin/cdmount, with a symlink from cdumount.)
--
[ The email address is munged in the headers and in the lines below, in
an ]
[ attempt to avoid junk (spam) mail. Sorry about the inconvenience
caused. ]
--
Paul Szabo - System Manager // School of Mathematics and
Statistics
psz at maths . usyd . edu . au // University of Sydney, NSW 2006,
Australia
[-- Attachment #2: cdmount.c --]
[-- Type: text/plain, Size: 6495 bytes --]
/*
* Mounts on unmounts a ISO-9660 CD-ROM: essentially
* mount -t cdfs -o noversion /dev/rz4c /cdrom
* or
* umount /cdrom
* Does not take any arguments (so is safe) ...
*
* by Hugues Talbot 10 Jan 1996
* Modified PSz 12 Nov 96
*/
/* Man page (created by PSz on 11 Mar 97)
* Put this into /usr/local/man/man1/cdmount.1 or similar.
* -----
.TH cdmount 1 ""
.ds n 5
.SH NAME
\fBcdmount\fR, \fBcdumount\fR - mounts or unmounts a CD-ROM to /cdrom
.SH SYNOPSIS
\fBcdmount\fR
.br
\fBcdumount\fR
.br
.SH DESCRIPTION
The \fBcdmount\fR command allows a (non-root) user to mount a CD-ROM on
/cdrom. It finds the CD drive device, and works similarly to the command
.PP
\fBmount\fR -t cdfs -o noversion /dev/rz4c /cdrom
.PP
The options used by \fBcdmount\fR are appropriate to the most common CD-ROM
disks, but they may not be suitable in all cases. Please see your system
administrator if you your disk needs different options.
.PP
The \fBcdumount\fR command allows a (non-root) user to unmount a CD-ROM from
/cdrom. It works similarly to the command
.PP
\fBumount\fR /cdrom
.PP
.SH SEE ALSO
\fBmount\fR(8), \fBcdfs\fR(4)
* -----
*/
#include <errno.h>
#include <stdio.h>
#include <sys/mount.h>
#include <cdfs/cdfsmount.h>
#include <sys/types.h>
#include <sys/stat.h>
#define DIR "/cdrom"
char *pgm;
doerror( int res )
{
if (errno == 0 && res != 0) errno = res;
if (errno) { perror(pgm); }
exit(errno);
}
main ( int argc, char *argv[] )
{
int res;
pgm = argv[0];
if ( argc != 1 ) {
fprintf (stderr, "%s: No arguments allowed\n", pgm);
exit(1);
}
if ( strcmp(pgm,"cdmount") == 0 ) {
struct stat stdat;
struct cdfs_args cddat;
char *devs[] = { "/dev/rz4c", "/dev/rz5c", NULL };
char **devp, *dev;
for ( devp = devs; *devp != NULL; devp++ ) {
dev = *devp;
/* printf("Trying device %s\n", dev); */
res = lstat(dev, &stdat);
if ( res || errno ) { errno = 0; continue; }
/* printf("st_rdev is %d = 0x%08x\n", stdat.st_rdev, stdat.st_rdev); */
/*
* Device numbers are 0x0800002 + SCSI_ID * 0x0400
* How to tell a CD drive (as different from a disk)?
* Use #include <sys/devgetinfo.h>; ioctl (fd, DEVGETINFO, ... )?
*/
if ( (stdat.st_rdev&0x0fff00ff) != 0x00800002 ) continue;
/* printf("Using device %s\n", dev); */
cddat.fspec = dev;
cddat.exflags = 0;
cddat.exroot = 0;
cddat.flags = M_NOVERSION /*| M_RRIP*/;
cddat.version = 0;
cddat.default_uid = 999;
cddat.default_gid = 999; /* dummy user by default... */
cddat.default_fmode = 0555;
cddat.default_dmode = 0555;
cddat.map_uid_ct = 0;
cddat.map_uid = NULL;
cddat.map_gid_ct = 0;
cddat.map_gid = NULL;
printf ("Executing: mount -t cdfs -o noversion %s %s\n", dev, DIR);
res = mount(MOUNT_CDFS, DIR,
M_RDONLY | M_NOSUID | M_NODEV,
&cddat);
doerror(res);
}
fprintf (stderr, "%s: No CD-ROM device found\n", pgm);
exit(1);
}
else if ( strcmp(pgm,"cdumount") == 0 ) {
printf ("Executing: umount %s\n", DIR);
res = umount(DIR, 0);
doerror(res);
}
else {
fprintf (stderr, "%s: Only cdmount or cdumount allowed\n", pgm);
exit(1);
}
return errno;
}
/* And another posted solution... maybe I should also use statfs ?
From: klaus@axp4.physik.fu-berlin.de (Wolfram Klaus)
Newsgroups: comp.unix.osf.osf1
Subject: Re: Non root access to CD-ROM
Date: 4 Feb 97 08:22:32 GMT
Organization: Freie Universitaet Berlin
Message-ID: <klaus.855044552@axp4.physik.fu-berlin.de>
References: <Pine.OSF.3.95.970204110412.22916D-100000@cyllene.uwa.edu.au>
Barney Dalton <barneyd@cyllene.uwa.edu.au> writes:
>Hi,
>Is there anyway for a non-root user to mount and read a CD_ROM under
>digital unix 3.2.
>thanks
>barney
Not directly. Since our users here wanted to be able to mount a cdrom
without the sysop I wrote a little program that does the mount. It has
to be installed as suid root, of course. It checks whether something
is mounted on /cdrom (I now found out a much more direct way of
testing this, but was too lazy to change the code. Basically it is
about using getmntinfo instead of statfs). If nothing is
mounted it mounts /dev/rz4c and writes the uid of the caller to a
temporary file. A second call of the program by the same user will
unmount the CDROM. If someone else calls the program while the CDROM
is mounted he"ll be informed that user x is using the cdrom drive.
Here is the code:
---
#include <sys/mount.h>
#include <cdfs/cdfsmount.h>
#include <pwd.h>
#include <stdio.h>
#define FLAG M_RDONLY|M_NOSUID|M_NODEV|M_LOWERCASE|M_DROPVERS|M_RRIP
#define UID_FILE "/tmp/cdrom.uid"
main(int argc, char *argv[])
{
FILE *uid;
struct statfs stat;
statfs( "/cdrom/.", &stat, sizeof(struct statfs) );
if (stat.f_type==MOUNT_CDFS)
{
uid=fopen(UID_FILE,"r");
if( uid!=NULL )
{
uid_t mounted_id; fscanf(uid,"%d",&mounted_id); fclose(uid);
if ( mounted_id==getuid() )
{
if (0==umount("/cdrom",0))
{
unlink(UID_FILE);
printf("CDROM unmounted\n");
exit(-1);
}
else
{
perror(argv[0]);
exit(-2);
}
}
else
{
struct passwd *m_user=getpwuid(mounted_id);
printf("CDROM currently used by %s\n", m_user->pw_name);
exit(1);
}
}
else
{
printf("CDROM currently used by root\n");
exit(1);
}
}
else
{
struct cdfs_args data;
data.fspec="/dev/rz4c";
data.flags=FLAG;
if (0==mount(MOUNT_CDFS, "/cdrom", FLAG, (caddr_t) &data))
{
uid=fopen(UID_FILE,"w"); fprintf(uid,"%d",getuid()); fclose(uid);
printf("CDROM mounted on /cdrom\n");
exit(0);
}
else
{
perror(argv[0]);
exit(2);
}
}
}
---
And here is something like a man page
---
..TH cdrom 1 ""
..ds n 5
..SH NAME
\fBcdrom\fR - mounts or unmounts ISO-cdrom to /cdrom
..SH SYNOPSIS
\fBcdrom\fR
..br
..SH DESCRIPTION
\fBcdrom\fR lets a user mount an ISO-cdrom on /cdrom. If the CDROM-drive is
currently used by another user, you will be informed and no attempt to mount
the CDROM will de made. To unmount the CDROM enter \fBcdrom\fR again. If
the CDROM contains Rockridge extensions, these will be used.
..SH SEE ALSO
\fBmount\fR(8), \fBcdfs\fR(4)
..\"#
..\"# The following sets edit modes for GNU EMACS
..\"# Local Variables:
..\"# mode:nroff
..\"# fill-column:79
..\"# End:
---
--
Wolfram Klaus (klaus@physik.fu-berlin.de) FU Berlin, FB Physik
*/
^ permalink raw reply [flat|nested] 2+ messages in thread