home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / auth-utils / part03 < prev    next >
Text File  |  1990-04-29  |  12KB  |  378 lines

  1. Subject:  v22i005:  Utilities for auth2.1 server, Part03/03
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 79f47823 c9e216d5 c5b8ab33 61bfe5b0
  5.  
  6. Submitted-by: Daniel J. Bernstein <brnstnd@acf10.nyu.edu>
  7. Posting-number: Volume 22, Issue 5
  8. Archive-name: auth-utils/part03
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 3 (of 3)."
  17. # Contents:  aport/checkaport.c
  18. # Wrapped by rsalz@litchi.bbn.com on Mon Apr 30 16:18:48 1990
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'aport/checkaport.c' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'aport/checkaport.c'\"
  22. else
  23. echo shar: Extracting \"'aport/checkaport.c'\" \(9972 characters\)
  24. sed "s/^X//" >'aport/checkaport.c' <<'END_OF_FILE'
  25. X/*
  26. Xcheckaport.c: clean up and check security in AUTHDIR
  27. X*/
  28. X
  29. X/* WARNING! checkaport should not be setuid auth, but it's probably */
  30. X/* running as either auth or root! All privileged programs are */
  31. X/* dangerous! Check them carefully! */
  32. X
  33. Xstatic char checkaportauthor[] =
  34. X"checkaport was written by Daniel J. Bernstein.\n\
  35. XInternet address: brnstnd@acf10.nyu.edu.\n";
  36. X
  37. Xstatic char checkaportversion[] = 
  38. X"checkaport version 1.0, April 23, 1990.\n\
  39. XCopyright (c) 1990, Daniel J. Bernstein.\n\
  40. XAll rights reserved.\n";
  41. X
  42. Xstatic char checkaportcopyright[] =
  43. X"checkaport version 1.0, April 23, 1990.\n\
  44. XCopyright (c) 1990, Daniel J. Bernstein.\n\
  45. XAll rights reserved.\n\
  46. X\n\
  47. XUntil January 1, 1995, you are granted the following rights: A. To make\n\
  48. Xcopies of this work in original form, so long as (1) the copies are exact\n\
  49. Xand complete; (2) the copies include the copyright notice, this paragraph,\n\
  50. Xand the disclaimer of warranty in their entirety. B. To distribute this\n\
  51. Xwork, or copies made under the provisions above, so long as (1) this is\n\
  52. Xthe original work and not a derivative form; (2) you do not charge a fee\n\
  53. Xfor copying or for distribution; (3) you ensure that the distributed form\n\
  54. Xincludes the copyright notice, this paragraph, and the disclaimer of\n\
  55. Xwarranty in their entirety. These rights are temporary and revocable upon\n\
  56. Xwritten, oral, or other notice by Daniel J. Bernstein. These rights are\n\
  57. Xautomatically revoked on January 1, 1995. This copyright notice shall be\n\
  58. Xgoverned by the laws of the state of New York.\n\
  59. X\n\
  60. XIf you have questions about checkaport or about this copyright notice,\n\
  61. Xor if you would like additional rights beyond those granted above,\n\
  62. Xplease feel free to contact the author at brnstnd@acf10.nyu.edu\n\
  63. Xon the Internet.\n";
  64. X
  65. Xstatic char checkaportwarranty[] =
  66. X"To the extent permitted by applicable law, Daniel J. Bernstein disclaims\n\
  67. Xall warranties, explicit or implied, including but not limited to the\n\
  68. Ximplied warranties of merchantability and fitness for a particular purpose.\n\
  69. XDaniel J. Bernstein is not and shall not be liable for any damages,\n\
  70. Xincidental or consequential, arising from the use of this program, even\n\
  71. Xif you inform him of the possibility of such damages. This disclaimer\n\
  72. Xshall be governed by the laws of the state of New York.\n\
  73. X\n\
  74. XIn other words, use this program at your own risk.\n\
  75. X\n\
  76. XIf you have questions about checkaport or about this disclaimer of warranty,\n\
  77. Xplease feel free to contact the author at brnstnd@acf10.nyu.edu\n\
  78. Xon the Internet.\n";
  79. X
  80. Xstatic char checkaportusage[] =
  81. X"Usage: checkaport [ -ACHUVW ] \n\
  82. XHelp:  checkaport -H\n";
  83. X
  84. Xstatic char checkaporthelp[] =
  85. X"checkaport goes through the auth directory, cleaning up and reporting\n\
  86. Xany inconsistencies or security violations.\n\
  87. X\n\
  88. Xcheckaport -A: print authorship notice\n\
  89. Xcheckaport -C: print copyright notice\n\
  90. Xcheckaport -H: print this notice\n\
  91. Xcheckaport -U: print short usage summary\n\
  92. Xcheckaport -V: print version number\n\
  93. Xcheckaport -W: print disclaimer of warranty\n\
  94. X\n\
  95. Xcheckaport: start checking\n\
  96. X\n\
  97. XIf you have questions about or suggestions for checkaport, please feel free\n\
  98. Xto contact the author, Daniel J. Bernstein, at brnstnd@acf10.nyu.edu\n\
  99. Xon the Internet.\n";
  100. X
  101. X#include <sys/types.h>
  102. X#include <sys/dir.h>
  103. X#include <sys/file.h>
  104. X#ifdef BSD
  105. X#include <limits.h>
  106. X#endif
  107. X#include <stdio.h>
  108. Xextern int getopt();
  109. Xextern char *optarg; /* these should be in getopt.h! */
  110. Xextern int optind;
  111. X#include "djberr.h"
  112. X
  113. X#ifndef AUTHDIR
  114. X#define AUTHDIR "/usr/etc/auth"
  115. X#endif
  116. X
  117. Xmain(argc,argv,envp)
  118. Xint argc;
  119. Xchar *argv[];
  120. Xchar *envp[];
  121. X{
  122. X int opt;
  123. X int uid = getuid();
  124. X int euid = geteuid();
  125. X DIR *dirp;
  126. X struct direct *dp;
  127. X int fd;
  128. X char buf[32]; /* same length as in attachport */
  129. X int r;
  130. X char username[10];
  131. X char usertemp[10];
  132. X int pid;
  133. X int pidchild;
  134. X unsigned short port;
  135. X unsigned long in;
  136. X unsigned short rport;
  137. X char pfn[sizeof(AUTHDIR) + 30];
  138. X
  139. X /* WARNING! We are probably running as auth or root! */
  140. X
  141. X while ((opt = getopt(argc,argv,"ACHUVW")) != EOF)
  142. X   switch(opt)
  143. X    {
  144. X     case 'A': (void) err(checkaportauthor); exit(1);
  145. X     case 'C': (void) err(checkaportcopyright); exit(1);
  146. X     case 'H': (void) err(checkaporthelp); exit(1);
  147. X     case 'U': (void) err(checkaportusage); exit(1);
  148. X     case 'V': (void) err(checkaportversion); exit(1);
  149. X     case 'W': (void) err(checkaportwarranty); exit(1);
  150. X     case '?': (void) err(checkaportusage); exit(1);
  151. X    }
  152. X argv += optind, argc -= optind;
  153. X
  154. X if ((chdir(AUTHDIR) == -1) || chdir("tcp") == -1)
  155. X  {
  156. X   perrn2("checkaport: fatal: cannot change to directory %s/tcp",AUTHDIR);
  157. X   exit(2);
  158. X  }
  159. X
  160. X if (!(dirp = opendir(".")))
  161. X  {
  162. X   perrn2("checkaport: fatal: cannot read directory %s/tcp",AUTHDIR);
  163. X   exit(3);
  164. X  }
  165. X
  166. X while (dp = readdir(dirp))
  167. X  {
  168. X   if ((dp->d_name[0] == '.')
  169. X     &&((dp->d_name[1] == '\0')
  170. X      ||((dp->d_name[1] == '.')
  171. X       &&(dp->d_name[2] == '\0'))))
  172. X     continue;
  173. X   switch(dp->d_name[0])
  174. X    {
  175. X     case 'l':
  176. X       if (sscanf(dp->d_name,"lock.%hd",&port) < 1)
  177. X    {
  178. X     errn2("checkaport: warning: UFO %s",dp->d_name);
  179. X    }
  180. X       else if (((fd = open(dp->d_name,O_RDONLY)) == -1)
  181. X           &&(flock(fd,LOCK_EX) == -1))
  182. X    {
  183. X     perrn2("checkaport: warning: cannot open %s",dp->d_name);
  184. X    }
  185. X       else if ((r = read(fd,buf,31)) <= 0)
  186. X    {
  187. X     errn2("checkaport: warning: cannot read %s, deleting",dp->d_name);
  188. X     (void) close(fd);
  189. X     (void) unlink(dp->d_name);
  190. X    }
  191. X       else
  192. X    {
  193. X     buf[r] = '\0';
  194. X     (void) close(fd);
  195. X     if (buf[0] == '!')
  196. X      {
  197. X       errn2("checkaport: warning: corrupted lock file %s",dp->d_name);
  198. X       errn2("checkaport: contents of lock file: %s",buf);
  199. X       errn("checkaport: this may be an attempted security breach!");
  200. X      }
  201. X     else if (sscanf(buf,"%d-%8s",&pid,username) < 2)
  202. X       if (sscanf(buf,"%d:%D.%hd %8s",&pid,&in,&rport,username) < 4)
  203. X        {
  204. X         errn2("checkaport: warning: UFO contents %s",dp->d_name);
  205. X        }
  206. X       else /* authtcp lock file */
  207. X        {
  208. X         sprintf(buf,"%D.%u.%u",in,port,rport);
  209. X         if (((fd = open(buf,O_RDONLY)) == -1)
  210. X           &&(flock(fd,LOCK_EX) == -1))
  211. X          {
  212. X           perrn2("checkaport: warning: cannot open %s",buf);
  213. X           errn2("checkaport: came from lock %s",dp->d_name);
  214. X          }
  215. X         else
  216. X          {
  217. X           if ((r = read(fd,usertemp,8)) <= 0)
  218. X         r = 0;
  219. X           usertemp[r] = '\0';
  220. X           if (r == 0 || strcmp(usertemp,username))
  221. X        {
  222. X         errn("checkaport: warning: security breach!");
  223. X         errn2("checkaport: lock file %s",dp->d_name);
  224. X         errn2("checkaport: ILR %s",buf);
  225. X         errn2("checkaport: lock file user %s",username);
  226. X         errn2("checkaport: ILR user %s",usertemp);
  227. X         errn("checkaport: deleting both files");
  228. X         (void) unlink(dp->d_name);
  229. X         (void) unlink(buf);
  230. X        }
  231. X           (void) close(fd);
  232. X          }
  233. X        }
  234. X     if (kill(pid,0) == -1)
  235. X      {
  236. X       errn2("checkaport: warning: no pid for lock file %s, deleting",
  237. X         dp->d_name);
  238. X       (void) unlink(dp->d_name);
  239. X      }
  240. X    }
  241. X       break;
  242. X     case 'p':
  243. X       if (sscanf(dp->d_name,"ps.%d.%d",&pid,&pidchild) < 2)
  244. X    {
  245. X     errn2("checkaport: warning: UFO %s",dp->d_name);
  246. X    }
  247. X       else if (((fd = open(dp->d_name,O_RDONLY)) == -1)
  248. X           &&(flock(fd,LOCK_EX) == -1))
  249. X    {
  250. X     perrn2("checkaport: warning: cannot open %s",dp->d_name);
  251. X    }
  252. X       else if ((r = read(fd,pfn,sizeof(pfn) - 1)) <= 0)
  253. X    {
  254. X     errn2("checkaport: warning: cannot read %s",dp->d_name);
  255. X     (void) close(fd);
  256. X    }
  257. X       else
  258. X    {
  259. X     pfn[r] = '\0';
  260. X     (void) close(fd);
  261. X     if (strncmp(pfn,AUTHDIR,strlen(AUTHDIR)))
  262. X      {
  263. X       errn2("checkaport: warning: ps %s out of directory, deleting",
  264. X         dp->d_name);
  265. X       (void) unlink(dp->d_name);
  266. X      }
  267. X     else if (((fd = open(pfn,O_RDONLY)) == -1)
  268. X            &&(flock(fd,LOCK_EX) == -1))
  269. X      {
  270. X       perrn2("checkaport: warning: cannot open ilr %s",pfn);
  271. X       errn2("checkaport: came from ps %s, deleting",dp->d_name);
  272. X       (void) unlink(dp->d_name);
  273. X      }
  274. X     else
  275. X      {
  276. X       /* XXX: Can we do more tests here? */
  277. X       (void) close(fd);
  278. X       if (kill(pid,0) == -1)
  279. X        {
  280. X         errn2("checkaport: warning: no pid for ps file %s, deleting",buf);
  281. X         (void) unlink(buf);
  282. X        }
  283. X       else if (kill(pidchild,0) == -1)
  284. X        {
  285. X         errn2("checkaport: warning: no pid for ps file %s, deleting",buf);
  286. X         (void) unlink(buf);
  287. X        }
  288. X      }
  289. X    }
  290. X       break;
  291. X     default:
  292. X       if (sscanf(dp->d_name,"%D.%hd.%hd",&in,&port,&rport) < 3)
  293. X    {
  294. X     errn2("checkaport: warning: UFO %s",dp->d_name);
  295. X    }
  296. X       else if (((fd = open(dp->d_name,O_RDONLY)) == -1)
  297. X           &&(flock(fd,LOCK_EX) == -1))
  298. X    {
  299. X     perrn2("checkaport: warning: cannot open %s",dp->d_name);
  300. X    }
  301. X       else if ((r = read(fd,usertemp,8)) <= 0)
  302. X    {
  303. X     errn2("checkaport: warning: cannot read %s, deleting",dp->d_name);
  304. X     (void) close(fd);
  305. X     (void) unlink(dp->d_name);
  306. X    }
  307. X       else
  308. X    {
  309. X     usertemp[r] = '\0';
  310. X     (void) close(fd);
  311. X     sprintf(buf,"lock.%u",port);
  312. X     if (((fd = open(buf,O_RDONLY)) == -1)
  313. X           &&(flock(fd,LOCK_EX) == -1))
  314. X      {
  315. X       perrn2("checkaport: warning: cannot open lock %s",buf);
  316. X       errn2("checkaport: came from ilr %s, deleting",dp->d_name);
  317. X       (void) unlink(dp->d_name);
  318. X      }
  319. X     if ((r = read(fd,buf,31)) <= 0)
  320. X      {
  321. X       errn2("checkaport: weird: cannot read lock.%u",port);
  322. X      }
  323. X     else
  324. X      {
  325. X       buf[r] = '\0';
  326. X       (void) close(fd);
  327. X       if ((sscanf(buf,"%d-%8s",&pid,username) < 2)
  328. X         &&(sscanf(buf,"%d:%D.%hd %8s",&pid,&in,&rport,username) < 4))
  329. X        {
  330. X         errn2("checkaport: warning: corrupted lock file lock.%u",port);
  331. X         errn2("checkaport: came from ilr %s, deleting",dp->d_name);
  332. X         errn("checkaport: this may be an attempted security breach!");
  333. X         (void) unlink(dp->d_name);
  334. X        }
  335. X       if (r == 0 || strcmp(usertemp,username))
  336. X        {
  337. X         errn2("checkaport: warning: corrupted lock file lock.%u",port);
  338. X         errn2("checkaport: came from ilr %s, deleting",dp->d_name);
  339. X         errn2("checkaport: ilr user %s",usertemp);
  340. X         errn2("checkaport: lock file user %s",username);
  341. X         errn("checkaport: this may be an attempted security breach!");
  342. X         (void) unlink(dp->d_name);
  343. X        }
  344. X      }
  345. X    }
  346. X       break;
  347. X    }
  348. X  }
  349. X
  350. X (void) closedir(dirp);
  351. X
  352. X exit(0);
  353. X}
  354. END_OF_FILE
  355. if test 9972 -ne `wc -c <'aport/checkaport.c'`; then
  356.     echo shar: \"'aport/checkaport.c'\" unpacked with wrong size!
  357. fi
  358. # end of 'aport/checkaport.c'
  359. fi
  360. echo shar: End of archive 3 \(of 3\).
  361. cp /dev/null ark3isdone
  362. MISSING=""
  363. for I in 1 2 3 ; do
  364.     if test ! -f ark${I}isdone ; then
  365.     MISSING="${MISSING} ${I}"
  366.     fi
  367. done
  368. if test "${MISSING}" = "" ; then
  369.     echo You have unpacked all 3 archives.
  370.     rm -f ark[1-9]isdone
  371. else
  372.     echo You still need to unpack the following archives:
  373.     echo "        " ${MISSING}
  374. fi
  375. ##  End of shell archive.
  376. exit 0
  377. exit 0 # Just in case...
  378.