3 * Copyright 1989 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, please see the file
8 * Simple UDP-based sample client program. For demonstration.
9 * This program performs no useful function.
13 RCSID("$Id: simple_client.c,v 1.15 1999/11/13 06:29:01 assar Exp $");
15 #define MSG "hi, Jennifer!" /* message text */
18 talkto(char *hostname, char *service, int port)
20 int flags = 0; /* flags for sendto() */
22 u_long cksum = 0L; /* cksum not used */
23 char c_realm[REALM_SZ]; /* local Kerberos realm */
24 char *s_realm; /* server's Kerberos realm */
26 KTEXT_ST k; /* Kerberos data */
31 struct sockaddr_in s_sock; /* server address */
32 char myhostname[MaxHostNameLen]; /* local hostname */
34 /* for krb_mk_safe/priv */
35 struct sockaddr_in c_sock; /* client address */
36 CREDENTIALS c; /* ticket & session key */
37 CREDENTIALS *cred = &c;
40 des_key_schedule sched; /* session key schedule */
42 /* Look up server host */
43 if ((host = gethostbyname(hostname)) == NULL) {
44 fprintf(stderr, "%s: unknown host \n", hostname);
48 /* Set server's address */
49 memset(&s_sock, 0, sizeof(s_sock));
50 memcpy(&s_sock.sin_addr, host->h_addr, sizeof(s_sock.sin_addr));
51 s_sock.sin_family = AF_INET;
53 s_sock.sin_port = port;
55 s_sock.sin_port = k_getportbyname (service, "tcp", htons(SAMPLE_PORT));
57 if (gethostname(myhostname, sizeof(myhostname)) < 0) {
62 if ((host = gethostbyname(myhostname)) == NULL) {
63 fprintf(stderr, "%s: unknown host\n", myhostname);
68 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
69 warn("socket SOCK_DGRAM");
73 memset(&c_sock, 0, sizeof(c_sock));
74 memcpy(&c_sock.sin_addr, host->h_addr, sizeof(c_sock.sin_addr));
75 c_sock.sin_family = AF_INET;
77 /* Bind it to set the address; kernel will fill in port # */
78 if (bind(sock, (struct sockaddr *)&c_sock, sizeof(c_sock)) < 0) {
83 /* Get local realm, not needed, just an example */
84 if ((i = krb_get_lrealm(c_realm, 1)) != KSUCCESS) {
85 fprintf(stderr, "can't find local Kerberos realm\n");
88 printf("Local Kerberos realm is %s\n", c_realm);
90 /* Get Kerberos realm of host */
91 s_realm = krb_realmofhost(hostname);
93 /* PREPARE KRB_MK_REQ MESSAGE */
95 /* Get credentials for server, create krb_mk_req message */
96 if ((i = krb_mk_req(ktxt, service, hostname, s_realm, cksum))
98 fprintf(stderr, "%s\n", krb_get_err_text(i));
101 printf("Got credentials for %s.\n", service);
103 /* Send authentication info to server */
104 i = sendto(sock, (char *)ktxt->dat, ktxt->length, flags,
105 (struct sockaddr *)&s_sock, sizeof(s_sock));
107 warn("sending datagram message");
108 printf("Sent authentication data: %d bytes\n", i);
110 /* PREPARE KRB_MK_SAFE MESSAGE */
113 memset(&c_sock, 0, sizeof(c_sock));
115 if (getsockname(sock, (struct sockaddr *)&c_sock, &i) < 0) {
120 /* Get session key */
121 i = krb_get_cred(service, hostname, s_realm, cred);
125 /* Make the safe message */
126 len = krb_mk_safe(MSG, ktxt->dat, strlen(MSG)+1,
127 &cred->session, &c_sock, &s_sock);
130 i = sendto(sock, (char *)ktxt->dat, (int) len, flags,
131 (struct sockaddr *)&s_sock, sizeof(s_sock));
133 warn("sending safe message");
134 printf("Sent checksummed message: %d bytes\n", i);
136 /* PREPARE KRB_MK_PRIV MESSAGE */
139 memset(sched, 0, sizeof(sched));
141 /* Get key schedule for session key */
142 des_key_sched(&cred->session, sched);
145 /* Make the encrypted message */
146 len = krb_mk_priv(MSG, ktxt->dat, strlen(MSG)+1,
147 sched, &cred->session, &c_sock, &s_sock);
150 i = sendto(sock, (char *)ktxt->dat, (int) len, flags,
151 (struct sockaddr *)&s_sock, sizeof(s_sock));
153 warn("sending encrypted message");
154 printf("Sent encrypted message: %d bytes\n", i);
161 fprintf (stderr, "Usage: %s [-s service] [-p port] hostname\n",
167 main(int argc, char **argv)
171 char service[SNAME_SZ];
172 struct servent *serv;
175 set_progname (argv[0]);
177 strlcpy (service, SAMPLE_SERVICE, sizeof(service));
179 while ((c = getopt(argc, argv, "s:p:")) != -1)
182 strlcpy (service, optarg, sizeof(service));
185 serv = getservbyname (optarg, "tcp");
189 port = htons(atoi(optarg));
200 ret &= talkto (*argv++, service, port);