xref: /freebsd/crypto/heimdal/kuser/generate-requests.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * 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 
365e9cd1aeSAssar Westerlund static unsigned
read_words(const char * filename,char *** ret_w)375e9cd1aeSAssar Westerlund read_words (const char *filename, char ***ret_w)
385e9cd1aeSAssar Westerlund {
395e9cd1aeSAssar Westerlund     unsigned n, alloc;
405e9cd1aeSAssar Westerlund     FILE *f;
415e9cd1aeSAssar Westerlund     char buf[256];
425e9cd1aeSAssar Westerlund     char **w = NULL;
435e9cd1aeSAssar Westerlund 
445e9cd1aeSAssar Westerlund     f = fopen (filename, "r");
455e9cd1aeSAssar Westerlund     if (f == NULL)
465e9cd1aeSAssar Westerlund 	err (1, "cannot open %s", filename);
475e9cd1aeSAssar Westerlund     alloc = n = 0;
485e9cd1aeSAssar Westerlund     while (fgets (buf, sizeof(buf), f) != NULL) {
49c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
505e9cd1aeSAssar Westerlund 	if (n >= alloc) {
515e9cd1aeSAssar Westerlund 	    alloc += 16;
525e9cd1aeSAssar Westerlund 	    w = erealloc (w, alloc * sizeof(char **));
535e9cd1aeSAssar Westerlund 	}
545e9cd1aeSAssar Westerlund 	w[n++] = estrdup (buf);
555e9cd1aeSAssar Westerlund     }
565e9cd1aeSAssar Westerlund     *ret_w = w;
57c19800e8SDoug Rabson     if (n == 0)
58c19800e8SDoug Rabson 	errx(1, "%s is an empty file, no words to try", filename);
59*ae771770SStanislav Sedov     fclose(f);
605e9cd1aeSAssar Westerlund     return n;
615e9cd1aeSAssar Westerlund }
625e9cd1aeSAssar Westerlund 
635e9cd1aeSAssar Westerlund static void
generate_requests(const char * filename,unsigned nreq)645e9cd1aeSAssar Westerlund generate_requests (const char *filename, unsigned nreq)
655e9cd1aeSAssar Westerlund {
66*ae771770SStanislav Sedov     krb5_principal client;
675e9cd1aeSAssar Westerlund     krb5_context context;
685e9cd1aeSAssar Westerlund     krb5_error_code ret;
695e9cd1aeSAssar Westerlund     krb5_creds cred;
705e9cd1aeSAssar Westerlund     int i;
715e9cd1aeSAssar Westerlund     char **words;
725e9cd1aeSAssar Westerlund     unsigned nwords;
735e9cd1aeSAssar Westerlund 
745e9cd1aeSAssar Westerlund     ret = krb5_init_context (&context);
755e9cd1aeSAssar Westerlund     if (ret)
765e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
775e9cd1aeSAssar Westerlund 
785e9cd1aeSAssar Westerlund     nwords = read_words (filename, &words);
795e9cd1aeSAssar Westerlund 
805e9cd1aeSAssar Westerlund     for (i = 0; i < nreq; ++i) {
815e9cd1aeSAssar Westerlund 	char *name = words[rand() % nwords];
825e9cd1aeSAssar Westerlund 
835e9cd1aeSAssar Westerlund 	memset(&cred, 0, sizeof(cred));
845e9cd1aeSAssar Westerlund 
85*ae771770SStanislav Sedov 	ret = krb5_parse_name (context, name, &client);
865e9cd1aeSAssar Westerlund 	if (ret)
875e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
885e9cd1aeSAssar Westerlund 
89*ae771770SStanislav Sedov 	ret = krb5_get_init_creds_password (context, &cred, client, "",
90*ae771770SStanislav Sedov 					    NULL, NULL, 0, NULL, NULL);
915e9cd1aeSAssar Westerlund 	if (ret)
92c19800e8SDoug Rabson 	    krb5_free_cred_contents (context, &cred);
93*ae771770SStanislav Sedov 	krb5_free_principal(context, client);
945e9cd1aeSAssar Westerlund     }
955e9cd1aeSAssar Westerlund }
965e9cd1aeSAssar Westerlund 
975e9cd1aeSAssar Westerlund static int version_flag	= 0;
985e9cd1aeSAssar Westerlund static int help_flag	= 0;
995e9cd1aeSAssar Westerlund 
1005e9cd1aeSAssar Westerlund static struct getargs args[] = {
1015e9cd1aeSAssar Westerlund     { "version", 	0,   arg_flag, &version_flag },
1025e9cd1aeSAssar Westerlund     { "help",		0,   arg_flag, &help_flag }
1035e9cd1aeSAssar Westerlund };
1045e9cd1aeSAssar Westerlund 
1055e9cd1aeSAssar Westerlund static void
usage(int ret)1065e9cd1aeSAssar Westerlund usage (int ret)
1075e9cd1aeSAssar Westerlund {
1085e9cd1aeSAssar Westerlund     arg_printusage (args,
1095e9cd1aeSAssar Westerlund 		    sizeof(args)/sizeof(*args),
1105e9cd1aeSAssar Westerlund 		    NULL,
1115e9cd1aeSAssar Westerlund 		    "file number");
1125e9cd1aeSAssar Westerlund     exit (ret);
1135e9cd1aeSAssar Westerlund }
1145e9cd1aeSAssar Westerlund 
1155e9cd1aeSAssar Westerlund int
main(int argc,char ** argv)1165e9cd1aeSAssar Westerlund main(int argc, char **argv)
1175e9cd1aeSAssar Westerlund {
118c19800e8SDoug Rabson     int optidx = 0;
1195e9cd1aeSAssar Westerlund     int nreq;
1205e9cd1aeSAssar Westerlund     char *end;
1215e9cd1aeSAssar Westerlund 
122adb0ddaeSAssar Westerlund     setprogname(argv[0]);
123c19800e8SDoug Rabson     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
1245e9cd1aeSAssar Westerlund 	usage(1);
1254137ff4cSJacques Vidrine 
1264137ff4cSJacques Vidrine     if (help_flag)
1274137ff4cSJacques Vidrine 	usage (0);
1284137ff4cSJacques Vidrine 
1294137ff4cSJacques Vidrine     if(version_flag) {
1304137ff4cSJacques Vidrine 	print_version(NULL);
1314137ff4cSJacques Vidrine 	exit(0);
1324137ff4cSJacques Vidrine     }
1334137ff4cSJacques Vidrine 
134c19800e8SDoug Rabson     argc -= optidx;
135c19800e8SDoug Rabson     argv += optidx;
1365e9cd1aeSAssar Westerlund 
1375e9cd1aeSAssar Westerlund     if (argc != 2)
1385e9cd1aeSAssar Westerlund 	usage (1);
1395e9cd1aeSAssar Westerlund     srand (0);
1405e9cd1aeSAssar Westerlund     nreq = strtol (argv[1], &end, 0);
1415e9cd1aeSAssar Westerlund     if (argv[1] == end || *end != '\0')
1425e9cd1aeSAssar Westerlund 	usage (1);
1435e9cd1aeSAssar Westerlund     generate_requests (argv[0], nreq);
1445e9cd1aeSAssar Westerlund     return 0;
1455e9cd1aeSAssar Westerlund }
146