home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume10 / chall < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  11.7 KB

  1. From decwrl!shlump.nac.dec.com!decuac!haven!aplcen!uunet!allbery Tue Jan 30 08:42:57 PST 1990
  2. Article 1290 of comp.sources.misc:
  3. Path: decwrl!shlump.nac.dec.com!decuac!haven!aplcen!uunet!allbery
  4. From: brian@apt.UUCP (Brian Litzinger)
  5. Newsgroups: comp.sources.misc
  6. Subject: v10i025: chall source program submittal
  7. Message-ID: <77160@uunet.UU.NET>
  8. Date: 20 Jan 90 01:16:33 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Organization: APT Technology, Inc.  San Jose, CA,  USA
  11. Lines: 439
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 10, Issue 25
  15. Submitted-by: brian@apt.UUCP (Brian Litzinger)
  16. Archive-name: chall
  17.  
  18. [And again:  ???  ++bsa]
  19.  
  20. The following program is just a useless waste of your time, so I'd
  21. just /bin/rm it and go one with more useful endeavours.
  22.  
  23. I've been accused of posting useless programs in the past, so I think
  24. you should seriously re-consider just tossing this code out.
  25.  
  26. There really are better ways to do these things.  This whole mess
  27. is just a waste of network bandwidth and I'm surprised the moderator
  28. actually let it slip through.
  29.  
  30. If you still want to use this program its a program using getopts that
  31. lets you combine chmod, chown, and chgrp into one command called chall.
  32.  
  33. It doesn't follow any of the BSD combined chown/grp functionality. I'm
  34. a System V'er after all.  It does have one saving grace though, you
  35. can maintain a personal .chall file in your home directory with macros
  36. in it like 'man:bin:bin:444' which means you can say 
  37.     chall -x man foobar
  38. and foobar will be chmod 444 foobar, chown bin foobar, chgrp bin foobar.
  39.  
  40. See the man page for more information.  It's too bad it doesn't 
  41. recursively decend directories, and it's too bad it doesn't use
  42. the +rw ... stuff of BSD.  The latter I actually tried, but all those
  43. different options, there just didn't seem to be away to do it.
  44.  
  45. <>  Brian Litzinger @ APT Technology Inc., San Jose, CA
  46. <>  UUCP:  {apple,sun,pyramid}!daver!apt!brian    brian@apt.UUCP
  47. <>  VOICE: 408 370 9077      FAX: 408 370 9291
  48.  
  49. #! /bin/sh
  50. # This is a shell archive.  Remove anything before this line, then unpack
  51. # it by saving it into a file and typing "sh file".  To overwrite existing
  52. # files, type "sh file -c".  You can also feed this as standard input via
  53. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  54. # will see the following message at the end:
  55. #        "End of shell archive."
  56. # Contents:  README chall.c chall.1
  57. # Wrapped by brian@apt on Wed Jan 17 00:12:56 1990
  58. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  59. if test -f 'README' -a "${1}" != "-c" ; then 
  60.   echo shar: Will not clobber existing file \"'README'\"
  61. else
  62. echo shar: Extracting \"'README'\" \(1300 characters\)
  63. sed "s/^X//" >'README' <<'END_OF_FILE'
  64. Xchall
  65. X
  66. XBy Brian E. Litzinger
  67. X
  68. XYou can do what you wish with this code.  Just don't make a profit
  69. Xoff of it.
  70. X
  71. XThe following program is just a useless waste of your time, so I'd
  72. Xjust /bin/rm it and go one with more useful endeavours.
  73. X
  74. XI've been accused of posting useless programs in the past, so I think
  75. Xyou should seriously re-consider just tossing this code out.
  76. X
  77. XThere really are better ways to do these things.  This whole mess
  78. Xis just a waste of network bandwidth and I'm surprised the moderator
  79. Xactually let it slip through.
  80. X
  81. XIf you still want to use this program its a program using getopts that
  82. Xlets you combine chmod, chown, and chgrp into one command called chall.
  83. X
  84. XIt doesn't follow any of the BSD combined chown/grp functionality. I'm
  85. Xa System V'er after all.  It does have one saving grace though, you
  86. Xcan maintain a personal .chall file in your home directory with macros
  87. Xin it like 'man:bin:bin:444' which means you can say 
  88. X    chall -x man foobar
  89. Xand foobar will be chmod 444 foobar, chown bin foobar, chgrp bin foobar.
  90. X
  91. XSee the man page for more information.  It's too bad it doesn't 
  92. Xrecursively decend directories, and it's too bad it doesn't use
  93. Xthe +rw ... stuff of BSD.  The latter I actually tried, but all those
  94. Xdifferent options, there just didn't seem to be away to do it.
  95. X
  96. END_OF_FILE
  97. if test 1300 -ne `wc -c <'README'`; then
  98.     echo shar: \"'README'\" unpacked with wrong size!
  99. fi
  100. # end of 'README'
  101. fi
  102. if test -f 'chall.c' -a "${1}" != "-c" ; then 
  103.   echo shar: Will not clobber existing file \"'chall.c'\"
  104. else
  105. echo shar: Extracting \"'chall.c'\" \(4143 characters\)
  106. sed "s/^X//" >'chall.c' <<'END_OF_FILE'
  107. X/* $Header: chall.c,v 1.4 89/12/18 18:39:55 brian Locked $ */
  108. X
  109. X/* By Brian E. Litzinger */
  110. X
  111. X#include <stdio.h>
  112. X#include <unistd.h>
  113. X#include <pwd.h>
  114. X#include <grp.h>
  115. X#include <sys/types.h>
  116. X#include <sys/stat.h>
  117. X#include <string.h>
  118. X
  119. Xmain(argc,argv)
  120. Xint argc;
  121. Xchar **argv;
  122. X{
  123. X    int c;
  124. X    extern char *optarg;
  125. X    extern int optind;
  126. X    extern int errno;
  127. X
  128. X    struct stat *malloc();
  129. X
  130. X    struct passwd *p;
  131. X    struct passwd *getpwnam();
  132. X
  133. X    struct group *g;
  134. X    struct group *getgrnam();
  135. X
  136. X    struct stat *f;
  137. X    int stat();
  138. X
  139. X    char t[256];
  140. X    int tlen;
  141. X
  142. X    int cmask;
  143. X
  144. X    FILE *fd;
  145. X
  146. X    char buffer[100];
  147. X
  148. X    char *s;
  149. X    char *q;
  150. X
  151. X    char *home;
  152. X
  153. X    short matchfound;
  154. X
  155. X    short errflag;
  156. X    short dflag;
  157. X    short oflag;
  158. X    short mflag;
  159. X    short gflag;
  160. X    short sflag;
  161. X    short xflag;
  162. X
  163. X    char *directory;
  164. X    char *owner;
  165. X    int Vowner;
  166. X    int OLDowner;
  167. X    char *group;
  168. X    int Vgroup;
  169. X    int OLDgroup;
  170. X    int mode;
  171. X
  172. X    errflag=gflag=mflag=oflag=dflag=xflag=0;
  173. X
  174. X    home=(char *)getenv("HOME");
  175. X
  176. X    /*
  177. X    +r -r +w -w +x -x +u -u +g -g
  178. X    umask((cmask=umask(0));
  179. X    */
  180. X
  181. X    while ((c=getopt(argc,argv,"x:o:u:g:m:d:")) != -1)
  182. X    switch (c) {
  183. X        case 'x':
  184. X        if (!home) {
  185. X            fprintf(stderr,"%s: no HOME variable in environment.",
  186. X                argv[0]);
  187. X            fprintf(stderr,"%s: Unable to file chall library file",
  188. X                argv[0]);
  189. X            exit(2);
  190. X        }
  191. X        matchfound = 0;
  192. X        sprintf(buffer,"%s/.chall",home);
  193. X        if((fd=fopen(buffer,"r"))!=NULL) {
  194. X            while ( !matchfound && fgets(buffer,sizeof(buffer),fd)) {
  195. X            q = strtok(buffer,":");
  196. X            if (q && !strcmp(optarg,q)) {
  197. X                matchfound = -1;
  198. X                if (q=strtok(NULL,":"))
  199. X                if (!oflag) {
  200. X                    owner = q;
  201. X                    oflag++;
  202. X                }
  203. X                if (q=strtok(NULL,":"))
  204. X                if (!gflag) {
  205. X                    group = q;
  206. X                    gflag++;
  207. X                }
  208. X                if (q=strtok(NULL,":"))
  209. X                if (!mflag) {
  210. X                    sscanf(q,"%o",&mode);
  211. X                    mflag++;
  212. X                }
  213. X            }
  214. X            }
  215. X            close(fd);
  216. X            if (!matchfound) {
  217. X            fprintf(stderr,"%s: '%s' not found in chall library.\n",
  218. X                argv[0],optarg);
  219. X            exit(2);
  220. X            }
  221. X        } else {
  222. X            fprintf(stderr,
  223. X                "%s: Unable to open ~/.chall library file.\n");
  224. X            exit(2);
  225. X        }
  226. X        break;
  227. X        case 'o':
  228. X        case 'u':
  229. X        owner = optarg;
  230. X        oflag++;
  231. X        break;
  232. X        case 'g':
  233. X        group = optarg;
  234. X        gflag++;
  235. X        break;
  236. X        case 'm':
  237. X        sscanf(optarg,"%o",&mode);
  238. X        mflag++;
  239. X        break;
  240. X        case 'd':
  241. X        if (dflag) errflag++;
  242. X        else {
  243. X            directory = optarg;
  244. X            dflag++;
  245. X        }
  246. X        break;
  247. X        default:
  248. X        errflag++;
  249. X    }
  250. X    if (errflag) {
  251. Xfprintf(stderr,"Illegal option.\n");
  252. Xfprintf(stderr,
  253. X"usage: %s [-o owner] [-g group] [-m mode] [-d directory] files ...\n",
  254. X    argv[0]);
  255. X    exit(1);
  256. X    }
  257. X
  258. X    if ( (oflag) && (!(Vowner=atoi(owner))) && ((p=getpwnam(owner))!=NULL) )
  259. X        Vowner = p->pw_uid;
  260. X
  261. X    if (gflag) {
  262. X    Vgroup = atoi(group);
  263. X    if (!Vgroup)
  264. X        if ((g=getgrnam(group))!=NULL)
  265. X        Vgroup = g->gr_gid;
  266. X        else 
  267. X        if ((p=getpwnam(group))!=NULL)
  268. X            Vgroup = p->pw_gid;
  269. X    }
  270. X
  271. X    f =  malloc(sizeof(struct stat));
  272. X    if (dflag) {
  273. X    strcpy(t,directory);
  274. X    tlen = strlen(t);
  275. X    t[tlen++]='/';
  276. X    }
  277. X    if ((oflag) || (gflag) || (mflag))
  278. X    for ( ; optind<argc; optind++) {
  279. X        if ( (dflag) && (argv[optind][0]!='/') ) {
  280. X        strcpy((t+tlen),argv[optind]);
  281. X        s = t;
  282. X        } else
  283. X        s = argv[optind];
  284. X        if (access(s,F_OK|W_OK)==0) {
  285. X        if ( (mflag) && (chmod(s,mode)==-1) )
  286. X            fprintf("Unable to chmod %s\n",s);
  287. X        sflag=1;
  288. X        if ( ((oflag) && (!gflag)) || ((!oflag) && (gflag)) ) {
  289. X            if (stat(s,f)!=-1) {
  290. X            OLDowner = (int) f->st_uid;
  291. X            OLDgroup = (int) f->st_gid;
  292. X            }
  293. X            else {
  294. X            fprintf(stderr,"stat of %s failed(%d)\n",
  295. X                s,errno); 
  296. X            sflag=0;
  297. X            }
  298. X        }
  299. X        if (sflag) {
  300. X            if ( (oflag) && (gflag) ) {
  301. X            if (chown(s,Vowner,Vgroup)==-1)
  302. X                fprintf(stderr,"Unable to chown/grp %s\n",
  303. X                    s);
  304. X            } else if (oflag) {
  305. X            if (chown(s,Vowner,OLDgroup)==-1)
  306. X                fprintf(stderr,"Unable to chown %s\n",
  307. X                    s);
  308. X            } else if (gflag)
  309. X            if (chown(s,OLDowner,Vgroup)==-1)
  310. X                fprintf(stderr,"Unable to chgrp %s\n",
  311. X                    s);
  312. X        }
  313. X        } else 
  314. X        fprintf(stderr,"access permission denied on file %s(%d)\n",
  315. X            s,errno);
  316. X    }
  317. X    free(f);
  318. X}
  319. END_OF_FILE
  320. if test 4143 -ne `wc -c <'chall.c'`; then
  321.     echo shar: \"'chall.c'\" unpacked with wrong size!
  322. fi
  323. # end of 'chall.c'
  324. fi
  325. if test -f 'chall.1' -a "${1}" != "-c" ; then 
  326.   echo shar: Will not clobber existing file \"'chall.1'\"
  327. else
  328. echo shar: Extracting \"'chall.1'\" \(2557 characters\)
  329. sed "s/^X//" >'chall.1' <<'END_OF_FILE'
  330. X.TH CHALL 1 09/26/88 "APT Technology, Inc."
  331. X.SH NAME
  332. Xchall \- a replacement for chmod, chgrp, and chmod
  333. X.SH SYNOPSIS
  334. X.B chall
  335. X[
  336. X.B \-o|-u \0owner
  337. X] [
  338. X.B \-g \0group
  339. X] [
  340. X.B \-m \0mode
  341. X] [
  342. X.B \-d\0directory
  343. X] [
  344. X.B \-x \0challdef
  345. X] files ...
  346. X.SH DESCRIPTION
  347. X.I chall
  348. Xis a replacement for the individual commands chmod, chown, and chgrp.
  349. X.PP
  350. XThe
  351. X.I chall
  352. Xprogram has the following five options:
  353. X.PP
  354. X.TP
  355. X.B \-o|-u owner
  356. XSpecifies the new owner to be applied to the list of files.
  357. XIf owner is not a number (uid),
  358. X.I /etc/passwd
  359. Xis searched to translate owner into a uid.
  360. X.I Chall
  361. Xalso allows the user to specify -u instead of -o if the user is more
  362. Xcomfortable with that notation.
  363. X.TP
  364. X.B \-g group
  365. XSpecifies the new group to be applied to the list of files. If group
  366. Xis not a number (gid), 
  367. X.I /etc/group
  368. Xis first searched to translate group into a gid.  If that fails
  369. X.I /etc/passwd
  370. Xis then searched to translate group into a gid.
  371. X.TP
  372. X.B \-m mode
  373. XSpecifies the new mode to be applied to the list of files. Note that
  374. Xthe +x -x +w -w +r -r options of some chmods is not support by
  375. X.I chall.
  376. X.TP
  377. X.B \-d directory
  378. X.I directory
  379. Xis prepended to all file names in the file list that do not begin
  380. Xwith
  381. X.I \/
  382. X(root).
  383. X.TP
  384. X.B \-x challdef
  385. X.I Chall
  386. Xsearches the file
  387. X.I $HOME/.chall
  388. Xlooking for a line which begins with challdef and uses the entries
  389. Xin the rest of the line as the owner, group, and mode to be applied
  390. Xby the 
  391. X.I Chall
  392. Xprogram.  Options fetched from the
  393. X.I $HOME/.chall
  394. Xfile are overridden by explict -o, -u, -g, or -m options in the
  395. Xargument line.
  396. X.SH DIAGNOSTICS
  397. XPermissions and access rights are checked at every operation. Any
  398. Xerrors will be reported on a per file basis.
  399. X.I Chall
  400. Xwill make all the changes possible skipping over errors when possible.
  401. X.SH IDENTIFICATION
  402. XAuthor: Brian Litzinger
  403. X.br
  404. XAPT Technology, Inc., San Jose, CA 95030
  405. X.sp 0
  406. X.SH FILES
  407. X$HOME/.chall
  408. X.SH ENVIROMENT
  409. XHOME
  410. X.SH SEE ALSO
  411. Xchmod(2), chown(2), chgrp(2)
  412. X.SH NOTES
  413. XThe
  414. X.I $HOME/.chall file is uses the following form, and can be
  415. Xcreated with any standard text editor:
  416. X.br
  417. X.ti +0.25i
  418. Xchalldef:owner:group:mode
  419. X.br
  420. XAn example
  421. X.I $HOME/.chall
  422. Xfile might be:
  423. X.br
  424. X.in +0.25i
  425. X.nf
  426. Xroot:root:sys:755
  427. Xbin:bin:bin:555
  428. Xrootuser:root::
  429. Xsysgroup::sys:
  430. Xme:brian::
  431. X.fi
  432. X.in -0.25i
  433. X.SH BUGS
  434. XThe +x -x +w -w +r -r syntax of some chmod commands in not supported
  435. Xby 
  436. X.I
  437. Xchall.
  438. X.br
  439. X.I chall
  440. Xcannot be applied to the executing image of itself as it will get
  441. Xa text file busy error (ETXTBSY) from the operating system.
  442. X.br
  443. XWhen using the -d option resultant file names must not exceed 255
  444. Xcharacters.
  445. END_OF_FILE
  446. if test 2557 -ne `wc -c <'chall.1'`; then
  447.     echo shar: \"'chall.1'\" unpacked with wrong size!
  448. fi
  449. # end of 'chall.1'
  450. fi
  451. echo shar: End of shell archive.
  452. exit 0
  453.  
  454.  
  455.