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 >
C/C++ Source or Header  |  1992-03-06  |  6KB  |  302 lines

  1. /*
  2. ** OWNER
  3. **  changes the ownership of files using the Cumana Password System
  4. **    owner <newgroup>.<newowner> <file> <file> ... 
  5. **     options are:
  6. **            -? = report usage
  7. **
  8. **  The password file is looked for on the following
  9. **            /DD/SYS/PASSWORD
  10. **            /H0/SYS/PASSWORD
  11. **            /D0/SYS/PASSWORD
  12. **
  13. ** 
  14. **  copyright 1991 by Cowen Software Ltd
  15. **  23 Bristol Ave, Levenshulme, Manchester,
  16. **  GB-M19 3NU, England, UK
  17. **  released to the public domain for non-commercial use
  18. **  
  19. */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <modes.h>
  24. #include <ctype.h>
  25. #define true 1
  26. #define false 0
  27. #define trace false
  28.  
  29. char qflag = false;
  30. int  pcount = 0;
  31. extern int  errno;
  32.  
  33. char pbuff[140],
  34.      *ppntr;
  35.  
  36. char **arg;
  37.  
  38. char *username;
  39. union {short  userno;
  40.        struct { char userhi, userlo;} users;
  41.       } user;
  42.  
  43. int userint;       
  44.  
  45.  
  46. /* ------------Main Procedure--------------------------------------*/
  47.  
  48.  
  49. main(argc,argv)
  50. int   argc;
  51. char  *argv[];
  52.    {
  53.  
  54. #if trace
  55. fprintf(stderr,"main(%d parameters)\n",argc);
  56. fflush(stderr); 
  57. #endif
  58.  
  59.    get_paras(argc,argv);
  60.    if ( qflag ) help ();      
  61.    if    (pcount < 1)  help ();
  62.  
  63.    arg = argv;
  64.    
  65.    getowner();
  66.    printf("User %03u.%03u : %s\n",(int) user.users.userhi,
  67.                                   (int) user.users.userlo,
  68.                                   username);
  69.    userint = user.users.userhi * 65536 + user.users.userlo;
  70.         /*   This is because CHOWN demands an int as para    */
  71.                                   
  72.    while ( pcount-- > 0 ) newowner();
  73.  
  74. #if trace
  75. fprintf(stderr,"exit\n");
  76. fflush(stderr); 
  77. #endif
  78.  
  79.    exit(0);
  80.    }
  81.    
  82.  
  83. /* ---------------Analyse Options-----------------------------------*/
  84.      
  85.   
  86. /*
  87. **    Read and store pointers and markers
  88. **    Called from MAIN procedure
  89. */
  90.    
  91. get_paras(pargc,pargv)
  92. int   pargc;
  93. char  **pargv;
  94.    {
  95.    char c, *p;
  96.  
  97. #if trace
  98. fprintf(stderr,"get_paras(%d parameters)\n",pargc);
  99. fflush(stderr); 
  100. #endif
  101.  
  102.    while ( (--pargc) )
  103.       {
  104.        if (*(p = *++pargv) == '-')
  105.          {
  106.          while (c = *++p)
  107.             {
  108.             switch (c)
  109.                {
  110.                case '?' :
  111.                      qflag = true;
  112.                      break;
  113.                default  :
  114.                      error("bad option",0);
  115.                }
  116.             }
  117.          }
  118.        else  pcount++;
  119.       }
  120.    }
  121.       
  122.  
  123. /* -----------------Get User from Password File----------------------*/
  124.  
  125. /*
  126. **    Scans down the PBUFF buffer to get the next parameter
  127. **    Terminates on "," (replacing it by \0)
  128. **    Errors on \n
  129. **    Uses the global pointer PPNTR, returns pointer to parameter
  130. */
  131.    
  132. char * getpass()
  133. {
  134. int pass, cnt;
  135. char *x1, *x2;
  136.  
  137. #if trace
  138. fprintf(stderr,"getpass(); ppntr = %-20s\n",ppntr);
  139. fflush(stderr); 
  140. #endif
  141.  
  142. if (*ppntr == '\0') return ppntr;
  143. for (x1 = ppntr; ( (*x1 != ',') & (*x1 != '\0') ) ;x1++);
  144.  
  145. *x1++ = '\0';
  146. x2 = ppntr;
  147. ppntr = x1;
  148. return x2;         
  149. }
  150.       
  151.  
  152. /*    get a User Number from a string pointer.
  153. **    called from getowner
  154. */
  155.  
  156. getno(ptr)
  157. char *ptr;
  158.       
  159. {
  160. char *p1, *p2;    
  161.  
  162. #if trace
  163. fprintf(stderr,"getno(%-8s)\n",ptr);
  164. fflush(stderr); 
  165. #endif
  166.  
  167. while (*ptr == ' ') ptr++;
  168. p1 = ptr;
  169. if (!isdigit(*ptr)) error ("No Group Number");
  170. while (isdigit(*ptr)) ptr++;
  171. if (*ptr != '.') error("No dot in User Number");
  172. *ptr++ = '\0';
  173. if (!isdigit(*ptr)) error ("No User Number");
  174. p2 = ptr;
  175. while (isdigit(*ptr)) ptr++;
  176. while (*ptr == ' ') ptr++;
  177. if (*ptr != '\0') error("Invalid User Number");
  178. return (atoi(p1) * 256 + atoi(p2) );
  179. }
  180.  
  181.  
  182.  
  183. /*
  184. **    Reads the new owner from the first parameter
  185. **    Called from MAIN procedure
  186. */
  187.    
  188. getowner()
  189.    {
  190.    int para_no, pass, cnt;
  191.    char x, *x1, *x2;
  192.  
  193. #if trace
  194. fprintf(stderr,"getowner()\n");
  195. fflush(stderr); 
  196. #endif
  197.  
  198.    pcount--;
  199.    while (**++arg == '-') ;
  200.    pass =  open("/DD/SYS/PASSWORD",_READ);
  201.    if ( pass == -1 ) pass =  open("/H0/SYS/PASSWORD",_READ);
  202.    if ( pass == -1 ) pass =  open("/D0/SYS/PASSWORD",_READ);
  203.    if ( pass == -1 )  error("Can't open password file");
  204.    x = **arg;
  205.    if (!isascii(x)) error("Corrupt Password File");
  206.  
  207.     para_no = (isdigit(x)) ? getno(*arg) : 0;
  208.    
  209.    while (cnt = readln(pass,pbuff,138) )
  210.       { 
  211.       if (cnt == -1) error("Error reading Password File");
  212.       pbuff[cnt-1] = '\0';
  213.       
  214. #if trace
  215. fprintf(stderr,"read: cnt = %d; pbuff = %-20s\n",cnt,pbuff);
  216. fflush(stderr); 
  217. #endif
  218.  
  219.       ppntr = pbuff;
  220.       while (*ppntr == ' ') ppntr++;
  221.       if (*ppntr == '\0') continue;
  222.       username = getpass();
  223.       getpass();              /* skip password */
  224.       x1 = getpass();
  225.       user.userno = getno(x1);
  226.  
  227. #if trace
  228. fprintf(stderr,"getno.loop para_no = %d; userno = %d\n",para_no,user.userno);
  229. fprintf(stderr,"      *arg = %-9s; username = %-9s\n",*arg,username);
  230. fflush(stderr); 
  231. #endif
  232.  
  233.       if (isdigit(x)) { if (para_no == user.userno) return 0; }
  234.       else            { if (!strcmp(*arg,username) ) return 0; }
  235.       }
  236.         
  237.    error("No such user");
  238.    exit(5);
  239.    }
  240.  
  241.  
  242. /* -----------------Change Ownership on named files-----------------*/
  243.     
  244.   
  245. /*
  246. **    Changes the File Ownership on named files
  247. **    Called from MAIN procedure
  248. */
  249.    
  250. newowner()
  251.    {
  252.  
  253. #if trace
  254. fprintf(stderr,"newowner()");
  255. fflush(stderr); 
  256. #endif
  257.  
  258.    while (**++arg == '-') ;
  259.    if (chown(*arg, userint) != 0) 
  260.               exit(errno);
  261.    printf("Owner changed for file : %s \n",*arg);
  262.  
  263.    ; 
  264.    }
  265.       
  266.  
  267. /* -------------------Help, Error, Etc------------------------------*/
  268.  
  269.   
  270. /*
  271. ** provide usage info for this command
  272. */
  273.  
  274.  
  275. help()
  276.    {
  277.    fputs("owner: change ownership of files          \n", stderr);
  278.    fputs("         (super user only)                \n", stderr);
  279.    fputs("Usage: owner user file file ...           \n", stderr);
  280.    fputs("Where:                                    \n", stderr);
  281.    fputs("User    = group.userno or username        \n", stderr);
  282.    fputs("Options = -?                              \n", stderr);
  283.    exit (0);
  284.    }
  285.    
  286.   
  287. /*
  288. ** write an error message, by 
  289. ** writing the string passed and then a <cr>
  290. */
  291.  
  292. error(s1)
  293. char  *s1;
  294.    {
  295.    fputs(s1,stderr);
  296.    fputs("\n",stderr);
  297.    exit(5);
  298.    }
  299.  
  300.  
  301.  
  302.