xref: /freebsd/crypto/heimdal/kadmin/add-random-users.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund #include "kadmin_locl.h"
355e9cd1aeSAssar Westerlund 
365e9cd1aeSAssar Westerlund #define WORDS_FILENAME "/usr/share/dict/words"
375e9cd1aeSAssar Westerlund 
385e9cd1aeSAssar Westerlund #define NUSERS 1000
395e9cd1aeSAssar Westerlund 
404137ff4cSJacques Vidrine #define WORDBUF_SIZE 65535
414137ff4cSJacques Vidrine 
425e9cd1aeSAssar Westerlund static unsigned
read_words(const char * filename,char *** ret_w)435e9cd1aeSAssar Westerlund read_words (const char *filename, char ***ret_w)
445e9cd1aeSAssar Westerlund {
455e9cd1aeSAssar Westerlund     unsigned n, alloc;
465e9cd1aeSAssar Westerlund     FILE *f;
475e9cd1aeSAssar Westerlund     char buf[256];
485e9cd1aeSAssar Westerlund     char **w = NULL;
494137ff4cSJacques Vidrine     char *wbuf = NULL, *wptr = NULL, *wend = NULL;
505e9cd1aeSAssar Westerlund 
515e9cd1aeSAssar Westerlund     f = fopen (filename, "r");
525e9cd1aeSAssar Westerlund     if (f == NULL)
535e9cd1aeSAssar Westerlund 	err (1, "cannot open %s", filename);
545e9cd1aeSAssar Westerlund     alloc = n = 0;
555e9cd1aeSAssar Westerlund     while (fgets (buf, sizeof(buf), f) != NULL) {
564137ff4cSJacques Vidrine 	size_t len;
574137ff4cSJacques Vidrine 
58c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
595e9cd1aeSAssar Westerlund 	if (n >= alloc) {
604137ff4cSJacques Vidrine 	    alloc = max(alloc + 16, alloc * 2);
615e9cd1aeSAssar Westerlund 	    w = erealloc (w, alloc * sizeof(char **));
625e9cd1aeSAssar Westerlund 	}
634137ff4cSJacques Vidrine 	len = strlen(buf);
644137ff4cSJacques Vidrine 	if (wptr + len + 1 >= wend) {
654137ff4cSJacques Vidrine 	    wptr = wbuf = emalloc (WORDBUF_SIZE);
664137ff4cSJacques Vidrine 	    wend = wbuf + WORDBUF_SIZE;
674137ff4cSJacques Vidrine 	}
684137ff4cSJacques Vidrine 	memmove (wptr, buf, len + 1);
694137ff4cSJacques Vidrine 	w[n++] = wptr;
704137ff4cSJacques Vidrine 	wptr += len + 1;
715e9cd1aeSAssar Westerlund     }
72c19800e8SDoug Rabson     if (n == 0)
73c19800e8SDoug Rabson 	errx(1, "%s is an empty file, no words to try", filename);
745e9cd1aeSAssar Westerlund     *ret_w = w;
75*ae771770SStanislav Sedov     fclose(f);
765e9cd1aeSAssar Westerlund     return n;
775e9cd1aeSAssar Westerlund }
785e9cd1aeSAssar Westerlund 
795e9cd1aeSAssar Westerlund static void
add_user(krb5_context context,void * kadm_handle,unsigned nwords,char ** words)805e9cd1aeSAssar Westerlund add_user (krb5_context context, void *kadm_handle,
815e9cd1aeSAssar Westerlund 	  unsigned nwords, char **words)
825e9cd1aeSAssar Westerlund {
835e9cd1aeSAssar Westerlund     kadm5_principal_ent_rec princ;
845e9cd1aeSAssar Westerlund     char name[64];
855e9cd1aeSAssar Westerlund     int r1, r2;
865e9cd1aeSAssar Westerlund     krb5_error_code ret;
875e9cd1aeSAssar Westerlund     int mask;
885e9cd1aeSAssar Westerlund 
895e9cd1aeSAssar Westerlund     r1 = rand();
905e9cd1aeSAssar Westerlund     r2 = rand();
915e9cd1aeSAssar Westerlund 
925e9cd1aeSAssar Westerlund     snprintf (name, sizeof(name), "%s%d", words[r1 % nwords], r2 % 1000);
935e9cd1aeSAssar Westerlund 
945e9cd1aeSAssar Westerlund     mask = KADM5_PRINCIPAL;
955e9cd1aeSAssar Westerlund 
965e9cd1aeSAssar Westerlund     memset(&princ, 0, sizeof(princ));
975e9cd1aeSAssar Westerlund     ret = krb5_parse_name(context, name, &princ.principal);
985e9cd1aeSAssar Westerlund     if (ret)
995e9cd1aeSAssar Westerlund 	krb5_err(context, 1, ret, "krb5_parse_name");
1005e9cd1aeSAssar Westerlund 
1015e9cd1aeSAssar Westerlund     ret = kadm5_create_principal (kadm_handle, &princ, mask, name);
1025e9cd1aeSAssar Westerlund     if (ret)
1035e9cd1aeSAssar Westerlund 	krb5_err (context, 1, ret, "kadm5_create_principal");
1045e9cd1aeSAssar Westerlund     kadm5_free_principal_ent(kadm_handle, &princ);
1055e9cd1aeSAssar Westerlund     printf ("%s\n", name);
1065e9cd1aeSAssar Westerlund }
1075e9cd1aeSAssar Westerlund 
1085e9cd1aeSAssar Westerlund static void
add_users(const char * filename,unsigned n)1094137ff4cSJacques Vidrine add_users (const char *filename, unsigned n)
1105e9cd1aeSAssar Westerlund {
1115e9cd1aeSAssar Westerlund     krb5_error_code ret;
1125e9cd1aeSAssar Westerlund     int i;
1135e9cd1aeSAssar Westerlund     void *kadm_handle;
1145e9cd1aeSAssar Westerlund     krb5_context context;
1155e9cd1aeSAssar Westerlund     unsigned nwords;
1165e9cd1aeSAssar Westerlund     char **words;
1175e9cd1aeSAssar Westerlund 
1185e9cd1aeSAssar Westerlund     ret = krb5_init_context(&context);
1195e9cd1aeSAssar Westerlund     if (ret)
1205e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
1215e9cd1aeSAssar Westerlund     ret = kadm5_s_init_with_password_ctx(context,
1225e9cd1aeSAssar Westerlund 					 KADM5_ADMIN_SERVICE,
1235e9cd1aeSAssar Westerlund 					 NULL,
1245e9cd1aeSAssar Westerlund 					 KADM5_ADMIN_SERVICE,
1255e9cd1aeSAssar Westerlund 					 NULL, 0, 0,
1265e9cd1aeSAssar Westerlund 					 &kadm_handle);
1275e9cd1aeSAssar Westerlund     if(ret)
1285e9cd1aeSAssar Westerlund 	krb5_err(context, 1, ret, "kadm5_init_with_password");
1295e9cd1aeSAssar Westerlund 
1304137ff4cSJacques Vidrine     nwords = read_words (filename, &words);
1315e9cd1aeSAssar Westerlund 
1325e9cd1aeSAssar Westerlund     for (i = 0; i < n; ++i)
1335e9cd1aeSAssar Westerlund 	add_user (context, kadm_handle, nwords, words);
1345e9cd1aeSAssar Westerlund     kadm5_destroy(kadm_handle);
1355e9cd1aeSAssar Westerlund     krb5_free_context(context);
1365e9cd1aeSAssar Westerlund }
1375e9cd1aeSAssar Westerlund 
1385e9cd1aeSAssar Westerlund static int version_flag	= 0;
1395e9cd1aeSAssar Westerlund static int help_flag	= 0;
1405e9cd1aeSAssar Westerlund 
1415e9cd1aeSAssar Westerlund static struct getargs args[] = {
1425e9cd1aeSAssar Westerlund     { "version", 	0,   arg_flag, &version_flag },
1435e9cd1aeSAssar Westerlund     { "help",		0,   arg_flag, &help_flag }
1445e9cd1aeSAssar Westerlund };
1455e9cd1aeSAssar Westerlund 
1465e9cd1aeSAssar Westerlund static void
usage(int ret)1475e9cd1aeSAssar Westerlund usage (int ret)
1485e9cd1aeSAssar Westerlund {
1495e9cd1aeSAssar Westerlund     arg_printusage (args,
1505e9cd1aeSAssar Westerlund 		    sizeof(args)/sizeof(*args),
1515e9cd1aeSAssar Westerlund 		    NULL,
1524137ff4cSJacques Vidrine 		    "[filename [n]]");
1535e9cd1aeSAssar Westerlund     exit (ret);
1545e9cd1aeSAssar Westerlund }
1555e9cd1aeSAssar Westerlund 
1565e9cd1aeSAssar Westerlund int
main(int argc,char ** argv)1575e9cd1aeSAssar Westerlund main(int argc, char **argv)
1585e9cd1aeSAssar Westerlund {
159c19800e8SDoug Rabson     int optidx = 0;
1604137ff4cSJacques Vidrine     int n = NUSERS;
1614137ff4cSJacques Vidrine     const char *filename = WORDS_FILENAME;
1625e9cd1aeSAssar Westerlund 
163adb0ddaeSAssar Westerlund     setprogname(argv[0]);
164c19800e8SDoug Rabson     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
1655e9cd1aeSAssar Westerlund 	usage(1);
1665e9cd1aeSAssar Westerlund     if (help_flag)
1675e9cd1aeSAssar Westerlund 	usage (0);
1684137ff4cSJacques Vidrine     if (version_flag) {
1694137ff4cSJacques Vidrine 	print_version(NULL);
1704137ff4cSJacques Vidrine 	return 0;
1714137ff4cSJacques Vidrine     }
1725e9cd1aeSAssar Westerlund     srand (0);
173c19800e8SDoug Rabson     argc -= optidx;
174c19800e8SDoug Rabson     argv += optidx;
1754137ff4cSJacques Vidrine 
1764137ff4cSJacques Vidrine     if (argc > 0) {
1774137ff4cSJacques Vidrine 	if (argc > 1)
1784137ff4cSJacques Vidrine 	    n = atoi(argv[1]);
1794137ff4cSJacques Vidrine 	filename = argv[0];
1804137ff4cSJacques Vidrine     }
1814137ff4cSJacques Vidrine 
1824137ff4cSJacques Vidrine     add_users (filename, n);
1835e9cd1aeSAssar Westerlund     return 0;
1845e9cd1aeSAssar Westerlund }
185