1 /* server.c sample kerberos authenticated server
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 const char *program_name
= "client";
33 doit (Shishi
* h
, int verbose
)
38 res
= shishi_encticketpart_get_key
39 (handle
, shishi_tkt_encticketpart (shishi_ap_tkt (ap
)), &tktkey
);
42 fprintf (stderr
, _("Could not extract key:\n%s\n%s\n"),
43 shishi_strerror (res
), shishi_strerror_details (handle
));
47 res
= shishi_safe_parse (handle
, stdin
, &asn1safe
);
50 fprintf (stderr
, _("Could not read SAFE:\n%s\n%s\n"),
51 shishi_strerror (res
), shishi_strerror_details (handle
));
55 res
= shishi_safe (handle
, &safe
);
58 fprintf (stderr
, _("Could not create SAFE:\n%s\n%s\n"),
59 shishi_strerror (res
), shishi_strerror_details (handle
));
63 shishi_safe_safe_set (safe
, asn1safe
);
65 res
= shishi_safe_verify (safe
, tktkey
);
68 fprintf (stderr
, _("Could not verify SAFE:\n%s\n%s\n"),
69 shishi_strerror (res
), shishi_strerror_details (handle
));
73 printf ("Verified SAFE successfully...\n");
75 userdatalen
= sizeof(userdata
);
76 res
= shishi_safe_user_data (handle
, asn1safe
, userdata
, &userdatalen
);
79 fprintf (stderr
, _("Could not extract user data:\n%s\n%s\n"),
80 shishi_strerror (res
), shishi_strerror_details (handle
));
83 userdata
[userdatalen
] = '\0';
84 printf("user data: `%s'\n", userdata
);
87 printf("Application exchange start. Press ^D to finish.\n");
89 while (fgets (line
, sizeof(line
), stdin
))
91 printf("read: %s", line
);
96 printf ("error reading stdin\n");
104 auth (Shishi
* h
, int verbose
, const char *cname
, const char *sname
)
113 printf ("Client: %s\n", cname
);
114 printf ("Server: %s\n", sname
);
116 /* Get key for the server. */
118 key
= shishi_hostkeys_for_server (h
, sname
);
121 printf ("could not find key: %s\n", shishi_strerror_details (h
));
126 shishi_key_print (h
, stderr
, key
);
128 /* Read Authentication request from client */
130 rc
= shishi_apreq_parse (h
, stdin
, &apreq
);
133 printf ("could not read AP-REQ: %s\n", shishi_strerror (rc
));
137 /* Create Authentication context */
139 rc
= shishi_ap (h
, &ap
);
142 printf ("Could not create AP: %s\n", shishi_strerror (rc
));
146 /* Store request in context */
148 shishi_ap_req_set (ap
, apreq
);
150 /* Process authentication request */
152 rc
= shishi_ap_req_process (ap
, key
);
155 printf ("Could not process AP-REQ: %s\n", shishi_strerror (rc
));
160 shishi_authenticator_print (h
, stderr
, shishi_ap_authenticator (ap
));
162 buflen
= sizeof (buf
);
163 rc
= shishi_authenticator_cnamerealm_get (h
, shishi_ap_authenticator (ap
),
166 printf ("Client name (from authenticator): %s\n", buf
);
168 buflen
= sizeof (buf
);
169 rc
= shishi_encticketpart_cnamerealm_get
170 (h
, shishi_tkt_encticketpart (shishi_ap_tkt (ap
)),
173 printf ("Client name (from encticketpart): %s\n", buf
);
175 buflen
= sizeof (buf
);
176 rc
= shishi_ticket_snamerealm_get (h
, shishi_tkt_ticket (shishi_ap_tkt (ap
)),
179 printf ("Server name (from ticket): %s\n", buf
);
181 /* User is authenticated. */
183 printf ("User authenticated.\n");
185 /* Authenticate ourself to client, if request */
187 if (shishi_apreq_mutual_required_p (h
, apreq
))
191 printf ("Mutual authentication required.\n");
193 rc
= shishi_ap_rep_asn1 (ap
, &aprep
);
196 printf ("Error creating AP-REP: %s\n", shishi_strerror (rc
));
201 shishi_encapreppart_print (h
, stderr
, shishi_ap_encapreppart (ap
));
203 shishi_aprep_print (h
, stdout
, aprep
);
205 /* We are authenticated to client */
208 return doit (h
, verbose
);
212 main (int argc
, char *argv
[])
218 printf ("sample-server (shishi " SHISHI_VERSION
")\n");
220 if (!shishi_check_version (SHISHI_VERSION
))
222 printf ("shishi_check_version() failed:\n"
223 "Header file incompatible with shared library.\n");
227 rc
= shishi_init_server (&h
);
230 printf ("error initializing shishi: %s\n", shishi_strerror (rc
));
237 sname
= shishi_server_for_local_service (h
, SERVICE
);
239 auth (h
, 1, shishi_principal_default (h
), sname
);