home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume12 / unpackmaps3.0 / part01 / unpackmaps < prev    next >
Encoding:
Text File  |  1990-05-14  |  7.6 KB  |  330 lines

  1. :
  2. #    Unpackmaps Copyright 1990, Chris Lewis, All Rights Reserved
  3. trap "rm -f /tmp/unp?$$; exit" 0 1 2 3 15
  4. IFS="     
  5. "
  6. export IFS
  7. PATH=/bin:/usr/bin
  8. export PATH
  9.  
  10. #    The name of the file that you've caused your news system to
  11. #    batch the file names of the map articles.
  12. # Eg: C-news
  13. #BATCH=/usr/lib/news/batch/b.maps/togo
  14. #Modern C-news (directory other than /usr/spool/news/out.going)
  15. BATCH=/usr/spool/news/out.special/maps/togo
  16. # Eg: B-news
  17. #BATCH=/usr/spool/batch/maps
  18. #    News spool directory
  19. NEWSSPOOL=/usr/spool/news
  20. #    Where you want the maps to go.
  21. #    I like using /usr/spool/maps, but on our system, /usr/spool/news
  22. #    is a separate file system, and /usr runs close to the limit...
  23. MAPDIR=/usr/spool/news/maps
  24. #    Person to send results and error messages to
  25. NOTIFY=clewis
  26. #    pathalias binary
  27. PATHALIAS=/usr/lbin/pathalias
  28. #    where you want the path files to go:
  29. #    A convenient place is /usr/lib/uucp/paths which is the smail
  30. #    default.  If you're going to put this in /usr/lib/uucp, I suggest
  31. #    (rather than make /usr/lib/uucp writeable by everybody), doing
  32. #    the following:
  33. #        su root
  34. #        cd /usr/lib/uucp
  35. #        touch paths
  36. #        chown news paths    (or usenet)
  37. #        chmod 644 paths
  38. PATHFILE=/usr/lib/uucp/paths
  39. #    Auxiliary options to pathalias.  Tune to local tastes....
  40. PATHOPTS="-dwatmath"
  41. #    If you have a version[s] of your machine's map entry that is different 
  42. #    from what's published, change this variable to point at it/them.
  43. #    (Eg: I publish the first entry here, and the second one is local tuning
  44. #    and hidden connections)
  45. PATHLOCAL="/usr2/clewis/maps/path.local /usr2/clewis/maps/path.nonpublic"
  46. #    If this variable is set to the compress binary, maps will be
  47. #    compressed.
  48. COMPRESS=/usr2/clewis/maps/compress12
  49. #    1 to strip comments from maps - don't do this if you want to use
  50. #    uuwhere.  However, this is a great space saver...
  51. NOCOMMENTS=0
  52. #    Define to the name of a file where you want the where database
  53. #    to be kept.  Undef if you don't want uuwhere at all.
  54. WHEREFILE=$MAPDIR/where.db
  55. #    Uncomment this if you want the map unpacker to remove the
  56. #    News articles after the maps have been extracted from them.
  57. #    DO NOT DO THIS IF YOU FORWARD MAP ARTICLES TO OTHER SITES!
  58. #    This also relies on your awk returning "exit" codes properly.
  59. #    Yours may not...
  60. #UNLINK=1
  61. #    PS: there is *one* possible edit that you might want to make
  62. #    below - the maps used to generate wierd domains, but most of that
  63. #    appears to be gone now (don't ask me, I never particularly understood
  64. #    it, but since Peter Honeyman recommended it...).  If you object
  65. #    to these wierd domains, uncomment the egrep.
  66.  
  67. #    Edit no more....
  68.  
  69. umask 022
  70.  
  71. if test ! -d $MAPDIR -o ! -w $MAPDIR
  72. then
  73.     echo "$MAPDIR missing, unwritable or not a directory" >&2
  74.     exit 1
  75. fi
  76.  
  77. if test $# = 1
  78. then
  79.     case $1 in
  80.     -p)
  81.         forcepath=true
  82.         ;;
  83.     -P)
  84.         forcepath=false
  85.         ;;
  86.     -i)
  87.         cd /
  88.         rm -f $BATCH.work
  89.         # using find/sort instead of ls just in case there's lots of
  90.         # articles....
  91.         find $NEWSSPOOL/comp/mail/maps -type f -print | sort > $BATCH
  92.         ;;
  93.     *)
  94.         echo "usage: unpackmaps [-i] [-p]" >&2
  95.         exit 1
  96.         ;;
  97.     esac
  98. fi
  99.  
  100. cd $MAPDIR
  101. WHERETMP=/tmp/WHERE$$
  102. rm -f $WHERETMP
  103.         
  104. while test -f $BATCH -o -f $BATCH.work
  105. do
  106.     # There is no window of vulnerability here as long as noone else is
  107.     # creating $BATCH.work.
  108.     if test ! -f $BATCH.work
  109.     then
  110.     mv $BATCH $BATCH.work
  111.     fi
  112.  
  113.     while read i stuff
  114.     do
  115.     #    Using stuff to capture remaining junk on line.
  116.     #    Eg: C-news article sizes.
  117.  
  118.     if test -z "$i"
  119.     then
  120.         break
  121.     fi
  122.  
  123.     if test ! -r $i
  124.     then
  125.         echo "$i apparently superseded or expired"
  126.         continue
  127.     fi
  128.  
  129.     # This awk script depends on the following map article format:
  130.     # <don't cares>
  131.     # cat << 'something' > filename
  132.     # map body
  133.     # something
  134.     # <don't cares>
  135.     # "something" doesn't have to be enclosed in quotes in the cat line.
  136.     # This isn't particularly fast - could be dramatically speeded up
  137.     # if written in C, but I was trying to ensure that this is as simple
  138.     # and self-evident as possible.
  139.  
  140.     awk '
  141.     BEGIN    {
  142.         where = "'"$WHEREFILE"'"
  143.         }
  144.     $1 == "cat" && collecting == 0 {
  145.         recno = 1
  146.         endtoken=$3;
  147.         if (substr(endtoken, 1, 1) == "'"'"'")
  148.             endtoken=substr(endtoken, 2, length(endtoken)-2);
  149.         collecting = 1;
  150.         foundone = 1;
  151.         name = $5;
  152.         if (index(name, "/") != 0) {
  153.             printf("Security violation attempt in %s!\n", "'$i'");
  154.             exit 1;
  155.         } else
  156.             printf("extracting %s from %s\n", name, "'$i'");
  157.         next;
  158.         }
  159.  
  160.         {
  161.         if (!collecting)
  162.             next;
  163.         if ($1 == endtoken) {
  164.             line = "rm -f " name ".Z"
  165.             print "" | line
  166.             collecting = 0;
  167.             next
  168.         }
  169.         if ($1 ~ /^#N/ && where) {
  170.             for (i = 2; i <= NF; i++) {
  171.             sname = $i
  172.             if (p = index(sname, ","))
  173.                 sname = substr(sname, 1, p-1)
  174.             printf "@%s %s %d\n", sname, name, recno >> \
  175.                 "'$WHERETMP'";
  176.             }
  177.         }
  178.         if ("'$NOCOMMENTS'" == 1 && $0 ~ /#/)
  179.             print substr($0, 1, index($0, "#")) > name
  180.         else {
  181.             print $0 > name
  182.         }
  183.         recno++
  184.         }
  185.         
  186.         END {
  187.         if (collecting) {
  188.             printf("Non-terminated map in %s\n", "'$i'");
  189.             exit 1;
  190.         }
  191.         if (!foundone) {
  192.             printf("%s does not contain a properly formed map\n", "'$i'");
  193.             exit 1;
  194.         }
  195.         }' $i
  196.  
  197.     if test $? = 0 -a -n "$UNLINK"
  198.     then
  199.         rm -f $i
  200.     done
  201.  
  202.     done < $BATCH.work
  203.     rm $BATCH.work
  204. done > /tmp/unpA$$ 2>&1
  205.  
  206. if test -n "$COMPRESS"
  207. then
  208.     files=`ls ?.* | sed -e '/\.Z$/d'`
  209.     if test -n "$files"
  210.     then
  211.     $COMPRESS -f $files
  212.     fi
  213. fi
  214.  
  215. if test -f "$PATHALIAS" -a "$forcepath" != false
  216. then
  217.     if test -s /tmp/unpA$$ -o "$forcepath" = true
  218.     then
  219.     cd $MAPDIR
  220.  
  221.     (
  222.     if test -n "$COMPRESS"
  223.     then
  224.         $COMPRESS -dc [ud].*.Z | cat - $PATHLOCAL
  225.     else
  226.         cat [ud].* $PATHLOCAL
  227.     fi |
  228.  
  229.     $PATHALIAS -f $PATHOPTS |
  230.  
  231.     # format of the pathalias -f output is
  232.     # cost    host    route
  233.     #
  234.     # format of a 'paths' file for smail is
  235.     # host    route    first_hop_cost
  236.     #
  237.     # move cost field to end of line:
  238.  
  239.     sed 's/\(.*\)    \(.*\)    \(.*\)/\2    \3    \1/' |
  240.  
  241.     # convert target domain/host to lower case:
  242.  
  243.     #lcasep |
  244.     
  245.     # remove some additional wierdnesses (per Peter Honeyman):
  246.     # You can leave it in or not.
  247.  
  248.     # egrep -v '(\.(com|edu|mil|gov|net|org|arpa|[a-z][a-z])    .*!.*!)|(.\.(com|edu|mil|gov|net|org|arpa|[a-z][a-z])    )' |
  249.  
  250.     # sort the stream:
  251.     
  252.     sort > /tmp/paths ) > /tmp/unpB$$ 2>&1
  253.  
  254.     if test ! -s /tmp/paths
  255.     then
  256.         echo "Pathalias failed no map file created" >> /tmp/unpB$$
  257.     else
  258.         cat /tmp/paths > $PATHFILE 2>> /tmp/unpB$$
  259.         if test $? != 0
  260.         then
  261.         echo "Copy to $PATHFILE failed" >> /tmp/unpB$$
  262.         else
  263.         rm /tmp/paths
  264.         fi
  265.         echo "Map remade" >> /tmp/unpB$$
  266.         ls -l $PATHFILE >> /tmp/unpB$$
  267.     fi
  268.  
  269.     if test -s /tmp/unpB$$
  270.     then
  271.         echo "Pathalias output:" >> /tmp/unpA$$
  272.         cat /tmp/unpB$$ >> /tmp/unpA$$
  273.     fi
  274.     fi
  275. fi
  276.  
  277. if test -n "$WHEREFILE" -a -s $WHERETMP
  278. then
  279.     if test ! -f $WHEREFILE
  280.     then
  281.     touch $WHEREFILE
  282.     fi
  283.  
  284.     # First awk: throws away WHERE references in $WHEREFILE that
  285.     #    are now in $WHERETMP
  286.     # Sort: sort by site name
  287.     # Second awk: coalesce references to same site/file to one line.
  288.     awk '
  289.     BEGIN {
  290.         mapseen[""] = 1
  291.     }
  292.     $1 ~ /^@/ {
  293.         printf("%s %s %s\n", substr($1, 2), $2, $3);
  294.         mapseen[$2] = 1
  295.         next;
  296.     }
  297.     {
  298.         if (mapseen[$2])
  299.         next
  300.         printf("%s %s %s\n", $1, $2, $3);
  301.     }' $WHERETMP $WHEREFILE | 
  302.     sort | 
  303.     awk '
  304.     {
  305.         if (site != $1 || map != $2) {
  306.         if (site)
  307.             printf("\n");
  308.         site = $1
  309.         map = $2
  310.         printf("%s %s %s", $1, $2, $3);
  311.         } else
  312.         printf(",%s", $3);
  313.     }
  314.     END {
  315.         printf("\n");
  316.     }' > /tmp/TMP2
  317.     
  318.     if test -s /tmp/TMP2
  319.     then
  320.     cat /tmp/TMP2 > $WHEREFILE
  321.     fi
  322.     echo "Where database ($WHEREFILE) rebuilt" >> /tmp/unpA$$
  323. fi
  324. rm -f /tmp/TMP2 $WHERETMP
  325.  
  326. if test -s /tmp/unpA$$
  327. then
  328.     mail $NOTIFY < /tmp/unpA$$
  329. fi
  330.