home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ACCESS.ZIP / ACCESS.C
Encoding:
Text File  |  1980-01-01  |  8.0 KB  |  258 lines

  1.  
  2. From mnetor!seismo!ut-sally!husc6!uwvax!rutgers!sri-spam!nike!ll-xn!adelie!axiom!linus!philabs!mcnc!ecsvax!bet Sun Oct 19 12:34:37 EDT 1986
  3. Article 3467 of net.sources:
  4. Relay-Version: version B 2.10.2 9/18/84; site lsuc.UUCP
  5. Path: lsuc!mnetor!seismo!ut-sally!husc6!uwvax!rutgers!sri-spam!nike!ll-xn!adelie!axiom!linus!philabs!mcnc!ecsvax!bet
  6. >From: bet@ecsvax.UUCP (Bennett E. Todd III)
  7. Newsgroups: net.sources
  8. Subject: Access -- a program to give permissions across logins
  9. Message-ID: <2146@ecsvax.UUCP>
  10. Date: 16 Oct 86 21:24:36 GMT
  11. Date-Received: 19 Oct 86 04:15:07 GMT
  12. Reply-To: bet@ecsvax.UUCP (Bennett E. Todd III)
  13. Distribution: net
  14. Organization: Duke University Computation Center
  15. Lines: 239
  16.  
  17.  
  18. #!/bin/sh
  19. # Cut above the preceeding line, or cut here if you must.
  20. # This is a shar archive.  Extract with sh, not csh.
  21. # The rest of this file will extract:
  22. # README Makefile access.c regerror.c
  23. sed 's/^X//' > README << '/*EOF'
  24. XAccess is a program I wrote to handle a not-so-infrequent situation that
  25. Xstandard UNIX file permissions didn't seem to address, quite. Given a
  26. Xset of users (e.g. students in a class) which are to be protected from
  27. Xeach other (can't read or destroy each other's homework, for example)
  28. Xand one or more "senior" logins (e.g. the teacher of the class, TAs for
  29. Xthe class) arrange for these (individually protected from each other)
  30. Xlogins to all be accessible to the senior, administrative logins. The
  31. Xsenior logins aren't to be given root permissions. I couldn't figure a
  32. Xway to do this with the standard UNIX permissions system, so I wrote the
  33. Xfollowing. It should be installed somewhere suid root (I'd make it owner
  34. Xroot, in a directory that isn't world writeable, mode 6711. The setgid
  35. Xprobably isn't necessary). It checks a file "/etc/accesstab" (shouldn't
  36. Xbe world writeable, right) for lines of the form
  37. X
  38. X       regexp1:regexp2
  39. X
  40. XIf the requesting user's username matches regexp1, then a regsub is done
  41. Xfrom that match onto regexp2, and the resulting pattern is matched
  42. Xagainst the requested destination. If it matches, the program succeeds.
  43. XOtherwise, it fails. In behavior, it is about the same as su(1) with the
  44. Xfollowing differences:
  45. X
  46. X       1) Doesn't require any password
  47. X       2) Requires a match in the accesstab file
  48. X       3) Changes to the login home directory of the requested user.
  49. X
  50. X(3) above is possibly not appropriate; it turned out to be proper for
  51. Xthe use I had for it, and didn't seem to me to be a bad idea. You can
  52. Xrip it out if you don't like it.
  53. X
  54. XCaveats:  I am not sure that this isn't a serious breach of security;
  55. XI cannot think of a way to break it, but that doesn't mean it's
  56. Xsecure. Use at your own risk, and check it before you use it. This
  57. Xcritter explicitly kicks holes in the UNIX security system; don't use
  58. Xit to give any logins access to privileged logins such as root, and
  59. Xit probably shouldn't be used for any permanent project, only for
  60. Xtemporary undertakings. DON'T TRY TO USE IT IF YOU AREN'T COMPLETELY
  61. XCOMFORTABLE WITH REGULAR EXPRESSIONS!!! Use completely explicit
  62. Xregular expressions, to ensure no accidental matches; regular
  63. Xexpression metacharacters should only be used whete the logins have a
  64. Xsystematic format (such as class logins with a common format). A
  65. Xreasonable use would be for a class with a teacher "deboss" with
  66. Xclass logins "cs00" - "cs49":
  67. X
  68. X    deboss:cs[0-4][0-9]
  69. X
  70. XMake sure that not only are all logins that should be permitted
  71. Xmatchable by the target, but that all patterns matchable by the
  72. Xtarget are ones that are supposed to be reachable.
  73. X
  74. XThis program is built with Henry Spencer's V8-compatible regexp
  75. Xlibrary, from mod.sources. Get it. It's excellent.
  76. X
  77. XThis program was written and tested under System V (AT&T System V
  78. XRelease 2.0.1 3B5 Version 2); as far as I know it should be portable to
  79. Xmost reasonably modern UNIXs.
  80. X
  81. X-Bennett
  82. /*EOF
  83. ls -l README
  84. sed 's/^X//' > Makefile << '/*EOF'
  85. X# MYINCL is where the include file "regexp.h" can be found, if it isn't
  86. X# in your /usr/include
  87. XMYINCL=$(HOME)/include
  88. X
  89. X# Likewise for regexp.a, if the regexp routines aren't in your libc.a
  90. XMYLIBS=$(HOME)/lib
  91. X
  92. XCFLAGS=-O -I$(MYINCL)
  93. X
  94. Xaccess : access.o regerror.o
  95. X       cc -O -o access access.o regerror.o $(MYLIBS)/regexp.a
  96. /*EOF
  97. ls -l Makefile
  98. sed 's/^X//' > access.c << '/*EOF'
  99. X#include <stdio.h>
  100. X#include "regexp.h"
  101. X/*
  102. X * access (userid) [command]
  103. X *
  104. X * Scans /etc/accesstab. Accesstab format
  105. X *
  106. X *     regex1:regex2
  107. X *
  108. X * If the login name of the invoker matches regex1, then a regsub is
  109. X * performed on regex2, and if the requested "(userid)" matches the
  110. X * resulting string, then it does a setgid to the gid of the requested
  111. X * "(userid)", a setuid to the appropriate userid, then exec's /bin/sh.
  112. X * It also changes directory to the home directory of the requested userid.
  113. X * [command], if specified, is passed to sh(1).
  114. X */
  115. X
  116. Xchar *progname;
  117. Xstatic char **shargv;
  118. Xstatic char *nope = "permission denied";
  119. Xstatic char *tablename = "/etc/accesstab";
  120. X
  121. X#ifndef ERR
  122. X#define ERR (-1)
  123. X#endif
  124. X
  125. Xmain(argc, argv)
  126. Xint argc;
  127. Xchar **argv;
  128. X{
  129. X       char *source,
  130. X                *dest,
  131. X                *getlogin();
  132. X
  133. X       progname = argv[0];
  134. X
  135. X       if (argc < 2)
  136. X               syntax();
  137. X
  138. X       dest = argv[1];
  139. X       shargv = argv+1;
  140. X
  141. X       if ((source = getlogin()) == NULL)
  142. X               error("Who are you?!");
  143. X
  144. X       if (check(source, dest))
  145. X               doit(dest);
  146. X       else
  147. X               error(nope);
  148. X}
  149. X
  150. Xsyntax()
  151. X{
  152. X       fprintf(stderr, "syntax: %s username [command]\n", progname);
  153. X       exit(1);
  154. X}
  155. X
  156. Xerror(s)
  157. Xchar *s;
  158. X{
  159. X       fprintf(stderr, "%s: %s\n", progname, s);
  160. X       exit(1);
  161. X}
  162. X
  163. XFILE *efopen(name, mode)
  164. Xchar *name, *mode;
  165. X{
  166. X       FILE *fp, *fopen();
  167. X       if (fp = fopen(name, mode))
  168. X               return(fp);
  169. X
  170. X       fprintf(stderr, "%s: cannot open %s\n", progname, name);
  171. X       exit(1);
  172. X}
  173. X
  174. Xcheck(s, d)
  175. Xchar *s, *d;
  176. X{
  177. X       FILE *fp, *efopen();
  178. X       char buffer[256], *ptr, destbuff[256], *strchr();
  179. X       regexp *r_src, *r_dest, *regcomp();
  180. X
  181. X       fp = efopen(tablename, "r");
  182. X       while (fgets(buffer, 256, fp)) {
  183. X               if (strlen(buffer) < 3 || *buffer == '#' || !strchr(buffer, ':'))
  184. X                       continue;
  185. X               while ((ptr=strchr(buffer, '\n')) || (ptr=strchr(buffer, '\r')))
  186. X                       *ptr = '\0';
  187. X               if (!(ptr=strchr(buffer, ':')))
  188. X                       error("parse error in accesstab");
  189. X               *ptr++ = '\0';
  190. X               r_src = regcomp(buffer);
  191. X               if (!regexec(r_src, s))
  192. X                       continue;
  193. X               if (!*ptr)
  194. X                       error("bad dest expression in accesstab");
  195. X               regsub(r_src, ptr, destbuff);
  196. X               r_dest = regcomp(destbuff);
  197. X               if (regexec(r_dest, d)) {
  198. X                       fclose(fp);
  199. X                       return(1);
  200. X               }
  201. X       }
  202. X       fclose(fp);
  203. X       return(0);
  204. X}
  205. X
  206. X#include <pwd.h>
  207. X
  208. Xdoit(name)
  209. Xchar *name;
  210. X{
  211. X       struct passwd *ptr, *getpwnam();
  212. X
  213. X       if (!(ptr=getpwnam(name)))
  214. X               error("no such userid");
  215. X       endpwent();
  216. X
  217. X       if (strcmp(name, ptr->pw_name) != 0)
  218. X               error("wrong passwd entry returned");
  219. X
  220. X       if (chdir(ptr->pw_dir) == ERR)
  221. X               error("cannot chdir");
  222. X
  223. X       if (setgid(ptr->pw_gid) == ERR)
  224. X               error("cannot setgid");
  225. X
  226. X       if (setuid(ptr->pw_uid) == ERR)
  227. X               error("cannot setuid");
  228. X
  229. X       shargv[0] = "sh";
  230. X
  231. X       execv("/bin/sh", shargv);
  232. X       error("exec failed");   
  233. X}
  234. /*EOF
  235. ls -l access.c
  236. sed 's/^X//' > regerror.c << '/*EOF'
  237. X#include <stdio.h>
  238. X
  239. Xextern char *progname;
  240. X
  241. Xvoid
  242. Xregerror(s)
  243. Xchar *s;
  244. X{
  245. X       fprintf(stderr, "%s: regexp(3): %s", progname, s);
  246. X       exit(1);
  247. X}
  248. /*EOF
  249. ls -l regerror.c
  250. exit
  251. -- 
  252.  
  253. Bennett Todd -- Duke Computation Center, Durham, NC 27706-7756; (919) 684-3695
  254. UUCP: ...{decvax,seismo,philabs,ihnp4,akgua}!mcnc!ecsvax!duccpc!bet
  255. BITNET: DBTODD@TUCC.BITNET -or- DBTODD@TUCCVM.BITNET -or- bet@ECSVAX.BITNET
  256.  
  257.  
  258.   $