Path: blob/main/crypto/heimdal/kadmin/add-random-users.c
34874 views
/*1* Copyright (c) 2000 - 2001 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 "kadmin_locl.h"3435#define WORDS_FILENAME "/usr/share/dict/words"3637#define NUSERS 10003839#define WORDBUF_SIZE 655354041static unsigned42read_words (const char *filename, char ***ret_w)43{44unsigned n, alloc;45FILE *f;46char buf[256];47char **w = NULL;48char *wbuf = NULL, *wptr = NULL, *wend = NULL;4950f = fopen (filename, "r");51if (f == NULL)52err (1, "cannot open %s", filename);53alloc = n = 0;54while (fgets (buf, sizeof(buf), f) != NULL) {55size_t len;5657buf[strcspn(buf, "\r\n")] = '\0';58if (n >= alloc) {59alloc = max(alloc + 16, alloc * 2);60w = erealloc (w, alloc * sizeof(char **));61}62len = strlen(buf);63if (wptr + len + 1 >= wend) {64wptr = wbuf = emalloc (WORDBUF_SIZE);65wend = wbuf + WORDBUF_SIZE;66}67memmove (wptr, buf, len + 1);68w[n++] = wptr;69wptr += len + 1;70}71if (n == 0)72errx(1, "%s is an empty file, no words to try", filename);73*ret_w = w;74fclose(f);75return n;76}7778static void79add_user (krb5_context context, void *kadm_handle,80unsigned nwords, char **words)81{82kadm5_principal_ent_rec princ;83char name[64];84int r1, r2;85krb5_error_code ret;86int mask;8788r1 = rand();89r2 = rand();9091snprintf (name, sizeof(name), "%s%d", words[r1 % nwords], r2 % 1000);9293mask = KADM5_PRINCIPAL;9495memset(&princ, 0, sizeof(princ));96ret = krb5_parse_name(context, name, &princ.principal);97if (ret)98krb5_err(context, 1, ret, "krb5_parse_name");99100ret = kadm5_create_principal (kadm_handle, &princ, mask, name);101if (ret)102krb5_err (context, 1, ret, "kadm5_create_principal");103kadm5_free_principal_ent(kadm_handle, &princ);104printf ("%s\n", name);105}106107static void108add_users (const char *filename, unsigned n)109{110krb5_error_code ret;111int i;112void *kadm_handle;113krb5_context context;114unsigned nwords;115char **words;116117ret = krb5_init_context(&context);118if (ret)119errx (1, "krb5_init_context failed: %d", ret);120ret = kadm5_s_init_with_password_ctx(context,121KADM5_ADMIN_SERVICE,122NULL,123KADM5_ADMIN_SERVICE,124NULL, 0, 0,125&kadm_handle);126if(ret)127krb5_err(context, 1, ret, "kadm5_init_with_password");128129nwords = read_words (filename, &words);130131for (i = 0; i < n; ++i)132add_user (context, kadm_handle, nwords, words);133kadm5_destroy(kadm_handle);134krb5_free_context(context);135}136137static int version_flag = 0;138static int help_flag = 0;139140static struct getargs args[] = {141{ "version", 0, arg_flag, &version_flag },142{ "help", 0, arg_flag, &help_flag }143};144145static void146usage (int ret)147{148arg_printusage (args,149sizeof(args)/sizeof(*args),150NULL,151"[filename [n]]");152exit (ret);153}154155int156main(int argc, char **argv)157{158int optidx = 0;159int n = NUSERS;160const char *filename = WORDS_FILENAME;161162setprogname(argv[0]);163if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))164usage(1);165if (help_flag)166usage (0);167if (version_flag) {168print_version(NULL);169return 0;170}171srand (0);172argc -= optidx;173argv += optidx;174175if (argc > 0) {176if (argc > 1)177n = atoi(argv[1]);178filename = argv[0];179}180181add_users (filename, n);182return 0;183}184185186