xref: /freebsd/crypto/heimdal/kuser/generate-requests.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
15e9cd1aeSAssar Westerlund /*
2c19800e8SDoug Rabson  * Copyright (c) 2000 - 2004 Kungliga Tekniska H�gskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund #include "kuser_locl.h"
355e9cd1aeSAssar Westerlund 
36c19800e8SDoug Rabson RCSID("$Id: generate-requests.c 19233 2006-12-06 08:04:05Z lha $");
375e9cd1aeSAssar Westerlund 
385e9cd1aeSAssar Westerlund static krb5_error_code
395e9cd1aeSAssar Westerlund null_key_proc (krb5_context context,
405e9cd1aeSAssar Westerlund 	       krb5_enctype type,
415e9cd1aeSAssar Westerlund 	       krb5_salt salt,
425e9cd1aeSAssar Westerlund 	       krb5_const_pointer keyseed,
435e9cd1aeSAssar Westerlund 	       krb5_keyblock **key)
445e9cd1aeSAssar Westerlund {
455e9cd1aeSAssar Westerlund     return ENOTTY;
465e9cd1aeSAssar Westerlund }
475e9cd1aeSAssar Westerlund 
485e9cd1aeSAssar Westerlund static unsigned
495e9cd1aeSAssar Westerlund read_words (const char *filename, char ***ret_w)
505e9cd1aeSAssar Westerlund {
515e9cd1aeSAssar Westerlund     unsigned n, alloc;
525e9cd1aeSAssar Westerlund     FILE *f;
535e9cd1aeSAssar Westerlund     char buf[256];
545e9cd1aeSAssar Westerlund     char **w = NULL;
555e9cd1aeSAssar Westerlund 
565e9cd1aeSAssar Westerlund     f = fopen (filename, "r");
575e9cd1aeSAssar Westerlund     if (f == NULL)
585e9cd1aeSAssar Westerlund 	err (1, "cannot open %s", filename);
595e9cd1aeSAssar Westerlund     alloc = n = 0;
605e9cd1aeSAssar Westerlund     while (fgets (buf, sizeof(buf), f) != NULL) {
61c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
625e9cd1aeSAssar Westerlund 	if (n >= alloc) {
635e9cd1aeSAssar Westerlund 	    alloc += 16;
645e9cd1aeSAssar Westerlund 	    w = erealloc (w, alloc * sizeof(char **));
655e9cd1aeSAssar Westerlund 	}
665e9cd1aeSAssar Westerlund 	w[n++] = estrdup (buf);
675e9cd1aeSAssar Westerlund     }
685e9cd1aeSAssar Westerlund     *ret_w = w;
69c19800e8SDoug Rabson     if (n == 0)
70c19800e8SDoug Rabson 	errx(1, "%s is an empty file, no words to try", filename);
715e9cd1aeSAssar Westerlund     return n;
725e9cd1aeSAssar Westerlund }
735e9cd1aeSAssar Westerlund 
745e9cd1aeSAssar Westerlund static void
755e9cd1aeSAssar Westerlund generate_requests (const char *filename, unsigned nreq)
765e9cd1aeSAssar Westerlund {
775e9cd1aeSAssar Westerlund     krb5_context context;
785e9cd1aeSAssar Westerlund     krb5_error_code ret;
795e9cd1aeSAssar Westerlund     krb5_creds cred;
805e9cd1aeSAssar Westerlund     int i;
815e9cd1aeSAssar Westerlund     char **words;
825e9cd1aeSAssar Westerlund     unsigned nwords;
835e9cd1aeSAssar Westerlund 
845e9cd1aeSAssar Westerlund     ret = krb5_init_context (&context);
855e9cd1aeSAssar Westerlund     if (ret)
865e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
875e9cd1aeSAssar Westerlund 
885e9cd1aeSAssar Westerlund     nwords = read_words (filename, &words);
895e9cd1aeSAssar Westerlund 
905e9cd1aeSAssar Westerlund     for (i = 0; i < nreq; ++i) {
915e9cd1aeSAssar Westerlund 	char *name = words[rand() % nwords];
925e9cd1aeSAssar Westerlund 	krb5_realm *client_realm;
935e9cd1aeSAssar Westerlund 
945e9cd1aeSAssar Westerlund 	memset(&cred, 0, sizeof(cred));
955e9cd1aeSAssar Westerlund 
965e9cd1aeSAssar Westerlund 	ret = krb5_parse_name (context, name, &cred.client);
975e9cd1aeSAssar Westerlund 	if (ret)
985e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
995e9cd1aeSAssar Westerlund 	client_realm = krb5_princ_realm (context, cred.client);
1005e9cd1aeSAssar Westerlund 
1015e9cd1aeSAssar Westerlund 	ret = krb5_make_principal(context, &cred.server, *client_realm,
1025e9cd1aeSAssar Westerlund 				  KRB5_TGS_NAME, *client_realm, NULL);
1035e9cd1aeSAssar Westerlund 	if (ret)
1045e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_make_principal");
1055e9cd1aeSAssar Westerlund 
1065e9cd1aeSAssar Westerlund 	ret = krb5_get_in_cred (context, 0, NULL, NULL, NULL, NULL,
1075e9cd1aeSAssar Westerlund 				null_key_proc, NULL, NULL, NULL,
1085e9cd1aeSAssar Westerlund 				&cred, NULL);
109c19800e8SDoug Rabson 	krb5_free_cred_contents (context, &cred);
1105e9cd1aeSAssar Westerlund     }
1115e9cd1aeSAssar Westerlund }
1125e9cd1aeSAssar Westerlund 
1135e9cd1aeSAssar Westerlund static int version_flag	= 0;
1145e9cd1aeSAssar Westerlund static int help_flag	= 0;
1155e9cd1aeSAssar Westerlund 
1165e9cd1aeSAssar Westerlund static struct getargs args[] = {
1175e9cd1aeSAssar Westerlund     { "version", 	0,   arg_flag, &version_flag },
1185e9cd1aeSAssar Westerlund     { "help",		0,   arg_flag, &help_flag }
1195e9cd1aeSAssar Westerlund };
1205e9cd1aeSAssar Westerlund 
1215e9cd1aeSAssar Westerlund static void
1225e9cd1aeSAssar Westerlund usage (int ret)
1235e9cd1aeSAssar Westerlund {
1245e9cd1aeSAssar Westerlund     arg_printusage (args,
1255e9cd1aeSAssar Westerlund 		    sizeof(args)/sizeof(*args),
1265e9cd1aeSAssar Westerlund 		    NULL,
1275e9cd1aeSAssar Westerlund 		    "file number");
1285e9cd1aeSAssar Westerlund     exit (ret);
1295e9cd1aeSAssar Westerlund }
1305e9cd1aeSAssar Westerlund 
1315e9cd1aeSAssar Westerlund int
1325e9cd1aeSAssar Westerlund main(int argc, char **argv)
1335e9cd1aeSAssar Westerlund {
134c19800e8SDoug Rabson     int optidx = 0;
1355e9cd1aeSAssar Westerlund     int nreq;
1365e9cd1aeSAssar Westerlund     char *end;
1375e9cd1aeSAssar Westerlund 
138adb0ddaeSAssar Westerlund     setprogname(argv[0]);
139c19800e8SDoug Rabson     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
1405e9cd1aeSAssar Westerlund 	usage(1);
1414137ff4cSJacques Vidrine 
1424137ff4cSJacques Vidrine     if (help_flag)
1434137ff4cSJacques Vidrine 	usage (0);
1444137ff4cSJacques Vidrine 
1454137ff4cSJacques Vidrine     if(version_flag) {
1464137ff4cSJacques Vidrine 	print_version(NULL);
1474137ff4cSJacques Vidrine 	exit(0);
1484137ff4cSJacques Vidrine     }
1494137ff4cSJacques Vidrine 
150c19800e8SDoug Rabson     argc -= optidx;
151c19800e8SDoug Rabson     argv += optidx;
1525e9cd1aeSAssar Westerlund 
1535e9cd1aeSAssar Westerlund     if (argc != 2)
1545e9cd1aeSAssar Westerlund 	usage (1);
1555e9cd1aeSAssar Westerlund     srand (0);
1565e9cd1aeSAssar Westerlund     nreq = strtol (argv[1], &end, 0);
1575e9cd1aeSAssar Westerlund     if (argv[1] == end || *end != '\0')
1585e9cd1aeSAssar Westerlund 	usage (1);
1595e9cd1aeSAssar Westerlund     generate_requests (argv[0], nreq);
1605e9cd1aeSAssar Westerlund     return 0;
1615e9cd1aeSAssar Westerlund }
162