home *** CD-ROM | disk | FTP | other *** search
- /* $Header: chall.c,v 1.4 89/12/18 18:39:55 brian Locked $ */
-
- /* By Brian E. Litzinger */
-
- #include <stdio.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <grp.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <string.h>
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- int c;
- extern char *optarg;
- extern int optind;
- extern int errno;
-
- struct stat *malloc();
-
- struct passwd *p;
- struct passwd *getpwnam();
-
- struct group *g;
- struct group *getgrnam();
-
- struct stat *f;
- int stat();
-
- char t[256];
- int tlen;
-
- int cmask;
-
- FILE *fd;
-
- char buffer[100];
-
- char *s;
- char *q;
-
- char *home;
-
- short matchfound;
-
- short errflag;
- short dflag;
- short oflag;
- short mflag;
- short gflag;
- short sflag;
- short xflag;
-
- char *directory;
- char *owner;
- int Vowner;
- int OLDowner;
- char *group;
- int Vgroup;
- int OLDgroup;
- int mode;
-
- errflag=gflag=mflag=oflag=dflag=xflag=0;
-
- home=(char *)getenv("HOME");
-
- /*
- +r -r +w -w +x -x +u -u +g -g
- umask((cmask=umask(0));
- */
-
- while ((c=getopt(argc,argv,"x:o:u:g:m:d:")) != -1)
- switch (c) {
- case 'x':
- if (!home) {
- fprintf(stderr,"%s: no HOME variable in environment.",
- argv[0]);
- fprintf(stderr,"%s: Unable to file chall library file",
- argv[0]);
- exit(2);
- }
- matchfound = 0;
- sprintf(buffer,"%s/.chall",home);
- if((fd=fopen(buffer,"r"))!=NULL) {
- while ( !matchfound && fgets(buffer,sizeof(buffer),fd)) {
- q = strtok(buffer,":");
- if (q && !strcmp(optarg,q)) {
- matchfound = -1;
- if (q=strtok(NULL,":"))
- if (!oflag) {
- owner = q;
- oflag++;
- }
- if (q=strtok(NULL,":"))
- if (!gflag) {
- group = q;
- gflag++;
- }
- if (q=strtok(NULL,":"))
- if (!mflag) {
- sscanf(q,"%o",&mode);
- mflag++;
- }
- }
- }
- close(fd);
- if (!matchfound) {
- fprintf(stderr,"%s: '%s' not found in chall library.\n",
- argv[0],optarg);
- exit(2);
- }
- } else {
- fprintf(stderr,
- "%s: Unable to open ~/.chall library file.\n");
- exit(2);
- }
- break;
- case 'o':
- case 'u':
- owner = optarg;
- oflag++;
- break;
- case 'g':
- group = optarg;
- gflag++;
- break;
- case 'm':
- sscanf(optarg,"%o",&mode);
- mflag++;
- break;
- case 'd':
- if (dflag) errflag++;
- else {
- directory = optarg;
- dflag++;
- }
- break;
- default:
- errflag++;
- }
- if (errflag) {
- fprintf(stderr,"Illegal option.\n");
- fprintf(stderr,
- "usage: %s [-o owner] [-g group] [-m mode] [-d directory] files ...\n",
- argv[0]);
- exit(1);
- }
-
- if ( (oflag) && (!(Vowner=atoi(owner))) && ((p=getpwnam(owner))!=NULL) )
- Vowner = p->pw_uid;
-
- if (gflag) {
- Vgroup = atoi(group);
- if (!Vgroup)
- if ((g=getgrnam(group))!=NULL)
- Vgroup = g->gr_gid;
- else
- if ((p=getpwnam(group))!=NULL)
- Vgroup = p->pw_gid;
- }
-
- f = malloc(sizeof(struct stat));
- if (dflag) {
- strcpy(t,directory);
- tlen = strlen(t);
- t[tlen++]='/';
- }
- if ((oflag) || (gflag) || (mflag))
- for ( ; optind<argc; optind++) {
- if ( (dflag) && (argv[optind][0]!='/') ) {
- strcpy((t+tlen),argv[optind]);
- s = t;
- } else
- s = argv[optind];
- if (access(s,F_OK|W_OK)==0) {
- if ( (mflag) && (chmod(s,mode)==-1) )
- fprintf("Unable to chmod %s\n",s);
- sflag=1;
- if ( ((oflag) && (!gflag)) || ((!oflag) && (gflag)) ) {
- if (stat(s,f)!=-1) {
- OLDowner = (int) f->st_uid;
- OLDgroup = (int) f->st_gid;
- }
- else {
- fprintf(stderr,"stat of %s failed(%d)\n",
- s,errno);
- sflag=0;
- }
- }
- if (sflag) {
- if ( (oflag) && (gflag) ) {
- if (chown(s,Vowner,Vgroup)==-1)
- fprintf(stderr,"Unable to chown/grp %s\n",
- s);
- } else if (oflag) {
- if (chown(s,Vowner,OLDgroup)==-1)
- fprintf(stderr,"Unable to chown %s\n",
- s);
- } else if (gflag)
- if (chown(s,OLDowner,Vgroup)==-1)
- fprintf(stderr,"Unable to chgrp %s\n",
- s);
- }
- } else
- fprintf(stderr,"access permission denied on file %s(%d)\n",
- s,errno);
- }
- free(f);
- }
-