Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
A.  Sample C-Based GSS-API Programs Server-Side Application Program Headers  Previous   Contents   Next 
   
 

main()

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

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

After parsing the command line, main() converts the name of the desired security mechanism (if provided) to an OID, acquires credentials, establishes a context and receives data, and then destroys the mechanism OID if necessary.


Note - Applications should normally not set the mechanism, but use defaults provided by the GSS-API.



Example A-9 main()

int
main(argc, argv)
     int argc;
     char **argv;
{
     char *service_name, *mechType = NULL;
     gss_cred_id_t server_creds;
     OM_uint32 min_stat;
     u_short port = 4444;
     int s;
     int once = 0;
     int do_inetd = 0;

     log = stdout;
     display_file = stdout;
     argc--; argv++;
     while (argc) {
          if (strcmp(*argv, "-port") == 0) {
               argc--; argv++;
               if (!argc) usage();
               port = atoi(*argv);
          } else if (strcmp(*argv, "-verbose") == 0) {
              verbose = 1;
          } else if (strcmp(*argv, "-once") == 0) {
              once = 1;
          } else if (strcmp(*argv, "-inetd") == 0) {
              do_inetd = 1;
          } else if (strcmp(*argv, "-mech") == 0) {
                argc--; argv++;
                if (!argc)      usage();
                mechType = *argv;
          } else if (strcmp(*argv, "-logfile") == 0) {
              argc--; argv++;
              if (!argc) usage();
              log = fopen(*argv, "a");
              display_file = log;
              if (!log) {
                  perror(*argv);
                  exit(1);
              }
          } else
               break;
          argc--; argv++;
     }
     if (argc != 1)
          usage();

     if ((*argv)[0] == '-')
          usage();

     service_name = *argv;

     if (mechType != NULL) {
             if ((g_mechOid = createMechOid(mechType)) == NULL) {
                     usage();
                     exit(-1);
             }
     }

     if (server_acquire_creds(service_name, g_mechOid, &server_creds) < 0)
         return -1;

     if (do_inetd) {
         close(1);
         close(2);

         sign_server(0, server_creds);
         close(0);
     } else {
         int stmp;

         if ((stmp = create_socket(port))) {
             do {
                 /* Accept a TCP connection */
                 if ((s = accept(stmp, NULL, 0)) < 0) {
                     perror("accepting connection");
                 } else {
                     /* this return value is not checked, because there's
                        not really anything to do if it fails */
                     sign_server(s, server_creds);
                 }
             } while (!once);
         }

         close(stmp);
     }

     (void) gss_release_cred(&min_stat, &server_creds);
     if (g_mechOid != GSS_C_NULL_OID)
             gss_release_oid(&min_stat, &g_mechOid);

     /*NOTREACHED*/
     (void) close(s);
     return 0;
}

createMechOid()

This function is shown for completeness' sake. Normally, you should use the default mechanism (specified by GSS_C_NULL_OID).


Example A-10 createMechOid()

gss_OID createMechOid(const char *mechStr)
{
        gss_buffer_desc mechDesc;
        gss_OID mechOid;
        OM_uint32 minor;

        if (mechStr == NULL)
                return (GSS_C_NULL_OID);

        mechDesc.length = strlen(mechStr);
        mechDesc.value = (void *) mechStr;

        if (gss_str_to_oid(&minor, &mechDesc, &mechOid) !
= GSS_S_COMPLETE) {
                fprintf(stderr, "Invalid mechanism oid specified <%s>",
                                mechStr);
                return (GSS_C_NULL_OID);
        }

        return (mechOid);
}

 
 
 
  Previous   Contents   Next