Trusted Solaris Developer's Guide

Client Program

This part of the client program accepts command line inputs and creates a client handle.

#include <stdio.h>
#include <stdlib.h>
#include <rpc/rpc.h>
#include <netdb.h>
#include <tsix/t6attrs.h>
#include "rpc_test.h"

extern int
main(int argc, char *argv[])
{
	struct timeval time_out;
	CLIENT *handlep;
	enum clnt_stat stat;
	int input, output;
	uid_t uid;
	if (argc < 2 || argc > 3) {
		fprintf(stderr,
			"Usage: simple_rpc_clnt_test HOSTNAME [UID]\n");
		exit(1);
	}

	handlep = clnt_create(argv[1], RPC_TEST_PROG,
		RPC_TEST_VERS, "udp");
		if (handlep == (CLIENT *) NULL) {
			fprintf(stderr, "Couldn't create client%s.\n",
				clnt_spcreateerror(""));
		exit(1);
	}

This part of the client program sets the client handle to point to the space allocated for the user ID to be input from the command line, sets the user ID value, sends the value to the server process, and waits for the server response. The client prints out the server response before it exits.

The client program needs the net_setid privilege in its effective set to send a changed outgoing user ID. The code comments indicate where privilege bracketing should occur.

	if (argc == 3) {
		handlep->cl_tsol_outgoing_attrsp = t6alloc_blk(T6M_UID);
		if (handlep->cl_tsol_outgoing_attrsp == NULL) {
			fprintf(stderr, "Can't create attr buffer\n");
			exit(1);
		}

		printf ("Sending UID %s\n", argv[2]);
		uid = atoi(argv[2]);
		if (t6set_attr(T6_UID, &uid,
			handlep->cl_tsol_outgoing_attrsp) != 0) {
			fprintf(stderr, "Error returned by t6set_attr.\n");
			exit(1);
		}
	}
	time_out.tv_sec = 30;
	time_out.tv_usec = 0;
	input = 3;

/* Turn net_uid on in the effective set */
	stat = clnt_call(handlep, RPC_TEST_DOUBLE1, xdr_int,
		(caddr_t) &input, xdr_int, (caddr_t) &output, time_out);
	if (stat != RPC_SUCCESS) {
		fprintf(stderr, "Call failed. %s.\n",
			clnt_sperror(handlep, ""));
			exit(1);
	}
/* Turn off the net_uid privilege */

	printf("Response received: %d\n", output);
	(void) clnt_destroy(handlep);

	return (0);
}