home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
cowen_tools.lzh
/
owner.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-06
|
6KB
|
302 lines
/*
** OWNER
** changes the ownership of files using the Cumana Password System
** owner <newgroup>.<newowner> <file> <file> ...
** options are:
** -? = report usage
**
** The password file is looked for on the following
** /DD/SYS/PASSWORD
** /H0/SYS/PASSWORD
** /D0/SYS/PASSWORD
**
**
** copyright 1991 by Cowen Software Ltd
** 23 Bristol Ave, Levenshulme, Manchester,
** GB-M19 3NU, England, UK
** released to the public domain for non-commercial use
**
*/
#include <stdio.h>
#include <modes.h>
#include <ctype.h>
#define true 1
#define false 0
#define trace false
char qflag = false;
int pcount = 0;
extern int errno;
char pbuff[140],
*ppntr;
char **arg;
char *username;
union {short userno;
struct { char userhi, userlo;} users;
} user;
int userint;
/* ------------Main Procedure--------------------------------------*/
main(argc,argv)
int argc;
char *argv[];
{
#if trace
fprintf(stderr,"main(%d parameters)\n",argc);
fflush(stderr);
#endif
get_paras(argc,argv);
if ( qflag ) help ();
if (pcount < 1) help ();
arg = argv;
getowner();
printf("User %03u.%03u : %s\n",(int) user.users.userhi,
(int) user.users.userlo,
username);
userint = user.users.userhi * 65536 + user.users.userlo;
/* This is because CHOWN demands an int as para */
while ( pcount-- > 0 ) newowner();
#if trace
fprintf(stderr,"exit\n");
fflush(stderr);
#endif
exit(0);
}
/* ---------------Analyse Options-----------------------------------*/
/*
** Read and store pointers and markers
** Called from MAIN procedure
*/
get_paras(pargc,pargv)
int pargc;
char **pargv;
{
char c, *p;
#if trace
fprintf(stderr,"get_paras(%d parameters)\n",pargc);
fflush(stderr);
#endif
while ( (--pargc) )
{
if (*(p = *++pargv) == '-')
{
while (c = *++p)
{
switch (c)
{
case '?' :
qflag = true;
break;
default :
error("bad option",0);
}
}
}
else pcount++;
}
}
/* -----------------Get User from Password File----------------------*/
/*
** Scans down the PBUFF buffer to get the next parameter
** Terminates on "," (replacing it by \0)
** Errors on \n
** Uses the global pointer PPNTR, returns pointer to parameter
*/
char * getpass()
{
int pass, cnt;
char *x1, *x2;
#if trace
fprintf(stderr,"getpass(); ppntr = %-20s\n",ppntr);
fflush(stderr);
#endif
if (*ppntr == '\0') return ppntr;
for (x1 = ppntr; ( (*x1 != ',') & (*x1 != '\0') ) ;x1++);
*x1++ = '\0';
x2 = ppntr;
ppntr = x1;
return x2;
}
/* get a User Number from a string pointer.
** called from getowner
*/
getno(ptr)
char *ptr;
{
char *p1, *p2;
#if trace
fprintf(stderr,"getno(%-8s)\n",ptr);
fflush(stderr);
#endif
while (*ptr == ' ') ptr++;
p1 = ptr;
if (!isdigit(*ptr)) error ("No Group Number");
while (isdigit(*ptr)) ptr++;
if (*ptr != '.') error("No dot in User Number");
*ptr++ = '\0';
if (!isdigit(*ptr)) error ("No User Number");
p2 = ptr;
while (isdigit(*ptr)) ptr++;
while (*ptr == ' ') ptr++;
if (*ptr != '\0') error("Invalid User Number");
return (atoi(p1) * 256 + atoi(p2) );
}
/*
** Reads the new owner from the first parameter
** Called from MAIN procedure
*/
getowner()
{
int para_no, pass, cnt;
char x, *x1, *x2;
#if trace
fprintf(stderr,"getowner()\n");
fflush(stderr);
#endif
pcount--;
while (**++arg == '-') ;
pass = open("/DD/SYS/PASSWORD",_READ);
if ( pass == -1 ) pass = open("/H0/SYS/PASSWORD",_READ);
if ( pass == -1 ) pass = open("/D0/SYS/PASSWORD",_READ);
if ( pass == -1 ) error("Can't open password file");
x = **arg;
if (!isascii(x)) error("Corrupt Password File");
para_no = (isdigit(x)) ? getno(*arg) : 0;
while (cnt = readln(pass,pbuff,138) )
{
if (cnt == -1) error("Error reading Password File");
pbuff[cnt-1] = '\0';
#if trace
fprintf(stderr,"read: cnt = %d; pbuff = %-20s\n",cnt,pbuff);
fflush(stderr);
#endif
ppntr = pbuff;
while (*ppntr == ' ') ppntr++;
if (*ppntr == '\0') continue;
username = getpass();
getpass(); /* skip password */
x1 = getpass();
user.userno = getno(x1);
#if trace
fprintf(stderr,"getno.loop para_no = %d; userno = %d\n",para_no,user.userno);
fprintf(stderr," *arg = %-9s; username = %-9s\n",*arg,username);
fflush(stderr);
#endif
if (isdigit(x)) { if (para_no == user.userno) return 0; }
else { if (!strcmp(*arg,username) ) return 0; }
}
error("No such user");
exit(5);
}
/* -----------------Change Ownership on named files-----------------*/
/*
** Changes the File Ownership on named files
** Called from MAIN procedure
*/
newowner()
{
#if trace
fprintf(stderr,"newowner()");
fflush(stderr);
#endif
while (**++arg == '-') ;
if (chown(*arg, userint) != 0)
exit(errno);
printf("Owner changed for file : %s \n",*arg);
;
}
/* -------------------Help, Error, Etc------------------------------*/
/*
** provide usage info for this command
*/
help()
{
fputs("owner: change ownership of files \n", stderr);
fputs(" (super user only) \n", stderr);
fputs("Usage: owner user file file ... \n", stderr);
fputs("Where: \n", stderr);
fputs("User = group.userno or username \n", stderr);
fputs("Options = -? \n", stderr);
exit (0);
}
/*
** write an error message, by
** writing the string passed and then a <cr>
*/
error(s1)
char *s1;
{
fputs(s1,stderr);
fputs("\n",stderr);
exit(5);
}