generate-requests.c revision 90926
176195Sbrian/*
276195Sbrian * Copyright (c) 2000 - 2001 Kungliga Tekniska H�gskolan
376195Sbrian * (Royal Institute of Technology, Stockholm, Sweden).
476195Sbrian * All rights reserved.
576195Sbrian *
676195Sbrian * Redistribution and use in source and binary forms, with or without
776195Sbrian * modification, are permitted provided that the following conditions
876195Sbrian * are met:
976195Sbrian *
1076195Sbrian * 1. Redistributions of source code must retain the above copyright
1176195Sbrian *    notice, this list of conditions and the following disclaimer.
1276195Sbrian *
1376195Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1476195Sbrian *    notice, this list of conditions and the following disclaimer in the
1576195Sbrian *    documentation and/or other materials provided with the distribution.
1676195Sbrian *
1776195Sbrian * 3. Neither the name of the Institute nor the names of its contributors
1876195Sbrian *    may be used to endorse or promote products derived from this software
1976195Sbrian *    without specific prior written permission.
2076195Sbrian *
2176195Sbrian * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2276195Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2376195Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2476195Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2576195Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2676195Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2776195Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2876195Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2976195Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3076195Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3176195Sbrian * SUCH DAMAGE.
3276195Sbrian */
3376195Sbrian
3476195Sbrian#include "kuser_locl.h"
3576195Sbrian
3676195SbrianRCSID("$Id: generate-requests.c,v 1.4 2001/08/24 01:07:22 assar Exp $");
3776195Sbrian
3876195Sbrianstatic krb5_error_code
3976195Sbriannull_key_proc (krb5_context context,
4076195Sbrian	       krb5_enctype type,
4176195Sbrian	       krb5_salt salt,
4276195Sbrian	       krb5_const_pointer keyseed,
4376195Sbrian	       krb5_keyblock **key)
4476195Sbrian{
4576195Sbrian    return ENOTTY;
4676195Sbrian}
4776195Sbrian
4876195Sbrianstatic unsigned
4976195Sbrianread_words (const char *filename, char ***ret_w)
5076195Sbrian{
5176195Sbrian    unsigned n, alloc;
5276195Sbrian    FILE *f;
5376195Sbrian    char buf[256];
5476195Sbrian    char **w = NULL;
5576195Sbrian
5676195Sbrian    f = fopen (filename, "r");
5776195Sbrian    if (f == NULL)
5876195Sbrian	err (1, "cannot open %s", filename);
5976195Sbrian    alloc = n = 0;
6076195Sbrian    while (fgets (buf, sizeof(buf), f) != NULL) {
6176195Sbrian	if (buf[strlen (buf) - 1] == '\n')
6276195Sbrian	    buf[strlen (buf) - 1] = '\0';
6376195Sbrian	if (n >= alloc) {
6476195Sbrian	    alloc += 16;
6576195Sbrian	    w = erealloc (w, alloc * sizeof(char **));
6676195Sbrian	}
6776195Sbrian	w[n++] = estrdup (buf);
6876195Sbrian    }
6976195Sbrian    *ret_w = w;
7076195Sbrian    return n;
7176195Sbrian}
7276195Sbrian
7376195Sbrianstatic void
7476195Sbriangenerate_requests (const char *filename, unsigned nreq)
7576195Sbrian{
7676195Sbrian    krb5_context context;
7776195Sbrian    krb5_error_code ret;
7876195Sbrian    krb5_creds cred;
7976195Sbrian    int i;
8076195Sbrian    char **words;
8176195Sbrian    unsigned nwords;
8276195Sbrian
8376195Sbrian    ret = krb5_init_context (&context);
8476195Sbrian    if (ret)
8576195Sbrian	errx (1, "krb5_init_context failed: %d", ret);
8676195Sbrian
8776195Sbrian    nwords = read_words (filename, &words);
8876195Sbrian
8976195Sbrian    for (i = 0; i < nreq; ++i) {
9076195Sbrian	char *name = words[rand() % nwords];
9176195Sbrian	krb5_realm *client_realm;
9276195Sbrian
9376195Sbrian	memset(&cred, 0, sizeof(cred));
9476195Sbrian
9576195Sbrian	ret = krb5_parse_name (context, name, &cred.client);
9676195Sbrian	if (ret)
9776195Sbrian	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
9876195Sbrian	client_realm = krb5_princ_realm (context, cred.client);
9976195Sbrian
10076195Sbrian	ret = krb5_make_principal(context, &cred.server, *client_realm,
10176195Sbrian				  KRB5_TGS_NAME, *client_realm, NULL);
10276195Sbrian	if (ret)
10376195Sbrian	    krb5_err (context, 1, ret, "krb5_make_principal");
10476195Sbrian
10576195Sbrian	ret = krb5_get_in_cred (context, 0, NULL, NULL, NULL, NULL,
10676195Sbrian				null_key_proc, NULL, NULL, NULL,
10776195Sbrian				&cred, NULL);
10876195Sbrian	krb5_free_creds_contents (context, &cred);
10976195Sbrian    }
11076195Sbrian}
11176195Sbrian
11276195Sbrianstatic int version_flag	= 0;
11376195Sbrianstatic int help_flag	= 0;
11476195Sbrian
11576195Sbrianstatic struct getargs args[] = {
11676195Sbrian    { "version", 	0,   arg_flag, &version_flag },
11776195Sbrian    { "help",		0,   arg_flag, &help_flag }
11876195Sbrian};
11976195Sbrian
12076195Sbrianstatic void
12176195Sbrianusage (int ret)
12276195Sbrian{
12376195Sbrian    arg_printusage (args,
12476195Sbrian		    sizeof(args)/sizeof(*args),
12576195Sbrian		    NULL,
12676195Sbrian		    "file number");
12776195Sbrian    exit (ret);
12876195Sbrian}
12976195Sbrian
13076195Sbrianint
13176195Sbrianmain(int argc, char **argv)
13276195Sbrian{
13376195Sbrian    int optind = 0;
13476195Sbrian    int nreq;
13576195Sbrian    char *end;
13676195Sbrian
13776195Sbrian    setprogname(argv[0]);
13876195Sbrian    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
13976195Sbrian	usage(1);
14076195Sbrian
14176195Sbrian    if (help_flag)
14276195Sbrian	usage (0);
14376195Sbrian
14476195Sbrian    if(version_flag) {
14576195Sbrian	print_version(NULL);
14676195Sbrian	exit(0);
14776195Sbrian    }
14876195Sbrian
14976195Sbrian    argc -= optind;
15076195Sbrian    argv += optind;
15176195Sbrian
15276195Sbrian    if (argc != 2)
15376195Sbrian	usage (1);
15476195Sbrian    srand (0);
15576195Sbrian    nreq = strtol (argv[1], &end, 0);
15676195Sbrian    if (argv[1] == end || *end != '\0')
15776195Sbrian	usage (1);
15876195Sbrian    generate_requests (argv[0], nreq);
15976195Sbrian    return 0;
16076195Sbrian}
16176195Sbrian