📄 growisofs.c
字号:
* this means that if installed set-root-uid, growisofs grants * access to DVD burner(s), but not to any other device. */ for (once=1;1;once--) { memset (&ccb,0,sizeof(ccb)); ccb.ccb_h.func_code = XPT_GDEVLIST; if (ioctl (in_fd,CAMGETPASSTHRU,&ccb) < 0) { if (errno==ENXIO && once) { if (ioctl (in_fd,CDIOCCLOSE)==0) continue; } fprintf (stderr,":-( unable to CAMGETPASSTHRU for %s: ",device), perror (NULL), exit (FATAL_START(errno)); } break; } sprintf (pass,"/dev/%.15s%u",ccb.cgdl.periph_name,ccb.cgdl.unit_number); cam = cam_open_pass (pass,O_RDWR,NULL); if (cam == NULL) { fprintf (stderr,":-( unable to cam_open_pass(\"%s\",O_RDWR): ",pass), perror (NULL); exit (FATAL_START(errno)); } ioctl_handle = (void *)cam; mmc_profile = get_mmc_profile (ioctl_handle); switch (mmc_profile&0xFFFF) { case 0x11: /* DVD-R */ case 0x13: /* DVD-RW Restricted Overwrite */ case 0x14: /* DVD-RW Sequential */ case 0x1A: /* DVD+RW */ case 0x1B: /* DVD+R */ case 0x2B: /* DVD+R Double Layer */ open_poor_man: poor_man = 1; out_fd = dup(in_fd); /* it's ignored in poor_man ... */ setuid (uid); /* drop all privileges */ return ioctl_device=cam->device_path; case 0x12: /* DVD-RAM */ if (poor_man>0) goto open_poor_man; break; default: fprintf (stderr,":-( %s: media is not recognized as " "recordable DVD: %X\n",device,mmc_profile); exit (FATAL_START(EMEDIUMTYPE)); } setuid(uid); goto open_rw;}#elif defined(__sun) || defined(sun)#include <volmgt.h>#include <sys/cdio.h>char *setup_fds (char *device){ uid_t uid=getuid(); struct stat64 sb,sc; int v; if ((v=volmgt_running())) { char *file=NULL,*volname; /* * I leak some memory here, but I don't care... */ if ((volname=volmgt_symname (device))) file=media_findname (volname); else file=media_findname (device); if (file) device=file; else v=0; } /* * We might be entering as euid=root! */ if ((in_fd = open64 (device,O_RDONLY)) < 0) if (errno != ENXIO || (in_fd = open64 (device,O_RDONLY|O_NONBLOCK)) < 0) fprintf (stderr,":-( unable to open(\"%s\",O_RDONLY): ",device), perror (NULL), exit (FATAL_START(errno)); if (fstat64 (in_fd,&sb) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",device), perror (NULL), exit (FATAL_START(errno)); if (!S_ISCHR(sb.st_mode)) { char *s; if (S_ISBLK(sb.st_mode) && (s=strstr (device,"/dsk/"))) { fprintf (stderr,":-) you most likely want to use %.*s/r%s instead!\n", (int)(s-device),device,s+1); if (isatty(0) && !dry_run) poll(NULL,0,5000); } setuid(uid); /* drop all privileges */ close (in_fd); /* reopen as mortal */ if ((in_fd = open64 (device,O_RDONLY)) < 0) fprintf (stderr,":-( unable to reopen(\"%s\",O_RDONLY): ",device), perror (NULL), exit (FATAL_START(errno)); open_rw: if ((out_fd = open64 (device,O_RDWR)) < 0) fprintf (stderr,":-( unable to open(\"%s\",O_RDWR): ",device), perror (NULL), exit (FATAL_START(errno)); if (fstat64(out_fd,&sc) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",device), perror (NULL), exit (FATAL_START(errno)); if (sb.st_dev!=sc.st_dev || sb.st_ino!=sc.st_ino) fprintf (stderr,":-( %s: race condition detected!\n",device), exit(FATAL_START(EPERM)); poor_man = 0; close (ioctl_fd); ioctl_handle = INVALID_HANDLE; setuid (uid); /* drop all privileges */ return device; } /* * Still as euid=root! But note that get_mmc_profile makes sure it's * an MMC device, as it terminates the program if the unit doesn't * reply to GET CONFIGURATON. In combination with following switch * this means that if installed set-root-uid, growisofs grants * access to DVD burner(s), but not to any other device. */ ioctl_handle = (void *)(long)dup (in_fd);#if 0 if (ioctl(ioctl_handle,CDROMSTART)<0 && errno==ENXIO) media_load(ioctl_handle);#endif mmc_profile = get_mmc_profile (ioctl_handle); switch (mmc_profile&0xFFFF) { case 0x11: /* DVD-R */ case 0x12: /* DVD-RAM */ case 0x13: /* DVD-RW Restricted Overwrite */ case 0x14: /* DVD-RW Sequential */ case 0x1A: /* DVD+RW */ case 0x1B: /* DVD+R */ case 0x2B: /* DVD+R Double Layer */ poor_man = 1; out_fd = dup(ioctl_fd);#if 0 /* 'man uscsi' maintains that root privileges are required upon * issue of USCSI ioctl, we therefore can't drop them... */ setuid (uid); /* drop all privileges */#endif return ioctl_device=device; default: fprintf (stderr,":-( %s: media is not recognized as " "recordable DVD: %X\n",device,mmc_profile); exit (FATAL_START(EMEDIUMTYPE)); } setuid(uid); goto open_rw;}#elif defined(__hpux)#ifdef SCTL_MAJOR# define SCTL_SANITY_CHECK SCTL_MAJOR-1# if SCTL_SANITY_CHECK<=0# undef SCTL_MAJOR# endif# undef SCTL_SANITY_CHECK#endif#ifndef SCTL_MAJOR#error "SCTL_MAJOR is undefined or not sane."#endif#include <sys/mknod.h>#include <sys/diskio.h>#define seteuid(x) setreuid((uid_t)-1,x)#if 1#define CANNOT_PASS_DEV_FD_N_TO_MKISOFS#elif 0--- ./multi.c.orig Wed Dec 25 15:15:24 2002+++ ./multi.c Tue Nov 11 17:12:27 2003@@ -1067,3 +1067,13 @@ open_merge_image(path) char *path; {+ int fd;++ if (sscanf (path,"/dev/fd/%u",&fd) == 1) {+ int fdd = dup(fd); /* validate file descriptor */+ if (fdd < 0) return -1;+ close (fdd);+ in_image = fdopen (fd,"rb");+ return in_image ? 0 : -1;+ }+#endif#ifdef CANNOT_PASS_DEV_FD_N_TO_MKISOFSchar *get_M_parm (int fd, char *device){ struct stat sb; dev_t m; char *ret=device; static char ctl[16]; if (fstat (fd,&sb)==0 && S_ISCHR(sb.st_mode)) { m = minor (sb.st_rdev); sprintf (ctl,"%d,%d,%d",(m>>16)&0xFF,(m>>12)&0xF,(m>>8)&0xF); ret = ctl; } return ret;}#endifchar *setup_fds (char *device){ uid_t uid=getuid(); struct stat64 sb,sc; dev_t m; static char rscsi [32]; disk_describe_type ddt; /* * We might be entering as euid=root! */ if ((in_fd = open64 (device,O_RDONLY)) < 0) fprintf (stderr,":-( unable to open(\"%s\",O_RDONLY): ",device), perror (NULL), exit (FATAL_START(errno)); if (fstat64 (in_fd,&sb) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",device), perror (NULL), exit (FATAL_START(errno)); if (!S_ISCHR(sb.st_mode)) { char *s; if (S_ISBLK(sb.st_mode) && (s=strstr (device,"/dsk/"))) { fprintf (stderr,":-) you most likely want to use %.*s/r%s instead!\n", (int)(s-device),device,s+1); if (isatty(0) && !dry_run) poll(NULL,0,5000); } setuid(uid); /* drop all privileges */ close (in_fd); /* reopen as mortal */ if ((in_fd = open64 (device,O_RDONLY)) < 0) fprintf (stderr,":-( unable to reopen(\"%s\",O_RDONLY): ",device), perror (NULL), exit (FATAL_START(errno)); open_rw: if ((out_fd = open64 (device,O_RDWR)) < 0) fprintf (stderr,":-( unable to open(\"%s\",O_RDWR): ",device), perror (NULL), exit (FATAL_START(errno)); if (fstat64(out_fd,&sc) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",device), perror (NULL), exit (FATAL_START(errno)); if (sb.st_dev!=sc.st_dev || sb.st_ino!=sc.st_ino) fprintf (stderr,":-( %s: race condition detected!\n",device), exit(FATAL_START(EPERM)); poor_man = 0; close (ioctl_fd); ioctl_handle = INVALID_HANDLE; setuid (uid); /* drop all privileges */ return device; } /* * Still as euid=root! But note that get_mmc_profile makes sure it's * an MMC device, as it terminates the program if the unit doesn't * reply to GET CONFIGURATON. In combination with following switch * this means that if installed set-root-uid, growisofs grants * access to DVD burner(s), but not to any other device. */ m=minor(sb.st_rdev); /* Make sure user isn't using /dev/rscsi/cXtYlZ... */#if 1 if (ioctl (in_fd,DIOC_DESCRIBE,&ddt) != 0)#else if (major(sb.st_rdev) == SCTL_MAJOR)#endif fprintf (stderr,":-( stick to /dev/rdsk/c%ut%u%c%x!\n", (m>>16)&0xFF,(m>>12)&0xF,'d',(m>>8)&0xF), exit(FATAL_START(EINVAL)); /* * Even though /dev/rdsk/cXtYdZ accepts SIOC_IO as well, we have to * use /dev/rscsi/cXtYlZ for pass-through access in order to avoid * command replay by upper "class" driver... */ sprintf (rscsi,"/dev/rscsi/c%ut%u%c%x", (m>>16)&0xFF,(m>>12)&0xF,'l',(m>>8)&0xF); ioctl_handle = (void *)(long)open64 (rscsi,O_RDONLY); if (ioctl_handle == INVALID_HANDLE) { fprintf (stderr,":-( unable to open(\"%s\",O_RDONLY): ",rscsi), perror (NULL); if (errno == ENOENT) fprintf (stderr,":-! consider " "'mknod %s c %d 0x%06x; chmod 0600 %s'\n", rscsi,SCTL_MAJOR,(m&0xFFFF00)|2,rscsi); exit (FATAL_START(errno)); } if (fstat64 (ioctl_fd,&sc) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",rscsi), perror (NULL), exit (FATAL_START(errno)); /* Make sure we land on same SCSI ID... */ if ((m&0xFFFF00) != (minor(sc.st_rdev)&0xFFFF00)) fprintf (stderr,":-( SCSI ID mismatch: %06x!=%06x\n", m,minor(sc.st_rdev)), exit(FATAL_START(EPERM)); mmc_profile = get_mmc_profile (ioctl_handle); switch (mmc_profile&0xFFFF) { case 0x11: /* DVD-R */ case 0x12: /* DVD-RAM */ case 0x13: /* DVD-RW Restricted Overwrite */ case 0x14: /* DVD-RW Sequential */ case 0x1A: /* DVD+RW */ case 0x1B: /* DVD+R */ case 0x2B: /* DVD+R Double Layer */ poor_man = 1; out_fd = dup(ioctl_fd);#if 0 /* HP-UX requires root privileges upon SIOC_IO ioctl */ setuid (uid); /* drop all privileges */#endif return ioctl_device=rscsi; default: fprintf (stderr,":-( %s: media is not recognized as " "recordable DVD: %X\n",device,mmc_profile); exit (FATAL_START(EMEDIUMTYPE)); } setuid(uid); goto open_rw;}/* * PA-RISC HP-UX doesn't have /dev/zero:-( */static fd_set _dev_zero;static int open64_zero(const char *pathname, int flags){ int fd; if (strcmp(pathname,"/dev/zero")) return open64 (pathname,flags); else if ((fd=open ("/dev/null",flags)) >= 0) FD_SET (fd,&_dev_zero); return fd; }static ssize_t read_zero (int fd, void *buf, size_t count){ if (!FD_ISSET(fd,&_dev_zero)) return read (fd,buf,count); memset (buf,0,count); return count;}static int close_zero (int fd){ int ret=close (fd); if (ret>=0 && FD_ISSET(fd,&_dev_zero)) FD_CLR (fd,&_dev_zero); return ret;}static int dup2_zero (int oldfd, int newfd){ int ret; ret = dup2 (oldfd,newfd); if (ret >= 0) { if (FD_ISSET(oldfd,&_dev_zero)) FD_SET (ret,&_dev_zero); else FD_CLR (ret,&_dev_zero); } return ret;}#define open64 open64_zero#define read read_zero#define close close_zero#define dup2 dup2_zero#elif defined(__sgi)#include <sys/attributes.h>#include <sys/param.h>#include <mediad.h>char *setup_fds (char *device){ uid_t uid=getuid(); struct stat64 sb,sc; char hw_path[MAXPATHLEN],*s; int hw_len=sizeof(hw_path)-1; int bus=0,tgt=4,lun=0; /* default config for O2 */ static char rscsi[64]; /* * We might be entering as euid=root! */ if ((in_fd = open64 (device,O_RDONLY)) < 0) { if (errno==EIO) goto tray_might_be_open; fprintf (stderr,":-( unable to open(\"%s\",O_RDONLY): ",device), perror (NULL), exit (FATAL_START(errno)); } if (fstat64 (in_fd,&sb) < 0) fprintf (stderr,":-( unable to stat(\"%s\"): ",device), perror (NULL), exit (FATAL_START(errno));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -