1 /* client.c sample kerberos authenticated client
2 * Copyright (C) 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define SERVICE "sample"
30 doit (Shishi
* h
, int verbose
)
35 /* XXX Unfinished application-level security */
36 res
= shishi_safe (handle
, &safe
);
39 printf ("Could not build SAFE: %s\n", shishi_strerror (res
));
43 res
= shishi_safe_set_user_data (handle
, shishi_safe_safe (safe
),
47 printf ("Could not set application data in SAFE: %s\n",
48 shishi_strerror (res
));
52 res
= shishi_safe_build (safe
, key
);
55 printf ("Could not build SAFE: %s\n", shishi_strerror (res
));
59 res
= shishi_safe_print (handle
, stdout
, shishi_safe_safe(safe
));
62 printf ("Could not print SAFE: %s\n", shishi_strerror (res
));
67 printf("Application exchange start. Press ^D to finish.\n");
69 while (fgets (line
, sizeof(line
), stdin
))
71 printf("read: %s", line
);
76 printf ("error reading stdin\n");
84 auth (Shishi
* h
, int verbose
, const char *cname
, const char *sname
)
88 Shishi_tkts_hint hint
;
93 printf ("Client: %s\n", cname
);
94 printf ("Server: %s\n", sname
);
96 /* Get a ticket for the server. */
98 memset (&hint
, 0, sizeof(hint
));
99 hint
.client
= (char*) cname
;
100 hint
.server
= (char*) sname
;
101 tkt
= shishi_tkts_get (shishi_tkts_default (h
), &hint
);
104 printf ("cannot find ticket for \"%s\"\n", sname
);
109 shishi_tkt_pretty_print (tkt
, stderr
);
111 /* Create Authentication context */
113 rc
= shishi_ap_tktoptions (h
, &ap
, tkt
, SHISHI_APOPTIONS_MUTUAL_REQUIRED
);
116 printf ("cannot create authentication context\n");
120 /* Build Authentication request */
122 rc
= shishi_ap_req_build (ap
);
125 printf ("cannot build authentication request: %s\n",
126 shishi_strerror (rc
));
131 shishi_authenticator_print (h
, stderr
, shishi_ap_authenticator (ap
));
133 /* Authentication ourself to server */
135 shishi_apreq_print (h
, stdout
, shishi_ap_req (ap
));
136 /* Note: to get the binary blob to send, use:
138 * char *out; int outlen;
140 * rc = shishi_ap_req_der_new (ap, &out, &outlen);
142 * write(fd, out, outlen);
145 /* For mutual authentication, wait for server reply. */
147 if (shishi_apreq_mutual_required_p (h
, shishi_ap_req (ap
)))
151 printf ("Waiting for server to authenticate itself...\n");
153 rc
= shishi_aprep_parse (h
, stdin
, &aprep
);
156 printf ("Cannot parse AP-REP from server: %s\n",
157 shishi_strerror (rc
));
161 rc
= shishi_ap_rep_verify_asn1 (ap
, aprep
);
163 printf ("AP-REP verification OK...\n");
166 if (rc
== SHISHI_APREP_VERIFY_FAILED
)
167 printf ("AP-REP verification failed...\n");
169 printf ("AP-REP verification error: %s\n", shishi_strerror (rc
));
173 /* The server is authenticated. */
174 printf ("Server authenticated.\n");
177 /* We are now authenticated. */
178 printf ("User authenticated.\n");
180 return doit (h
, verbose
);
184 main (int argc
, char *argv
[])
190 printf ("sample-client (shishi " SHISHI_VERSION
")\n");
192 if (!shishi_check_version (SHISHI_VERSION
))
194 printf ("shishi_check_version() failed:\n"
195 "Header file incompatible with shared library.\n");
199 rc
= shishi_init (&h
);
202 printf ("error initializing shishi: %s\n", shishi_strerror (rc
));
209 sname
= shishi_server_for_local_service (h
, SERVICE
);
211 auth (h
, 1, shishi_principal_default (h
), sname
);