Solaris 开发者安全性指南

GSSAPI 客户机示例: main() 函数

与所有 C 程序一样,该程序的外部 shell 也包含在入口点函数 main() 中。main() 可执行以下四种功能:

main() 例程的源代码如以下示例所示。


注 –

此示例的源代码也可以通过 Sun 下载中心获取。请访问 http://www.sun.com/download/products.xml?id=41912db5



示例 5–1 gss-client 示例:main()

int main(argc, argv)

     int argc;

     char **argv;

{

     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 command-line 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];



     /* Create GSSAPI object ID. */

     if (mechanism)

         parse_oid(mechanism, &g_mechOid);



     /* Call server to create context and send data. */

     if (call_server(hostname, port, g_mechOid, service_name,

                   deleg_flag, msg, use_file) < 0)

          exit(1);



     /* Release storage space for OID, if still allocated  */

     if (g_mechOid != GSS_C_NULL_OID)

         (void) gss_release_oid(&min_stat, &gmechOid);

         

     return 0;

}