home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / sockauth / server.c < prev   
Encoding:
C/C++ Source or Header  |  1997-10-09  |  3.9 KB  |  213 lines

  1. /*++
  2.  
  3. Copyright 1996-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     server.c
  8.  
  9. Abstract:
  10.  
  11.     A command line app that establishes an authenticated connection
  12.     with a client.
  13.  
  14. Revision History:
  15.  
  16. --*/
  17.  
  18. #include <windows.h>
  19. #include <winsock.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "security.h"
  23. #include "comm.h"
  24.  
  25. BOOL AcceptAuthSocket (SOCKET *s);
  26. BOOL CloseAuthSocket (SOCKET s);
  27. BOOL DoAuthentication (SOCKET s);
  28.  
  29. static PBYTE g_pInBuf = NULL;
  30. static PBYTE g_pOutBuf = NULL;
  31. static DWORD g_cbMaxMessage = 0;
  32. static unsigned short g_usPort = 2000;
  33.  
  34. void main ()
  35. {
  36.     char szUser[80];
  37.     DWORD cbUser = 80;
  38.     SOCKET s;
  39.  
  40.     // initialize
  41.     //
  42.     if (!InitWinsock ())
  43.         exit (EXIT_FAILURE);
  44.  
  45.     if (!InitPackage (&g_cbMaxMessage))
  46.         exit (EXIT_FAILURE);
  47.  
  48.     g_pInBuf = (PBYTE) malloc (g_cbMaxMessage);
  49.     g_pOutBuf = (PBYTE) malloc (g_cbMaxMessage);
  50.     if (NULL == g_pInBuf || NULL == g_pOutBuf)
  51.         exit (EXIT_FAILURE);
  52.  
  53.     // Make an authenticated connection with client
  54.     //
  55.     if (!AcceptAuthSocket (&s))
  56.         exit (EXIT_FAILURE);
  57.  
  58.     // impersonate the client
  59.     //
  60.     if (!ImpersonateContext (s))
  61.         exit (EXIT_FAILURE);
  62.  
  63.     // get the user name
  64.     //
  65.     if (!GetUserName (szUser, &cbUser))
  66.         exit (EXIT_FAILURE);
  67.  
  68.     // revert to self
  69.     //
  70.     if (!RevertContext (s))
  71.         exit (EXIT_FAILURE);
  72.  
  73.     // send user name to client
  74.     //
  75.     if (!SendBytes (s, szUser, cbUser))
  76.         exit (EXIT_FAILURE);
  77.  
  78.     // terminate and cleanup
  79.     //
  80.     CloseAuthSocket (s);
  81.  
  82.     TermPackage ();
  83.  
  84.     TermWinsock ();
  85.  
  86.     free (g_pInBuf);
  87.     free (g_pOutBuf);
  88.  
  89.     exit (EXIT_SUCCESS);
  90. }
  91.  
  92. BOOL AcceptAuthSocket (SOCKET *s)
  93. /*++
  94.  
  95.  Routine Description:
  96.  
  97.     Establishes an authenticated socket connection with a client and
  98.     initializes any needed security package resources.
  99.  
  100.  Return Value:
  101.  
  102.     Returns TRUE is successful; otherwise FALSE is returned.
  103.  
  104. --*/
  105. {
  106.     SOCKET sockListen;
  107.     SOCKET sockClient;
  108.     SOCKADDR_IN sin;
  109.     int nRes;
  110.  
  111.     // create listening socket
  112.     //
  113.     sockListen = socket (PF_INET, SOCK_STREAM, 0);
  114.     if (INVALID_SOCKET == sockListen)  {
  115.         fprintf (stderr, "Failed to create socket: %u\n", GetLastError ());
  116.         return(FALSE);
  117.     }
  118.  
  119.     // bind to local port
  120.     //
  121.     sin.sin_family = AF_INET;
  122.     sin.sin_addr.s_addr = 0;
  123.     sin.sin_port = htons(g_usPort);
  124.     nRes = bind (sockListen, (LPSOCKADDR) &sin, sizeof (sin));
  125.     if (SOCKET_ERROR == nRes)  {
  126.         fprintf (stderr, "bind failed: %u\n", GetLastError ());
  127.         return(FALSE);
  128.     }
  129.  
  130.     // listen for client
  131.     //
  132.     nRes = listen (sockListen, 1);
  133.     if (SOCKET_ERROR == nRes)  {
  134.         fprintf (stderr, "listen failed: %u\n", GetLastError ());
  135.         return(FALSE);
  136.     }
  137.  
  138.     // accept client
  139.     //
  140.     sockClient = accept (sockListen, NULL, NULL);
  141.     if (INVALID_SOCKET == sockClient)  {
  142.         fprintf (stderr, "accept failed: %u\n", GetLastError ());
  143.         return(FALSE);
  144.     }
  145.  
  146.     closesocket (sockListen);
  147.  
  148.     if (!InitSession (sockClient))
  149.         return(FALSE);
  150.  
  151.     if (!DoAuthentication (sockClient))
  152.         return(FALSE);
  153.  
  154.     // return socket
  155.     //
  156.     *s = sockClient;
  157.  
  158.     return(TRUE);
  159. }    
  160.  
  161. BOOL CloseAuthSocket (SOCKET s)
  162. /*++
  163.  
  164.  Routine Description:
  165.  
  166.     Closes a socket and releases security resources associated with
  167.     the socket
  168.  
  169.  Return Value:
  170.  
  171.     Returns TRUE is successful; otherwise FALSE is returned.
  172.  
  173. --*/
  174. {
  175.     TermSession (s);
  176.     shutdown (s, 2);
  177.     closesocket (s);
  178.     return(TRUE);
  179. }    
  180.  
  181. BOOL DoAuthentication (SOCKET s)
  182. /*++
  183.  
  184.  Routine Description:
  185.  
  186.     Manges the authentication conversation with the client via the
  187.     supplied socket handle.
  188.  
  189.  Return Value:
  190.  
  191.     Returns TRUE is successful; otherwise FALSE is returned.
  192.  
  193. --*/
  194. {
  195.     DWORD cbIn, cbOut;
  196.     BOOL done = FALSE;
  197.     
  198.     do {
  199.         if (!ReceiveMsg (s, g_pInBuf, g_cbMaxMessage, &cbIn))
  200.             return(FALSE);
  201.         
  202.         cbOut = g_cbMaxMessage;
  203.         if (!GenServerContext (s, g_pInBuf, cbIn, g_pOutBuf, &cbOut, &done))
  204.             return(FALSE);
  205.         
  206.         if (!SendMsg (s, g_pOutBuf, cbOut))
  207.             return(FALSE);
  208.     }
  209.     while(!done);
  210.  
  211.     return(TRUE);
  212. }    
  213.