summaryrefslogtreecommitdiffstats
path: root/crypto/kerberosIV/appl/sample/simple_client.c
blob: 87697252096f8a19a515af6e9f219515efc9b2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *
 * Copyright 1989 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Simple UDP-based sample client program.  For demonstration.
 * This program performs no useful function.
 */

#include "sample.h"
RCSID("$Id: simple_client.c,v 1.13 1998/06/09 19:24:39 joda Exp $");

#define MSG "hi, Jennifer!"		/* message text */

static int
talkto(char *hostname, char *service, int port)
{
  int flags = 0;			/* flags for sendto() */
  long len;
  u_long cksum = 0L;		/* cksum not used */
  char c_realm[REALM_SZ];		/* local Kerberos realm */
  char *s_realm;			/* server's Kerberos realm */

  KTEXT_ST k;			/* Kerberos data */
  KTEXT ktxt = &k;

  int sock, i;
  struct hostent *host;
  struct sockaddr_in s_sock;	/* server address */
  char myhostname[MaxHostNameLen]; /* local hostname */

  /* for krb_mk_safe/priv */
  struct sockaddr_in c_sock;	/* client address */
  CREDENTIALS c;			/* ticket & session key */
  CREDENTIALS *cred = &c;

  /* for krb_mk_priv */
  des_key_schedule sched;		/* session key schedule */

  /* Look up server host */
  if ((host = gethostbyname(hostname)) == NULL) {
    fprintf(stderr, "%s: unknown host \n", hostname);
    return 1;
  }

  /* Set server's address */
  memset(&s_sock, 0, sizeof(s_sock));
  memcpy(&s_sock.sin_addr, host->h_addr, sizeof(s_sock.sin_addr));
  s_sock.sin_family = AF_INET;
  if (port)
    s_sock.sin_port = port;
  else
    s_sock.sin_port = k_getportbyname (service, "tcp", htons(SAMPLE_PORT));

  if (gethostname(myhostname, sizeof(myhostname)) < 0) {
    warn("gethostname");
    return 1;
  }

  if ((host = gethostbyname(myhostname)) == NULL) {
    fprintf(stderr, "%s: unknown host\n", myhostname);
    return 1;
  }

  /* Open a socket */
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    warn("socket SOCK_DGRAM");
    return 1;
  }

  memset(&c_sock, 0, sizeof(c_sock));
  memcpy(&c_sock.sin_addr, host->h_addr, sizeof(c_sock.sin_addr));
  c_sock.sin_family = AF_INET;

  /* Bind it to set the address; kernel will fill in port # */
  if (bind(sock, (struct sockaddr *)&c_sock, sizeof(c_sock)) < 0) {
    warn("bind");
    return 1;
  }
	
  /* Get local realm, not needed, just an example */
  if ((i = krb_get_lrealm(c_realm, 1)) != KSUCCESS) {
    fprintf(stderr, "can't find local Kerberos realm\n");
    return 1;
  }
  printf("Local Kerberos realm is %s\n", c_realm);

  /* Get Kerberos realm of host */
  s_realm = krb_realmofhost(hostname);

  /* PREPARE KRB_MK_REQ MESSAGE */

  /* Get credentials for server, create krb_mk_req message */
  if ((i = krb_mk_req(ktxt, service, hostname, s_realm, cksum))
      != KSUCCESS) {
    fprintf(stderr, "%s\n", krb_get_err_text(i));
    return 1;
  }
  printf("Got credentials for %s.\n", service);

  /* Send authentication info to server */
  i = sendto(sock, (char *)ktxt->dat, ktxt->length, flags,
	     (struct sockaddr *)&s_sock, sizeof(s_sock));
  if (i < 0)
    warn("sending datagram message");
  printf("Sent authentication data: %d bytes\n", i);

  /* PREPARE KRB_MK_SAFE MESSAGE */

  /* Get my address */
  memset(&c_sock, 0, sizeof(c_sock));
  i = sizeof(c_sock);
  if (getsockname(sock, (struct sockaddr *)&c_sock, &i) < 0) {
    warn("getsockname");
    return 1;
  }

  /* Get session key */
  i = krb_get_cred(service, hostname, s_realm, cred);
  if (i != KSUCCESS)
    return 1;

  /* Make the safe message */
  len = krb_mk_safe(MSG, ktxt->dat, strlen(MSG)+1,
		    &cred->session, &c_sock, &s_sock);

  /* Send it */
  i = sendto(sock, (char *)ktxt->dat, (int) len, flags,
	     (struct sockaddr *)&s_sock, sizeof(s_sock));
  if (i < 0)
    warn("sending safe message");
  printf("Sent checksummed message: %d bytes\n", i);

  /* PREPARE KRB_MK_PRIV MESSAGE */

#ifdef NOENCRYPTION
  memset(sched, 0, sizeof(sched));
#else
  /* Get key schedule for session key */
  des_key_sched(&cred->session, sched);
#endif

  /* Make the encrypted message */
  len = krb_mk_priv(MSG, ktxt->dat, strlen(MSG)+1,
		    sched, &cred->session, &c_sock, &s_sock);

  /* Send it */
  i = sendto(sock, (char *)ktxt->dat, (int) len, flags,
	     (struct sockaddr *)&s_sock, sizeof(s_sock));
  if (i < 0)
    warn("sending encrypted message");
  printf("Sent encrypted message: %d bytes\n", i);
  return 0;
}

static void
usage (void)
{
  fprintf (stderr, "Usage: %s [-s service] [-p port] hostname\n",
	   __progname);
  exit (1);
}

int
main(int argc, char **argv)
{
  int ret = 0;
  int port = 0;
  char service[SNAME_SZ];
  struct servent *serv;
  int c;

  set_progname (argv[0]);

  strcpy_truncate (service, SAMPLE_SERVICE, sizeof(service));

  while ((c = getopt(argc, argv, "s:p:")) != EOF)
    switch(c) {
    case 's' :
      strcpy_truncate (service, optarg, sizeof(service));
      break;
    case 'p' :
      serv = getservbyname (optarg, "tcp");
      if (serv)
	port = serv->s_port;
      else
	port = htons(atoi(optarg));
      break;
    case '?' :
    default :
      usage();
    }

  argc -= optind;
  argv += optind;

  while (argc-- > 0)
    ret &= talkto (*argv++, service, port);
  return ret;
}
OpenPOWER on IntegriCloud