3 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
5 * For copying and distribution information,
6 * please see the file <mit-copyright.h>.
9 * A sample Kerberos client, which connects to a server on a remote host,
10 * at port "sample" (be sure to define it in /etc/services)
11 * and authenticates itself to the server. The server then writes back
12 * (in ASCII) the authenticated name.
15 * sample_client <hostname> <checksum>
17 * <hostname> is the name of the foreign host to contact.
19 * <checksum> is an integer checksum to be used for the call to krb_mk_req()
20 * and mutual authentication
26 RCSID("$Id: sample_client.c,v 1.21 1999/11/13 06:27:01 assar Exp $");
31 fprintf (stderr, "Usage: %s [-s service] [-p port] hostname checksum\n",
37 main(int argc, char **argv)
40 struct sockaddr_in sin, lsin;
50 des_key_schedule sched;
53 char service[SNAME_SZ];
58 set_progname (argv[0]);
59 strlcpy (service, SAMPLE_SERVICE, sizeof(service));
62 while ((c = getopt(argc, argv, "s:p:")) != -1)
65 strlcpy (service, optarg, sizeof(service));
68 serv = getservbyname (optarg, "tcp");
72 port = htons(atoi(optarg));
85 /* convert cksum to internal rep */
86 cksum = atoi(argv[1]);
88 printf("Setting checksum to %ld\n", (long)cksum);
90 /* clear out the structure first */
91 memset(&sin, 0, sizeof(sin));
92 sin.sin_family = AF_INET;
96 sin.sin_port = k_getportbyname (service, "tcp", htons(SAMPLE_PORT));
98 /* look up the server host */
99 hp = gethostbyname(argv[0]);
101 errx (1, "gethostbyname(%s): %s", argv[0],
104 /* copy the hostname into non-volatile storage */
105 remote_host = strdup(hp->h_name);
106 if (remote_host == NULL)
107 errx (1, "strdup: out of memory");
109 /* set up the address of the foreign socket for connect() */
110 sin.sin_family = hp->h_addrtype;
112 for (h_addr_list = hp->h_addr_list;
115 memcpy(&sin.sin_addr, *h_addr_list, sizeof(sin.sin_addr));
116 fprintf (stderr, "Trying %s...\n", inet_ntoa(sin.sin_addr));
118 /* open a TCP socket */
119 sock = socket(PF_INET, SOCK_STREAM, 0);
123 /* connect to the server */
124 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
129 if (*h_addr_list == NULL)
132 /* find out who I am, now that we are connected and therefore bound */
133 namelen = sizeof(lsin);
134 if (getsockname(sock, (struct sockaddr *) &lsin, &namelen) < 0) {
136 err (1, "getsockname");
139 /* call Kerberos library routine to obtain an authenticator,
140 pass it over the socket to the server, and obtain mutual
143 authopts = KOPT_DO_MUTUAL;
144 status = krb_sendauth(authopts, sock, &ticket,
145 service, remote_host,
146 NULL, cksum, &msg_data, &cred,
147 sched, &lsin, &sin, SAMPLE_VERSION);
148 if (status != KSUCCESS)
149 errx (1, "cannot authenticate to server: %s",
150 krb_get_err_text(status));
152 /* After we send the authenticator to the server, it will write
153 back the name we authenticated to. Read what it has to say. */
154 status = read(sock, buf, sizeof(buf));
158 /* make sure it's null terminated before printing */
159 if (status < sizeof(buf))
162 buf[sizeof(buf) - 1] = '\0';
164 printf("The server says:\n%s\n", buf);