generate-requests.c revision 178825
172445Sassar/*
2178825Sdfr * Copyright (c) 2000 - 2004 Kungliga Tekniska H�gskolan
372445Sassar * (Royal Institute of Technology, Stockholm, Sweden).
472445Sassar * All rights reserved.
572445Sassar *
672445Sassar * Redistribution and use in source and binary forms, with or without
772445Sassar * modification, are permitted provided that the following conditions
872445Sassar * are met:
972445Sassar *
1072445Sassar * 1. Redistributions of source code must retain the above copyright
1172445Sassar *    notice, this list of conditions and the following disclaimer.
1272445Sassar *
1372445Sassar * 2. Redistributions in binary form must reproduce the above copyright
1472445Sassar *    notice, this list of conditions and the following disclaimer in the
1572445Sassar *    documentation and/or other materials provided with the distribution.
1672445Sassar *
1772445Sassar * 3. Neither the name of the Institute nor the names of its contributors
1872445Sassar *    may be used to endorse or promote products derived from this software
1972445Sassar *    without specific prior written permission.
2072445Sassar *
2172445Sassar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2272445Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2372445Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2472445Sassar * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2572445Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2672445Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2772445Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2872445Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2972445Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3072445Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3172445Sassar * SUCH DAMAGE.
3272445Sassar */
3372445Sassar
3472445Sassar#include "kuser_locl.h"
3572445Sassar
36178825SdfrRCSID("$Id: generate-requests.c 19233 2006-12-06 08:04:05Z lha $");
3772445Sassar
3872445Sassarstatic krb5_error_code
3972445Sassarnull_key_proc (krb5_context context,
4072445Sassar	       krb5_enctype type,
4172445Sassar	       krb5_salt salt,
4272445Sassar	       krb5_const_pointer keyseed,
4372445Sassar	       krb5_keyblock **key)
4472445Sassar{
4572445Sassar    return ENOTTY;
4672445Sassar}
4772445Sassar
4872445Sassarstatic unsigned
4972445Sassarread_words (const char *filename, char ***ret_w)
5072445Sassar{
5172445Sassar    unsigned n, alloc;
5272445Sassar    FILE *f;
5372445Sassar    char buf[256];
5472445Sassar    char **w = NULL;
5572445Sassar
5672445Sassar    f = fopen (filename, "r");
5772445Sassar    if (f == NULL)
5872445Sassar	err (1, "cannot open %s", filename);
5972445Sassar    alloc = n = 0;
6072445Sassar    while (fgets (buf, sizeof(buf), f) != NULL) {
61178825Sdfr	buf[strcspn(buf, "\r\n")] = '\0';
6272445Sassar	if (n >= alloc) {
6372445Sassar	    alloc += 16;
6472445Sassar	    w = erealloc (w, alloc * sizeof(char **));
6572445Sassar	}
6672445Sassar	w[n++] = estrdup (buf);
6772445Sassar    }
6872445Sassar    *ret_w = w;
69178825Sdfr    if (n == 0)
70178825Sdfr	errx(1, "%s is an empty file, no words to try", filename);
7172445Sassar    return n;
7272445Sassar}
7372445Sassar
7472445Sassarstatic void
7572445Sassargenerate_requests (const char *filename, unsigned nreq)
7672445Sassar{
7772445Sassar    krb5_context context;
7872445Sassar    krb5_error_code ret;
7972445Sassar    krb5_creds cred;
8072445Sassar    int i;
8172445Sassar    char **words;
8272445Sassar    unsigned nwords;
8372445Sassar
8472445Sassar    ret = krb5_init_context (&context);
8572445Sassar    if (ret)
8672445Sassar	errx (1, "krb5_init_context failed: %d", ret);
8772445Sassar
8872445Sassar    nwords = read_words (filename, &words);
8972445Sassar
9072445Sassar    for (i = 0; i < nreq; ++i) {
9172445Sassar	char *name = words[rand() % nwords];
9272445Sassar	krb5_realm *client_realm;
9372445Sassar
9472445Sassar	memset(&cred, 0, sizeof(cred));
9572445Sassar
9672445Sassar	ret = krb5_parse_name (context, name, &cred.client);
9772445Sassar	if (ret)
9872445Sassar	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
9972445Sassar	client_realm = krb5_princ_realm (context, cred.client);
10072445Sassar
10172445Sassar	ret = krb5_make_principal(context, &cred.server, *client_realm,
10272445Sassar				  KRB5_TGS_NAME, *client_realm, NULL);
10372445Sassar	if (ret)
10472445Sassar	    krb5_err (context, 1, ret, "krb5_make_principal");
10572445Sassar
10672445Sassar	ret = krb5_get_in_cred (context, 0, NULL, NULL, NULL, NULL,
10772445Sassar				null_key_proc, NULL, NULL, NULL,
10872445Sassar				&cred, NULL);
109178825Sdfr	krb5_free_cred_contents (context, &cred);
11072445Sassar    }
11172445Sassar}
11272445Sassar
11372445Sassarstatic int version_flag	= 0;
11472445Sassarstatic int help_flag	= 0;
11572445Sassar
11672445Sassarstatic struct getargs args[] = {
11772445Sassar    { "version", 	0,   arg_flag, &version_flag },
11872445Sassar    { "help",		0,   arg_flag, &help_flag }
11972445Sassar};
12072445Sassar
12172445Sassarstatic void
12272445Sassarusage (int ret)
12372445Sassar{
12472445Sassar    arg_printusage (args,
12572445Sassar		    sizeof(args)/sizeof(*args),
12672445Sassar		    NULL,
12772445Sassar		    "file number");
12872445Sassar    exit (ret);
12972445Sassar}
13072445Sassar
13172445Sassarint
13272445Sassarmain(int argc, char **argv)
13372445Sassar{
134178825Sdfr    int optidx = 0;
13572445Sassar    int nreq;
13672445Sassar    char *end;
13772445Sassar
13878527Sassar    setprogname(argv[0]);
139178825Sdfr    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
14072445Sassar	usage(1);
14190926Snectar
14290926Snectar    if (help_flag)
14390926Snectar	usage (0);
14490926Snectar
14590926Snectar    if(version_flag) {
14690926Snectar	print_version(NULL);
14790926Snectar	exit(0);
14890926Snectar    }
14990926Snectar
150178825Sdfr    argc -= optidx;
151178825Sdfr    argv += optidx;
15272445Sassar
15372445Sassar    if (argc != 2)
15472445Sassar	usage (1);
15572445Sassar    srand (0);
15672445Sassar    nreq = strtol (argv[1], &end, 0);
15772445Sassar    if (argv[1] == end || *end != '\0')
15872445Sassar	usage (1);
15972445Sassar    generate_requests (argv[0], nreq);
16072445Sassar    return 0;
16172445Sassar}
162