home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume10 / chall / chall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-19  |  4.0 KB  |  213 lines

  1. /* $Header: chall.c,v 1.4 89/12/18 18:39:55 brian Locked $ */
  2.  
  3. /* By Brian E. Litzinger */
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <pwd.h>
  8. #include <grp.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <string.h>
  12.  
  13. main(argc,argv)
  14. int argc;
  15. char **argv;
  16. {
  17.     int c;
  18.     extern char *optarg;
  19.     extern int optind;
  20.     extern int errno;
  21.  
  22.     struct stat *malloc();
  23.  
  24.     struct passwd *p;
  25.     struct passwd *getpwnam();
  26.  
  27.     struct group *g;
  28.     struct group *getgrnam();
  29.  
  30.     struct stat *f;
  31.     int stat();
  32.  
  33.     char t[256];
  34.     int tlen;
  35.  
  36.     int cmask;
  37.  
  38.     FILE *fd;
  39.  
  40.     char buffer[100];
  41.  
  42.     char *s;
  43.     char *q;
  44.  
  45.     char *home;
  46.  
  47.     short matchfound;
  48.  
  49.     short errflag;
  50.     short dflag;
  51.     short oflag;
  52.     short mflag;
  53.     short gflag;
  54.     short sflag;
  55.     short xflag;
  56.  
  57.     char *directory;
  58.     char *owner;
  59.     int Vowner;
  60.     int OLDowner;
  61.     char *group;
  62.     int Vgroup;
  63.     int OLDgroup;
  64.     int mode;
  65.  
  66.     errflag=gflag=mflag=oflag=dflag=xflag=0;
  67.  
  68.     home=(char *)getenv("HOME");
  69.  
  70.     /*
  71.     +r -r +w -w +x -x +u -u +g -g
  72.     umask((cmask=umask(0));
  73.     */
  74.  
  75.     while ((c=getopt(argc,argv,"x:o:u:g:m:d:")) != -1)
  76.     switch (c) {
  77.         case 'x':
  78.         if (!home) {
  79.             fprintf(stderr,"%s: no HOME variable in environment.",
  80.                 argv[0]);
  81.             fprintf(stderr,"%s: Unable to file chall library file",
  82.                 argv[0]);
  83.             exit(2);
  84.         }
  85.         matchfound = 0;
  86.         sprintf(buffer,"%s/.chall",home);
  87.         if((fd=fopen(buffer,"r"))!=NULL) {
  88.             while ( !matchfound && fgets(buffer,sizeof(buffer),fd)) {
  89.             q = strtok(buffer,":");
  90.             if (q && !strcmp(optarg,q)) {
  91.                 matchfound = -1;
  92.                 if (q=strtok(NULL,":"))
  93.                 if (!oflag) {
  94.                     owner = q;
  95.                     oflag++;
  96.                 }
  97.                 if (q=strtok(NULL,":"))
  98.                 if (!gflag) {
  99.                     group = q;
  100.                     gflag++;
  101.                 }
  102.                 if (q=strtok(NULL,":"))
  103.                 if (!mflag) {
  104.                     sscanf(q,"%o",&mode);
  105.                     mflag++;
  106.                 }
  107.             }
  108.             }
  109.             close(fd);
  110.             if (!matchfound) {
  111.             fprintf(stderr,"%s: '%s' not found in chall library.\n",
  112.                 argv[0],optarg);
  113.             exit(2);
  114.             }
  115.         } else {
  116.             fprintf(stderr,
  117.                 "%s: Unable to open ~/.chall library file.\n");
  118.             exit(2);
  119.         }
  120.         break;
  121.         case 'o':
  122.         case 'u':
  123.         owner = optarg;
  124.         oflag++;
  125.         break;
  126.         case 'g':
  127.         group = optarg;
  128.         gflag++;
  129.         break;
  130.         case 'm':
  131.         sscanf(optarg,"%o",&mode);
  132.         mflag++;
  133.         break;
  134.         case 'd':
  135.         if (dflag) errflag++;
  136.         else {
  137.             directory = optarg;
  138.             dflag++;
  139.         }
  140.         break;
  141.         default:
  142.         errflag++;
  143.     }
  144.     if (errflag) {
  145. fprintf(stderr,"Illegal option.\n");
  146. fprintf(stderr,
  147. "usage: %s [-o owner] [-g group] [-m mode] [-d directory] files ...\n",
  148.     argv[0]);
  149.     exit(1);
  150.     }
  151.  
  152.     if ( (oflag) && (!(Vowner=atoi(owner))) && ((p=getpwnam(owner))!=NULL) )
  153.         Vowner = p->pw_uid;
  154.  
  155.     if (gflag) {
  156.     Vgroup = atoi(group);
  157.     if (!Vgroup)
  158.         if ((g=getgrnam(group))!=NULL)
  159.         Vgroup = g->gr_gid;
  160.         else 
  161.         if ((p=getpwnam(group))!=NULL)
  162.             Vgroup = p->pw_gid;
  163.     }
  164.  
  165.     f =  malloc(sizeof(struct stat));
  166.     if (dflag) {
  167.     strcpy(t,directory);
  168.     tlen = strlen(t);
  169.     t[tlen++]='/';
  170.     }
  171.     if ((oflag) || (gflag) || (mflag))
  172.     for ( ; optind<argc; optind++) {
  173.         if ( (dflag) && (argv[optind][0]!='/') ) {
  174.         strcpy((t+tlen),argv[optind]);
  175.         s = t;
  176.         } else
  177.         s = argv[optind];
  178.         if (access(s,F_OK|W_OK)==0) {
  179.         if ( (mflag) && (chmod(s,mode)==-1) )
  180.             fprintf("Unable to chmod %s\n",s);
  181.         sflag=1;
  182.         if ( ((oflag) && (!gflag)) || ((!oflag) && (gflag)) ) {
  183.             if (stat(s,f)!=-1) {
  184.             OLDowner = (int) f->st_uid;
  185.             OLDgroup = (int) f->st_gid;
  186.             }
  187.             else {
  188.             fprintf(stderr,"stat of %s failed(%d)\n",
  189.                 s,errno); 
  190.             sflag=0;
  191.             }
  192.         }
  193.         if (sflag) {
  194.             if ( (oflag) && (gflag) ) {
  195.             if (chown(s,Vowner,Vgroup)==-1)
  196.                 fprintf(stderr,"Unable to chown/grp %s\n",
  197.                     s);
  198.             } else if (oflag) {
  199.             if (chown(s,Vowner,OLDgroup)==-1)
  200.                 fprintf(stderr,"Unable to chown %s\n",
  201.                     s);
  202.             } else if (gflag)
  203.             if (chown(s,OLDowner,Vgroup)==-1)
  204.                 fprintf(stderr,"Unable to chgrp %s\n",
  205.                     s);
  206.         }
  207.         } else 
  208.         fprintf(stderr,"access permission denied on file %s(%d)\n",
  209.             s,errno);
  210.     }
  211.     free(f);
  212. }
  213.