/**************************************************************************** Microsoft RPC Version 1`1 Copyright Microsoft Corp. 1992 Hello Example FILE: kerbcli.c USAGE: client -n network_address -p protocol_sequence -e endpoint -o options PURPOSE: Client side of RPC distributed application FUNCTIONS: main() - binds to server and calls remote procedure COMMENTS: This distributed application prints a string such as "hello, world" on the server. The client manages its connection to the server. The client uses the binding handle hello_IfHandle defined in the file hello.h. ****************************************************************************/ #include #include #include #include // RPC API functions, types #include "kerbtest.h" // header file generated by MIDL compiler void Usage(char * pszProgramName) { fprintf(stderr, "Usage: %s\n", pszProgramName); fprintf(stderr, " -p protocol_sequence\n"); fprintf(stderr, " -n network_address\n"); fprintf(stderr, " -d delegation address\n"); fprintf(stderr, " -e endpoint\n"); fprintf(stderr, " -o options\n"); fprintf(stderr, " -a authn level\n"); fprintf(stderr, " -s authn service\n"); fprintf(stderr, " -r recursiion level\n"); fprintf(stderr, " -x shutdown server\n"); fprintf(stderr, " -# number of times to call\n"); fprintf(stderr, " -t target principal\n"); exit(1); } int __cdecl main (argc, argv) int argc; char *argv[]; { RPC_STATUS status; // returned by RPC API function unsigned char * pszProtocolSequence = "ncacn_ip_tcp"; unsigned char * pszNetworkAddress = NULL; unsigned char * pszEndpoint = "30760"; unsigned char * pszOptions = NULL; unsigned char * pszStringBinding = NULL; unsigned char * pszDelegationAddress = NULL; unsigned char * pszPrincipal = NULL; unsigned char PrincipalBuffer[100]; ULONG PrincipalLength; ULONG AuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT; ULONG AuthnService = RPC_C_AUTHN_DCE_PRIVATE; ULONG RecursionLevel = 0; ULONG LoopCount = 1; BOOLEAN ShutdownService = FALSE; ULONG i; handle_t BindingHandle = NULL; // allow the user to override settings with command line switches for (i = 1; i < (ULONG) argc; i++) { if ((*argv[i] == '-') || (*argv[i] == '/')) { switch (tolower(*(argv[i]+1))) { case 'p': // protocol sequence pszProtocolSequence = argv[++i]; break; case 'n': // network address pszNetworkAddress = argv[++i]; break; case 'd': // network address pszDelegationAddress = argv[++i]; break; case 'e': pszEndpoint = argv[++i]; break; case 'o': pszOptions = argv[++i]; break; case 't': pszPrincipal = argv[++i]; break; case 'a': sscanf(argv[++i],"%d",&AuthnLevel); break; case 's': sscanf(argv[++i],"%d",&AuthnService); break; case 'r': sscanf(argv[++i],"%d",&RecursionLevel); break; case '#': sscanf(argv[++i],"%d",&LoopCount); break; case 'x': ShutdownService = TRUE; break; case 'h': case '?': default: Usage(argv[0]); } } else { Usage(argv[0]); } } // // If the principal is NULL, get it from the environment // if (pszPrincipal == NULL) { LPSTR pszUserRealm; LPSTR pszUserName; PrincipalBuffer[0] = '\0'; pszUserRealm = getenv( "USERDOMAIN" ); pszUserName = getenv( "USERNAME" ); if (pszUserRealm != NULL) { strcpy(PrincipalBuffer, pszUserRealm); } if ((pszUserRealm != NULL) && (pszUserName != NULL)) { strcat(PrincipalBuffer,"\\"); } if (pszUserName != NULL) { strcat(PrincipalBuffer,pszUserName); } pszPrincipal = PrincipalBuffer; } // Use a convenience function to concatenate the elements of // the string binding into the proper sequence. status = RpcStringBindingCompose(NULL, pszProtocolSequence, pszNetworkAddress, pszEndpoint, pszOptions, &pszStringBinding); if (status) { printf("RpcStringBindingCompose returned %d\n", status); exit(2); } printf("pszStringBinding = %s\n", pszStringBinding); // // Set the binding handle that will be used to bind to the server. // status = RpcBindingFromStringBinding(pszStringBinding, &BindingHandle); if (status) { printf("RpcBindingFromStringBinding returned %d\n", status); exit(2); } status = RpcStringFree(&pszStringBinding); // remote calls done; unbind if (status) { printf("RpcStringFree returned %d\n", status); exit(2); } // // Tell RPC to do the security thing. // printf("Binding auth info set to level %d, service %d, principal %s\n", AuthnLevel, AuthnService, pszPrincipal ); status = RpcBindingSetAuthInfo( BindingHandle, pszPrincipal, AuthnLevel, AuthnService, NULL, RPC_C_AUTHZ_NAME ); if ( status ) { printf("RpcBindingSetAuthInfo returned %ld\n", status); exit(2); } // // Do the actual RPC calls to the server. // RpcTryExcept { for (i = 0; i < LoopCount ; i++ ) { status = RemoteCall( BindingHandle, 0, // no options for now pszDelegationAddress, pszProtocolSequence, pszEndpoint, pszPrincipal, pszNetworkAddress, AuthnLevel, AuthnService, RecursionLevel ); if (status != 0) { printf("RemoteCall failed: 0x%x\n",status); break; } } if (ShutdownService) { Shutdown( BindingHandle ); } } RpcExcept(EXCEPTION_EXECUTE_HANDLER) { printf("Runtime library reported an exception %d\n", RpcExceptionCode()); } RpcEndExcept // The calls to the remote procedures are complete. // Free the binding handle status = RpcBindingFree(&BindingHandle); // remote calls done; unbind if (status) { printf("RpcBindingFree returned %d\n", status); exit(2); } return 0; } // ==================================================================== // MIDL allocate and free // ==================================================================== void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len) { return(malloc(len)); } void __RPC_API MIDL_user_free(void __RPC_FAR * ptr) { free(ptr); } /* end file helloc.c */