1 /* 2 * Copyright (c) 2000 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "kadmin_locl.h" 35 36 RCSID("$Id: add-random-users.c,v 1.2 2000/12/31 07:43:39 assar Exp $"); 37 38 #define WORDS_FILENAME "/usr/share/dict/words" 39 40 #define NUSERS 1000 41 42 static unsigned 43 read_words (const char *filename, char ***ret_w) 44 { 45 unsigned n, alloc; 46 FILE *f; 47 char buf[256]; 48 char **w = NULL; 49 50 f = fopen (filename, "r"); 51 if (f == NULL) 52 err (1, "cannot open %s", filename); 53 alloc = n = 0; 54 while (fgets (buf, sizeof(buf), f) != NULL) { 55 if (buf[strlen (buf) - 1] == '\n') 56 buf[strlen (buf) - 1] = '\0'; 57 if (n >= alloc) { 58 alloc += 16; 59 w = erealloc (w, alloc * sizeof(char **)); 60 } 61 w[n++] = estrdup (buf); 62 } 63 *ret_w = w; 64 return n; 65 } 66 67 static void 68 add_user (krb5_context context, void *kadm_handle, 69 unsigned nwords, char **words) 70 { 71 kadm5_principal_ent_rec princ; 72 char name[64]; 73 int r1, r2; 74 krb5_error_code ret; 75 int mask; 76 77 r1 = rand(); 78 r2 = rand(); 79 80 snprintf (name, sizeof(name), "%s%d", words[r1 % nwords], r2 % 1000); 81 82 mask = KADM5_PRINCIPAL; 83 84 memset(&princ, 0, sizeof(princ)); 85 ret = krb5_parse_name(context, name, &princ.principal); 86 if (ret) 87 krb5_err(context, 1, ret, "krb5_parse_name"); 88 89 ret = kadm5_create_principal (kadm_handle, &princ, mask, name); 90 if (ret) 91 krb5_err (context, 1, ret, "kadm5_create_principal"); 92 kadm5_free_principal_ent(kadm_handle, &princ); 93 printf ("%s\n", name); 94 } 95 96 static void 97 add_users (unsigned n) 98 { 99 krb5_error_code ret; 100 int i; 101 void *kadm_handle; 102 krb5_context context; 103 unsigned nwords; 104 char **words; 105 106 ret = krb5_init_context(&context); 107 if (ret) 108 errx (1, "krb5_init_context failed: %d", ret); 109 ret = kadm5_s_init_with_password_ctx(context, 110 KADM5_ADMIN_SERVICE, 111 NULL, 112 KADM5_ADMIN_SERVICE, 113 NULL, 0, 0, 114 &kadm_handle); 115 if(ret) 116 krb5_err(context, 1, ret, "kadm5_init_with_password"); 117 118 nwords = read_words (WORDS_FILENAME, &words); 119 120 for (i = 0; i < n; ++i) 121 add_user (context, kadm_handle, nwords, words); 122 kadm5_destroy(kadm_handle); 123 krb5_free_context(context); 124 } 125 126 static int version_flag = 0; 127 static int help_flag = 0; 128 129 static struct getargs args[] = { 130 { "version", 0, arg_flag, &version_flag }, 131 { "help", 0, arg_flag, &help_flag } 132 }; 133 134 static void 135 usage (int ret) 136 { 137 arg_printusage (args, 138 sizeof(args)/sizeof(*args), 139 NULL, 140 NULL); 141 exit (ret); 142 } 143 144 int 145 main(int argc, char **argv) 146 { 147 int optind = 0; 148 149 set_progname(argv[0]); 150 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)) 151 usage(1); 152 if (help_flag) 153 usage (0); 154 srand (0); 155 add_users (NUSERS); 156 return 0; 157 } 158