~ubuntu-branches/ubuntu/oneiric/likewise-open/oneiric

« back to all changes in this revision

Viewing changes to krb5/src/appl/user_user/client.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Salley
  • Date: 2010-11-22 12:06:00 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122120600-8lba1fpceot71wlb
Tags: 6.0.0.53010-1
Likewise Open 6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * appl/user_user/client.c
 
3
 *
 
4
 * Copyright 1991 by the Massachusetts Institute of Technology.
 
5
 * All Rights Reserved.
 
6
 *
 
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.
 
11
 * 
 
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.
 
25
 * 
 
26
 *
 
27
 * Other end of user-user client/server pair.
 
28
 */
 
29
 
 
30
#include <sys/types.h>
 
31
#include <sys/socket.h>
 
32
#include <netinet/in.h>
 
33
#include <arpa/inet.h>
 
34
#include <netdb.h>
 
35
#include <stdio.h>
 
36
#include <string.h>
 
37
#include <errno.h>
 
38
 
 
39
#include "k5-int.h"
 
40
#include "com_err.h"
 
41
 
 
42
int main (argc, argv)
 
43
int argc;
 
44
char *argv[];
 
45
{
 
46
  int s;
 
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 */
 
51
  struct servent *serv;
 
52
  struct hostent *host;
 
53
  struct sockaddr_in serv_net_addr, cli_net_addr;
 
54
  krb5_ccache cc;
 
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;
 
59
  krb5_context context;
 
60
  unsigned short port;
 
61
 
 
62
  if (argc < 2 || argc > 4) {
 
63
      fputs ("usage: uu-client <hostname> [message [port]]\n", stderr);
 
64
      return 1;
 
65
  }
 
66
 
 
67
  retval = krb5_init_context(&context);
 
68
  if (retval) {
 
69
      com_err(argv[0], retval, "while initializing krb5");
 
70
      exit(1);
 
71
  }
 
72
 
 
73
  if (argc == 4) {
 
74
      port = htons(atoi(argv[3]));
 
75
  }
 
76
  else if ((serv = getservbyname ("uu-sample", "tcp")) == NULL)
 
77
  {
 
78
      fputs ("uu-client: unknown service \"uu-sample/tcp\"\n", stderr);
 
79
      return 2;
 
80
  } else {
 
81
      port = serv->s_port;
 
82
  }
 
83
 
 
84
  if ((host = gethostbyname (argv[1])) == NULL) {
 
85
      fprintf (stderr, "uu-client: can't get address of host \"%s\".\n", 
 
86
               argv[1]);
 
87
      return 3;
 
88
  }
 
89
  
 
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]);
 
93
      return 3;
 
94
  }
 
95
 
 
96
  hname = strdup (host->h_name);
 
97
 
 
98
#ifndef USE_STDOUT
 
99
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 
100
      com_err ("uu-client", errno, "creating socket");
 
101
      return 4;
 
102
  } else {
 
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");
 
109
          return 4;
 
110
      }
 
111
  }
 
112
  
 
113
  serv_net_addr.sin_family = AF_INET;
 
114
  serv_net_addr.sin_port = port;
 
115
 
 
116
  i = 0;
 
117
  while (1) {
 
118
      if (host->h_addr_list[i] == 0) {
 
119
          fprintf (stderr, "uu-client: unable to connect to \"%s\"\n", hname);
 
120
          return 5;
 
121
      }
 
122
 
 
123
      memcpy ((char *)&serv_net_addr.sin_addr, host->h_addr_list[i++], 
 
124
              sizeof(serv_net_addr.sin_addr));
 
125
 
 
126
      if (connect(s, (struct sockaddr *)&serv_net_addr, 
 
127
                  sizeof (serv_net_addr)) == 0)
 
128
          break;
 
129
      com_err ("uu-client", errno, "connecting to \"%s\" (%s).",
 
130
               hname, inet_ntoa(serv_net_addr.sin_addr));
 
131
  }
 
132
#else
 
133
  s = 1;
 
134
#endif
 
135
 
 
136
  retval = krb5_cc_default(context, &cc);
 
137
  if (retval) {
 
138
      com_err("uu-client", retval, "getting credentials cache");
 
139
      return 6;
 
140
  }
 
141
 
 
142
  memset ((char*)&creds, 0, sizeof(creds));
 
143
 
 
144
  retval = krb5_cc_get_principal(context, cc, &creds.client);
 
145
  if (retval) {
 
146
      com_err("uu-client", retval, "getting principal name");
 
147
      return 6;
 
148
  }
 
149
  
 
150
  retval = krb5_unparse_name(context, creds.client, &princ);
 
151
  if (retval) {
 
152
      com_err("uu-client", retval, "printing principal name");
 
153
      return 7;
 
154
  } 
 
155
  else
 
156
      fprintf(stderr, "uu-client: client principal is \"%s\".\n", princ);
 
157
 
 
158
  retval = krb5_get_host_realm(context, hname, &srealms);
 
159
  if (retval) {
 
160
      com_err("uu-client", retval, "getting realms for \"%s\"", hname);
 
161
      return 7;
 
162
  }
 
163
 
 
164
  retval = 
 
165
      krb5_build_principal_ext(context, &creds.server,
 
166
                               krb5_princ_realm(context, creds.client)->length,
 
167
                               krb5_princ_realm(context, creds.client)->data,
 
168
                               6, "krbtgt",
 
169
                               krb5_princ_realm(context, creds.client)->length,
 
170
                               krb5_princ_realm(context, creds.client)->data,
 
171
                               0);
 
172
  if (retval) {
 
173
      com_err("uu-client", retval, "setting up tgt server name");
 
174
      return 7;
 
175
  }
 
176
  
 
177
  /* Get TGT from credentials cache */
 
178
  retval = krb5_get_credentials(context, KRB5_GC_CACHED, cc, 
 
179
                                &creds, &new_creds);
 
180
  if (retval) {
 
181
      com_err("uu-client", retval, "getting TGT");
 
182
      return 6;
 
183
  }
 
184
 
 
185
  i = strlen(princ) + 1;
 
186
 
 
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);
 
192
  if (retval) {
 
193
      com_err("uu-client", retval, "sending principal name to server");
 
194
      return 8;
 
195
  }
 
196
  
 
197
  free(princ);
 
198
  
 
199
  retval = krb5_write_message(context, (krb5_pointer) &s, &new_creds->ticket);
 
200
  if (retval) {
 
201
      com_err("uu-client", retval, "sending ticket to server");
 
202
      return 8;
 
203
  }
 
204
 
 
205
  retval = krb5_read_message(context, (krb5_pointer) &s, &reply);
 
206
  if (retval) {
 
207
      com_err("uu-client", retval, "reading reply from server");
 
208
      return 9;
 
209
  }
 
210
 
 
211
  retval = krb5_auth_con_init(context, &auth_context);
 
212
  if (retval) {
 
213
      com_err("uu-client", retval, "initializing the auth_context");
 
214
      return 9;
 
215
  }
 
216
  
 
217
  retval = 
 
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);
 
221
  if (retval) {
 
222
      com_err("uu-client", retval, "generating addrs for auth_context");
 
223
        return 9;
 
224
    }
 
225
 
 
226
  retval = krb5_auth_con_setflags(context, auth_context,
 
227
                                  KRB5_AUTH_CONTEXT_DO_SEQUENCE);
 
228
  if (retval) {
 
229
      com_err("uu-client", retval, "initializing the auth_context flags");
 
230
      return 9;
 
231
  }
 
232
  
 
233
  retval = krb5_auth_con_setuseruserkey(context, auth_context, 
 
234
                                        &new_creds->keyblock);
 
235
  if (retval) {
 
236
      com_err("uu-client", retval, "setting useruserkey for authcontext");
 
237
      return 9;
 
238
  }
 
239
  
 
240
#if 1
 
241
  /* read the ap_req to get the session key */
 
242
  retval = krb5_rd_req(context, &auth_context, &reply,
 
243
                       NULL, NULL, NULL, &ticket);
 
244
  free(reply.data);
 
245
#else
 
246
  retval = krb5_recvauth(context, &auth_context, (krb5_pointer)&s, "???",
 
247
                         0, /* server */, 0, NULL, &ticket);
 
248
#endif
 
249
  
 
250
  if (retval) {
 
251
      com_err("uu-client", retval, "reading AP_REQ from server");
 
252
      return 9;
 
253
  }
 
254
 
 
255
  retval = krb5_unparse_name(context, ticket->enc_part2->client, &princ);
 
256
  if (retval)
 
257
      com_err("uu-client", retval, "while unparsing client name");
 
258
  else {
 
259
      printf("server is named \"%s\"\n", princ);
 
260
      free(princ);
 
261
  }
 
262
 
 
263
  retval = krb5_read_message(context, (krb5_pointer) &s, &reply);
 
264
  if (retval) {
 
265
      com_err("uu-client", retval, "reading reply from server");
 
266
      return 9;
 
267
  }
 
268
  
 
269
  retval = krb5_rd_safe(context, auth_context, &reply, &msg, NULL);
 
270
  if (retval) {
 
271
      com_err("uu-client", retval, "decoding reply from server");
 
272
      return 10;
 
273
  }
 
274
 
 
275
  printf ("uu-client: server says \"%s\".\n", msg.data);
 
276
  return 0;
 
277
}
 
278