1117395Skan/*
2169689Skan * Copyright (c) 2000 - 2001 Kungliga Tekniska H��gskolan
3117395Skan * (Royal Institute of Technology, Stockholm, Sweden).
4117395Skan * All rights reserved.
5117395Skan *
6117395Skan * Redistribution and use in source and binary forms, with or without
7117395Skan * modification, are permitted provided that the following conditions
8117395Skan * are met:
9117395Skan *
10117395Skan * 1. Redistributions of source code must retain the above copyright
11117395Skan *    notice, this list of conditions and the following disclaimer.
12117395Skan *
13117395Skan * 2. Redistributions in binary form must reproduce the above copyright
14117395Skan *    notice, this list of conditions and the following disclaimer in the
15117395Skan *    documentation and/or other materials provided with the distribution.
16117395Skan *
17117395Skan * 3. Neither the name of the Institute nor the names of its contributors
18117395Skan *    may be used to endorse or promote products derived from this software
19117395Skan *    without specific prior written permission.
20117395Skan *
21117395Skan * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22117395Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23117395Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24117395Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25117395Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26117395Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29117395Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30117395Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31117395Skan * SUCH DAMAGE.
32117395Skan */
33117395Skan
34117395Skan#include "kadmin_locl.h"
35117395Skan
36117395Skan#define WORDS_FILENAME "/usr/share/dict/words"
37169689Skan
38169689Skan#define NUSERS 1000
39169689Skan
40169689Skan#define WORDBUF_SIZE 65535
41169689Skan
42117395Skanstatic unsigned
43117395Skanread_words (const char *filename, char ***ret_w)
44117395Skan{
45117395Skan    unsigned n, alloc;
46117395Skan    FILE *f;
47117395Skan    char buf[256];
48117395Skan    char **w = NULL;
49117395Skan    char *wbuf = NULL, *wptr = NULL, *wend = NULL;
50117395Skan
51117395Skan    f = fopen (filename, "r");
52117395Skan    if (f == NULL)
53117395Skan	err (1, "cannot open %s", filename);
54117395Skan    alloc = n = 0;
55117395Skan    while (fgets (buf, sizeof(buf), f) != NULL) {
56117395Skan	size_t len;
57117395Skan
58117395Skan	buf[strcspn(buf, "\r\n")] = '\0';
59117395Skan	if (n >= alloc) {
60117395Skan	    alloc = max(alloc + 16, alloc * 2);
61117395Skan	    w = erealloc (w, alloc * sizeof(char **));
62117395Skan	}
63117395Skan	len = strlen(buf);
64117395Skan	if (wptr + len + 1 >= wend) {
65117395Skan	    wptr = wbuf = emalloc (WORDBUF_SIZE);
66117395Skan	    wend = wbuf + WORDBUF_SIZE;
67117395Skan	}
68117395Skan	memmove (wptr, buf, len + 1);
69117395Skan	w[n++] = wptr;
70117395Skan	wptr += len + 1;
71117395Skan    }
72117395Skan    if (n == 0)
73117395Skan	errx(1, "%s is an empty file, no words to try", filename);
74117395Skan    *ret_w = w;
75117395Skan    fclose(f);
76117395Skan    return n;
77117395Skan}
78117395Skan
79117395Skanstatic void
80117395Skanadd_user (krb5_context context, void *kadm_handle,
81117395Skan	  unsigned nwords, char **words)
82117395Skan{
83117395Skan    kadm5_principal_ent_rec princ;
84117395Skan    char name[64];
85117395Skan    int r1, r2;
86117395Skan    krb5_error_code ret;
87117395Skan    int mask;
88117395Skan
89117395Skan    r1 = rand();
90117395Skan    r2 = rand();
91117395Skan
92117395Skan    snprintf (name, sizeof(name), "%s%d", words[r1 % nwords], r2 % 1000);
93117395Skan
94117395Skan    mask = KADM5_PRINCIPAL;
95117395Skan
96117395Skan    memset(&princ, 0, sizeof(princ));
97117395Skan    ret = krb5_parse_name(context, name, &princ.principal);
98117395Skan    if (ret)
99117395Skan	krb5_err(context, 1, ret, "krb5_parse_name");
100117395Skan
101117395Skan    ret = kadm5_create_principal (kadm_handle, &princ, mask, name);
102117395Skan    if (ret)
103117395Skan	krb5_err (context, 1, ret, "kadm5_create_principal");
104117395Skan    kadm5_free_principal_ent(kadm_handle, &princ);
105117395Skan    printf ("%s\n", name);
106117395Skan}
107117395Skan
108117395Skanstatic void
109117395Skanadd_users (const char *filename, unsigned n)
110117395Skan{
111117395Skan    krb5_error_code ret;
112117395Skan    int i;
113117395Skan    void *kadm_handle;
114117395Skan    krb5_context context;
115117395Skan    unsigned nwords;
116117395Skan    char **words;
117117395Skan
118117395Skan    ret = krb5_init_context(&context);
119117395Skan    if (ret)
120117395Skan	errx (1, "krb5_init_context failed: %d", ret);
121117395Skan    ret = kadm5_s_init_with_password_ctx(context,
122117395Skan					 KADM5_ADMIN_SERVICE,
123117395Skan					 NULL,
124117395Skan					 KADM5_ADMIN_SERVICE,
125117395Skan					 NULL, 0, 0,
126117395Skan					 &kadm_handle);
127117395Skan    if(ret)
128117395Skan	krb5_err(context, 1, ret, "kadm5_init_with_password");
129117395Skan
130117395Skan    nwords = read_words (filename, &words);
131117395Skan
132117395Skan    for (i = 0; i < n; ++i)
133117395Skan	add_user (context, kadm_handle, nwords, words);
134117395Skan    kadm5_destroy(kadm_handle);
135117395Skan    krb5_free_context(context);
136117395Skan}
137117395Skan
138117395Skanstatic int version_flag	= 0;
139117395Skanstatic int help_flag	= 0;
140117395Skan
141117395Skanstatic struct getargs args[] = {
142117395Skan    { "version", 	0,   arg_flag, &version_flag },
143117395Skan    { "help",		0,   arg_flag, &help_flag }
144117395Skan};
145117395Skan
146117395Skanstatic void
147117395Skanusage (int ret)
148117395Skan{
149117395Skan    arg_printusage (args,
150117395Skan		    sizeof(args)/sizeof(*args),
151117395Skan		    NULL,
152117395Skan		    "[filename [n]]");
153117395Skan    exit (ret);
154117395Skan}
155117395Skan
156117395Skanint
157169689Skanmain(int argc, char **argv)
158169689Skan{
159    int optidx = 0;
160    int n = NUSERS;
161    const char *filename = WORDS_FILENAME;
162
163    setprogname(argv[0]);
164    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
165	usage(1);
166    if (help_flag)
167	usage (0);
168    if (version_flag) {
169	print_version(NULL);
170	return 0;
171    }
172    srand (0);
173    argc -= optidx;
174    argv += optidx;
175
176    if (argc > 0) {
177	if (argc > 1)
178	    n = atoi(argv[1]);
179	filename = argv[0];
180    }
181
182    add_users (filename, n);
183    return 0;
184}
185