Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
  Previous   Contents   Next 
   
 
Appendix A

Sample C-Based GSS-API Programs

This appendix shows the source code for two sample applications that use GSS-API to make a safe network connection. One application is a client, and the other is a server. The two programs display benchmarks as they run, so that a user can "see" GSS-API being used. Additionally, certain miscellaneous functions are provided for use by the client and server applications. For convenience's sake we have divided each application into its constituent functions.

These programs are examined in detail in Chapter 2, A Walk-Through of the Sample GSS-API Programs.

Client-Side Application

The following sections detail the client-side program, gss_client.

Program Headers

These are the declarations for the client program, plus a function that explains the syntax if an incorrect command line is given.


Example A-1 Client Program Headers

/*
 * Copyright 1994 by OpenVision Technologies, Inc.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of OpenVision not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. OpenVision makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <error.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <gssapi/gssapi.h>
#include <gssapi/gssapi_ext.h>
#include "gss-misc.h"

/* global mech oid needed by display status, and acquire cred */
gss_OID g_mechOid = GSS_C_NULL_OID;


void usage()
{
     fprintf(stderr, "Usage: gss-client [-port port] [-d]"
                        " [-mech mechOid] host service msg\n");
     exit(1);
}

main()

This is the entry point to the program. The program takes the following syntax on the command line:

gss-client [-port port] [-d] [-mech mech] host service msg

After parsing the command line, main() converts the name of the appropriate security mechanism (if provided) to an OID, establishes a secure connection, and then destroys the mechanism OID, if necessary.


Note - main() uses a nonstandard function, gss_release_oid(). This function is not supported by all implementations of the GSS-API and should not be used if possible. Since applications should use a default mechanism (specified by GSS_C_NULL_OID) instead of allocating one of their own, this function should not be needed in any case. It is included here for reasons of backward compatibility and to show the full extent of this implementation of the GSS-API.



Example A-2 main()

int main(argc, argv)
     int argc;
     char **argv;
{
    /* char *service_name, *hostname, *msg; */
     char *msg;
     char service_name[128]; 
     char hostname[128];  
     char *mechanism = 0;
     u_short port = 4444;
     int use_file = 0;
     OM_uint32 deleg_flag = 0, min_stat;
     
     display_file = stdout;

     /* Parse arguments. */

        argc--; argv++;
     while (argc) {
          if (strcmp(*argv, "-port") == 0) {
               argc--; argv++;
               if (!argc) usage();
               port = atoi(*argv);
           } else if (strcmp(*argv, "-mech") == 0) {
               argc--; argv++;
               if (!argc) usage();
               mechanism = *argv;
           } else if (strcmp(*argv, "-d") == 0) {
               deleg_flag = GSS_C_DELEG_FLAG;
          } else if (strcmp(*argv, "-f") == 0) {
               use_file = 1;
          } else
                break;
          argc--; argv++;
     }
     if (argc != 3)
          usage();

     if (argc > 1) {
                strcpy(hostname, argv[0]);
        } else if (gethostname(hostname, sizeof(hostname)) == -1) {
                        perror("gethostname");
                        exit(1);
        }


     if (argc > 2) { 
        strcpy(service_name, argv[1]);
        strcat(service_name, "@");
        strcat(service_name, hostname);
 
     }

      msg = argv[2];
     
     if (mechanism)
         parse_oid(mechanism, &g_mechOid);

     if (call_server(hostname, port, g_mechOid, service_name,
                   deleg_flag, msg, use_file) < 0)
          exit(1);

     if (g_mechOid != GSS_C_NULL_OID)
         (void) gss_release_oid(&min_stat, &gmechOid);
         
     return 0;
}

 
 
 
  Previous   Contents   Next