2
* appl/user_user/client.c
4
* Copyright 1991 by the Massachusetts Institute of Technology.
7
* Export of this software from the United States of America may
8
* require a specific license from the United States Government.
9
* It is the responsibility of any person or organization contemplating
10
* export to obtain such a license before exporting.
12
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13
* distribute this software and its documentation for any purpose and
14
* without fee is hereby granted, provided that the above copyright
15
* notice appear in all copies and that both that copyright notice and
16
* this permission notice appear in supporting documentation, and that
17
* the name of M.I.T. not be used in advertising or publicity pertaining
18
* to distribution of the software without specific, written prior
19
* permission. Furthermore if you modify this software you must label
20
* your software as modified software and not distribute it in such a
21
* fashion that it might be confused with the original M.I.T. software.
22
* M.I.T. makes no representations about the suitability of
23
* this software for any purpose. It is provided "as is" without express
24
* or implied warranty.
27
* Other end of user-user client/server pair.
30
#include <sys/types.h>
31
#include <sys/socket.h>
32
#include <netinet/in.h>
33
#include <arpa/inet.h>
47
register int retval, i;
48
char *hname; /* full name of server */
49
char **srealms; /* realm(s) of server */
50
char *princ; /* principal in credentials cache */
53
struct sockaddr_in serv_net_addr, cli_net_addr;
55
krb5_creds creds, *new_creds;
56
krb5_data reply, msg, princ_data;
57
krb5_auth_context auth_context = NULL;
58
krb5_ticket * ticket = NULL;
62
if (argc < 2 || argc > 4) {
63
fputs ("usage: uu-client <hostname> [message [port]]\n", stderr);
67
retval = krb5_init_context(&context);
69
com_err(argv[0], retval, "while initializing krb5");
74
port = htons(atoi(argv[3]));
76
else if ((serv = getservbyname ("uu-sample", "tcp")) == NULL)
78
fputs ("uu-client: unknown service \"uu-sample/tcp\"\n", stderr);
84
if ((host = gethostbyname (argv[1])) == NULL) {
85
fprintf (stderr, "uu-client: can't get address of host \"%s\".\n",
90
if (host->h_addrtype != AF_INET) {
91
fprintf (stderr, "uu-client: bad address type %d for \"%s\".\n",
92
host->h_addrtype, argv[1]);
96
hname = strdup (host->h_name);
99
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
100
com_err ("uu-client", errno, "creating socket");
103
cli_net_addr.sin_family = AF_INET;
104
cli_net_addr.sin_port = 0;
105
cli_net_addr.sin_addr.s_addr = 0;
106
if (bind (s, (struct sockaddr *)&cli_net_addr,
107
sizeof (cli_net_addr)) < 0) {
108
com_err ("uu-client", errno, "binding socket");
113
serv_net_addr.sin_family = AF_INET;
114
serv_net_addr.sin_port = port;
118
if (host->h_addr_list[i] == 0) {
119
fprintf (stderr, "uu-client: unable to connect to \"%s\"\n", hname);
123
memcpy ((char *)&serv_net_addr.sin_addr, host->h_addr_list[i++],
124
sizeof(serv_net_addr.sin_addr));
126
if (connect(s, (struct sockaddr *)&serv_net_addr,
127
sizeof (serv_net_addr)) == 0)
129
com_err ("uu-client", errno, "connecting to \"%s\" (%s).",
130
hname, inet_ntoa(serv_net_addr.sin_addr));
136
retval = krb5_cc_default(context, &cc);
138
com_err("uu-client", retval, "getting credentials cache");
142
memset ((char*)&creds, 0, sizeof(creds));
144
retval = krb5_cc_get_principal(context, cc, &creds.client);
146
com_err("uu-client", retval, "getting principal name");
150
retval = krb5_unparse_name(context, creds.client, &princ);
152
com_err("uu-client", retval, "printing principal name");
156
fprintf(stderr, "uu-client: client principal is \"%s\".\n", princ);
158
retval = krb5_get_host_realm(context, hname, &srealms);
160
com_err("uu-client", retval, "getting realms for \"%s\"", hname);
165
krb5_build_principal_ext(context, &creds.server,
166
krb5_princ_realm(context, creds.client)->length,
167
krb5_princ_realm(context, creds.client)->data,
169
krb5_princ_realm(context, creds.client)->length,
170
krb5_princ_realm(context, creds.client)->data,
173
com_err("uu-client", retval, "setting up tgt server name");
177
/* Get TGT from credentials cache */
178
retval = krb5_get_credentials(context, KRB5_GC_CACHED, cc,
181
com_err("uu-client", retval, "getting TGT");
185
i = strlen(princ) + 1;
187
fprintf(stderr, "uu-client: sending %d bytes\n",new_creds->ticket.length + i);
188
princ_data.data = princ;
189
princ_data.length = i; /* include null terminator for
190
server's convenience */
191
retval = krb5_write_message(context, (krb5_pointer) &s, &princ_data);
193
com_err("uu-client", retval, "sending principal name to server");
199
retval = krb5_write_message(context, (krb5_pointer) &s, &new_creds->ticket);
201
com_err("uu-client", retval, "sending ticket to server");
205
retval = krb5_read_message(context, (krb5_pointer) &s, &reply);
207
com_err("uu-client", retval, "reading reply from server");
211
retval = krb5_auth_con_init(context, &auth_context);
213
com_err("uu-client", retval, "initializing the auth_context");
218
krb5_auth_con_genaddrs(context, auth_context, s,
219
KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR |
220
KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR);
222
com_err("uu-client", retval, "generating addrs for auth_context");
226
retval = krb5_auth_con_setflags(context, auth_context,
227
KRB5_AUTH_CONTEXT_DO_SEQUENCE);
229
com_err("uu-client", retval, "initializing the auth_context flags");
233
retval = krb5_auth_con_setuseruserkey(context, auth_context,
234
&new_creds->keyblock);
236
com_err("uu-client", retval, "setting useruserkey for authcontext");
241
/* read the ap_req to get the session key */
242
retval = krb5_rd_req(context, &auth_context, &reply,
243
NULL, NULL, NULL, &ticket);
246
retval = krb5_recvauth(context, &auth_context, (krb5_pointer)&s, "???",
247
0, /* server */, 0, NULL, &ticket);
251
com_err("uu-client", retval, "reading AP_REQ from server");
255
retval = krb5_unparse_name(context, ticket->enc_part2->client, &princ);
257
com_err("uu-client", retval, "while unparsing client name");
259
printf("server is named \"%s\"\n", princ);
263
retval = krb5_read_message(context, (krb5_pointer) &s, &reply);
265
com_err("uu-client", retval, "reading reply from server");
269
retval = krb5_rd_safe(context, auth_context, &reply, &msg, NULL);
271
com_err("uu-client", retval, "decoding reply from server");
275
printf ("uu-client: server says \"%s\".\n", msg.data);