Path: blob/main/crypto/heimdal/kuser/generate-requests.c
34876 views
/*1* Copyright (c) 2000 - 2004 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "kuser_locl.h"3435static unsigned36read_words (const char *filename, char ***ret_w)37{38unsigned n, alloc;39FILE *f;40char buf[256];41char **w = NULL;4243f = fopen (filename, "r");44if (f == NULL)45err (1, "cannot open %s", filename);46alloc = n = 0;47while (fgets (buf, sizeof(buf), f) != NULL) {48buf[strcspn(buf, "\r\n")] = '\0';49if (n >= alloc) {50alloc += 16;51w = erealloc (w, alloc * sizeof(char **));52}53w[n++] = estrdup (buf);54}55*ret_w = w;56if (n == 0)57errx(1, "%s is an empty file, no words to try", filename);58fclose(f);59return n;60}6162static void63generate_requests (const char *filename, unsigned nreq)64{65krb5_principal client;66krb5_context context;67krb5_error_code ret;68krb5_creds cred;69int i;70char **words;71unsigned nwords;7273ret = krb5_init_context (&context);74if (ret)75errx (1, "krb5_init_context failed: %d", ret);7677nwords = read_words (filename, &words);7879for (i = 0; i < nreq; ++i) {80char *name = words[rand() % nwords];8182memset(&cred, 0, sizeof(cred));8384ret = krb5_parse_name (context, name, &client);85if (ret)86krb5_err (context, 1, ret, "krb5_parse_name %s", name);8788ret = krb5_get_init_creds_password (context, &cred, client, "",89NULL, NULL, 0, NULL, NULL);90if (ret)91krb5_free_cred_contents (context, &cred);92krb5_free_principal(context, client);93}94}9596static int version_flag = 0;97static int help_flag = 0;9899static struct getargs args[] = {100{ "version", 0, arg_flag, &version_flag },101{ "help", 0, arg_flag, &help_flag }102};103104static void105usage (int ret)106{107arg_printusage (args,108sizeof(args)/sizeof(*args),109NULL,110"file number");111exit (ret);112}113114int115main(int argc, char **argv)116{117int optidx = 0;118int nreq;119char *end;120121setprogname(argv[0]);122if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))123usage(1);124125if (help_flag)126usage (0);127128if(version_flag) {129print_version(NULL);130exit(0);131}132133argc -= optidx;134argv += optidx;135136if (argc != 2)137usage (1);138srand (0);139nreq = strtol (argv[1], &end, 0);140if (argv[1] == end || *end != '\0')141usage (1);142generate_requests (argv[0], nreq);143return 0;144}145146147