172445Sassar/*
2233294Sstas * Copyright (c) 2000 - 2004 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
572445Sassar *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
972445Sassar *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1272445Sassar *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1672445Sassar *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2072445Sassar *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3272445Sassar */
3372445Sassar
3472445Sassar#include "kuser_locl.h"
3572445Sassar
3672445Sassarstatic unsigned
3772445Sassarread_words (const char *filename, char ***ret_w)
3872445Sassar{
3972445Sassar    unsigned n, alloc;
4072445Sassar    FILE *f;
4172445Sassar    char buf[256];
4272445Sassar    char **w = NULL;
4372445Sassar
4472445Sassar    f = fopen (filename, "r");
4572445Sassar    if (f == NULL)
4672445Sassar	err (1, "cannot open %s", filename);
4772445Sassar    alloc = n = 0;
4872445Sassar    while (fgets (buf, sizeof(buf), f) != NULL) {
49178825Sdfr	buf[strcspn(buf, "\r\n")] = '\0';
5072445Sassar	if (n >= alloc) {
5172445Sassar	    alloc += 16;
5272445Sassar	    w = erealloc (w, alloc * sizeof(char **));
5372445Sassar	}
5472445Sassar	w[n++] = estrdup (buf);
5572445Sassar    }
5672445Sassar    *ret_w = w;
57178825Sdfr    if (n == 0)
58178825Sdfr	errx(1, "%s is an empty file, no words to try", filename);
59233294Sstas    fclose(f);
6072445Sassar    return n;
6172445Sassar}
6272445Sassar
6372445Sassarstatic void
6472445Sassargenerate_requests (const char *filename, unsigned nreq)
6572445Sassar{
66233294Sstas    krb5_principal client;
6772445Sassar    krb5_context context;
6872445Sassar    krb5_error_code ret;
6972445Sassar    krb5_creds cred;
7072445Sassar    int i;
7172445Sassar    char **words;
7272445Sassar    unsigned nwords;
7372445Sassar
7472445Sassar    ret = krb5_init_context (&context);
7572445Sassar    if (ret)
7672445Sassar	errx (1, "krb5_init_context failed: %d", ret);
7772445Sassar
7872445Sassar    nwords = read_words (filename, &words);
7972445Sassar
8072445Sassar    for (i = 0; i < nreq; ++i) {
8172445Sassar	char *name = words[rand() % nwords];
8272445Sassar
8372445Sassar	memset(&cred, 0, sizeof(cred));
8472445Sassar
85233294Sstas	ret = krb5_parse_name (context, name, &client);
8672445Sassar	if (ret)
8772445Sassar	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
8872445Sassar
89233294Sstas	ret = krb5_get_init_creds_password (context, &cred, client, "",
90233294Sstas					    NULL, NULL, 0, NULL, NULL);
9172445Sassar	if (ret)
92233294Sstas	    krb5_free_cred_contents (context, &cred);
93233294Sstas	krb5_free_principal(context, client);
9472445Sassar    }
9572445Sassar}
9672445Sassar
9772445Sassarstatic int version_flag	= 0;
9872445Sassarstatic int help_flag	= 0;
9972445Sassar
10072445Sassarstatic struct getargs args[] = {
10172445Sassar    { "version", 	0,   arg_flag, &version_flag },
10272445Sassar    { "help",		0,   arg_flag, &help_flag }
10372445Sassar};
10472445Sassar
10572445Sassarstatic void
10672445Sassarusage (int ret)
10772445Sassar{
10872445Sassar    arg_printusage (args,
10972445Sassar		    sizeof(args)/sizeof(*args),
11072445Sassar		    NULL,
11172445Sassar		    "file number");
11272445Sassar    exit (ret);
11372445Sassar}
11472445Sassar
11572445Sassarint
11672445Sassarmain(int argc, char **argv)
11772445Sassar{
118178825Sdfr    int optidx = 0;
11972445Sassar    int nreq;
12072445Sassar    char *end;
12172445Sassar
12278527Sassar    setprogname(argv[0]);
123178825Sdfr    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
12472445Sassar	usage(1);
12590926Snectar
12690926Snectar    if (help_flag)
12790926Snectar	usage (0);
12890926Snectar
12990926Snectar    if(version_flag) {
13090926Snectar	print_version(NULL);
13190926Snectar	exit(0);
13290926Snectar    }
13390926Snectar
134178825Sdfr    argc -= optidx;
135178825Sdfr    argv += optidx;
13672445Sassar
13772445Sassar    if (argc != 2)
13872445Sassar	usage (1);
13972445Sassar    srand (0);
14072445Sassar    nreq = strtol (argv[1], &end, 0);
14172445Sassar    if (argv[1] == end || *end != '\0')
14272445Sassar	usage (1);
14372445Sassar    generate_requests (argv[0], nreq);
14472445Sassar    return 0;
14572445Sassar}
146