generate-requests.c revision 90926
1227825Stheraven/*
2227825Stheraven * Copyright (c) 2000 - 2001 Kungliga Tekniska H�gskolan
3227825Stheraven * (Royal Institute of Technology, Stockholm, Sweden).
4227825Stheraven * All rights reserved.
5227825Stheraven *
6227825Stheraven * Redistribution and use in source and binary forms, with or without
7227825Stheraven * modification, are permitted provided that the following conditions
8227825Stheraven * are met:
9227825Stheraven *
10262801Sdim * 1. Redistributions of source code must retain the above copyright
11262801Sdim *    notice, this list of conditions and the following disclaimer.
12227825Stheraven *
13227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright
14227825Stheraven *    notice, this list of conditions and the following disclaimer in the
15227825Stheraven *    documentation and/or other materials provided with the distribution.
16241903Sdim *
17241903Sdim * 3. Neither the name of the Institute nor the names of its contributors
18241903Sdim *    may be used to endorse or promote products derived from this software
19241903Sdim *    without specific prior written permission.
20249998Sdim *
21227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22232950Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23232950Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24232950Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25262801Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26232950Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27232950Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28232950Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30241903Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31241903Sdim * SUCH DAMAGE.
32241903Sdim */
33262801Sdim
34241903Sdim#include "kuser_locl.h"
35241903Sdim
36227825StheravenRCSID("$Id: generate-requests.c,v 1.4 2001/08/24 01:07:22 assar Exp $");
37227825Stheraven
38262801Sdimstatic krb5_error_code
39262801Sdimnull_key_proc (krb5_context context,
40227825Stheraven	       krb5_enctype type,
41227825Stheraven	       krb5_salt salt,
42227825Stheraven	       krb5_const_pointer keyseed,
43227825Stheraven	       krb5_keyblock **key)
44262801Sdim{
45227825Stheraven    return ENOTTY;
46227825Stheraven}
47227825Stheraven
48227825Stheravenstatic unsigned
49227825Stheravenread_words (const char *filename, char ***ret_w)
50227825Stheraven{
51227825Stheraven    unsigned n, alloc;
52227825Stheraven    FILE *f;
53227825Stheraven    char buf[256];
54227825Stheraven    char **w = NULL;
55227825Stheraven
56227825Stheraven    f = fopen (filename, "r");
57227825Stheraven    if (f == NULL)
58227825Stheraven	err (1, "cannot open %s", filename);
59227825Stheraven    alloc = n = 0;
60227825Stheraven    while (fgets (buf, sizeof(buf), f) != NULL) {
61227825Stheraven	if (buf[strlen (buf) - 1] == '\n')
62227825Stheraven	    buf[strlen (buf) - 1] = '\0';
63227825Stheraven	if (n >= alloc) {
64227825Stheraven	    alloc += 16;
65227825Stheraven	    w = erealloc (w, alloc * sizeof(char **));
66227825Stheraven	}
67227825Stheraven	w[n++] = estrdup (buf);
68227825Stheraven    }
69227825Stheraven    *ret_w = w;
70227825Stheraven    return n;
71262801Sdim}
72227825Stheraven
73227825Stheravenstatic void
74227825Stheravengenerate_requests (const char *filename, unsigned nreq)
75227825Stheraven{
76227825Stheraven    krb5_context context;
77227825Stheraven    krb5_error_code ret;
78227825Stheraven    krb5_creds cred;
79227825Stheraven    int i;
80227825Stheraven    char **words;
81227825Stheraven    unsigned nwords;
82227825Stheraven
83227825Stheraven    ret = krb5_init_context (&context);
84227825Stheraven    if (ret)
85227825Stheraven	errx (1, "krb5_init_context failed: %d", ret);
86227825Stheraven
87227825Stheraven    nwords = read_words (filename, &words);
88227825Stheraven
89227825Stheraven    for (i = 0; i < nreq; ++i) {
90262801Sdim	char *name = words[rand() % nwords];
91227825Stheraven	krb5_realm *client_realm;
92227825Stheraven
93227825Stheraven	memset(&cred, 0, sizeof(cred));
94227825Stheraven
95227825Stheraven	ret = krb5_parse_name (context, name, &cred.client);
96227825Stheraven	if (ret)
97227825Stheraven	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
98227825Stheraven	client_realm = krb5_princ_realm (context, cred.client);
99227825Stheraven
100262801Sdim	ret = krb5_make_principal(context, &cred.server, *client_realm,
101227825Stheraven				  KRB5_TGS_NAME, *client_realm, NULL);
102232950Stheraven	if (ret)
103227825Stheraven	    krb5_err (context, 1, ret, "krb5_make_principal");
104227825Stheraven
105227825Stheraven	ret = krb5_get_in_cred (context, 0, NULL, NULL, NULL, NULL,
106227825Stheraven				null_key_proc, NULL, NULL, NULL,
107227825Stheraven				&cred, NULL);
108227825Stheraven	krb5_free_creds_contents (context, &cred);
109227825Stheraven    }
110227825Stheraven}
111227825Stheraven
112227825Stheravenstatic int version_flag	= 0;
113227825Stheravenstatic int help_flag	= 0;
114227825Stheraven
115227825Stheravenstatic struct getargs args[] = {
116227825Stheraven    { "version", 	0,   arg_flag, &version_flag },
117227825Stheraven    { "help",		0,   arg_flag, &help_flag }
118227825Stheraven};
119262801Sdim
120227825Stheravenstatic void
121227825Stheravenusage (int ret)
122227825Stheraven{
123227825Stheraven    arg_printusage (args,
124227825Stheraven		    sizeof(args)/sizeof(*args),
125227825Stheraven		    NULL,
126227825Stheraven		    "file number");
127262801Sdim    exit (ret);
128227825Stheraven}
129227825Stheraven
130227825Stheravenint
131227825Stheravenmain(int argc, char **argv)
132227825Stheraven{
133227825Stheraven    int optind = 0;
134262801Sdim    int nreq;
135227825Stheraven    char *end;
136227825Stheraven
137227825Stheraven    setprogname(argv[0]);
138227825Stheraven    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
139227825Stheraven	usage(1);
140227825Stheraven
141262801Sdim    if (help_flag)
142227825Stheraven	usage (0);
143227825Stheraven
144227825Stheraven    if(version_flag) {
145227825Stheraven	print_version(NULL);
146227825Stheraven	exit(0);
147227825Stheraven    }
148262801Sdim
149262801Sdim    argc -= optind;
150227825Stheraven    argv += optind;
151227825Stheraven
152227825Stheraven    if (argc != 2)
153262801Sdim	usage (1);
154227825Stheraven    srand (0);
155262801Sdim    nreq = strtol (argv[1], &end, 0);
156227825Stheraven    if (argv[1] == end || *end != '\0')
157232950Stheraven	usage (1);
158232950Stheraven    generate_requests (argv[0], nreq);
159262801Sdim    return 0;
160262801Sdim}
161227825Stheraven