xref: /freebsd/crypto/heimdal/kpasswd/kpasswd-generator.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * Copyright (c) 2000 - 2004 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 "kpasswd_locl.h"
355e9cd1aeSAssar Westerlund 
36*ae771770SStanislav Sedov RCSID("$Id$");
375e9cd1aeSAssar Westerlund 
385e9cd1aeSAssar Westerlund static unsigned
read_words(const char * filename,char *** ret_w)395e9cd1aeSAssar Westerlund read_words (const char *filename, char ***ret_w)
405e9cd1aeSAssar Westerlund {
415e9cd1aeSAssar Westerlund     unsigned n, alloc;
425e9cd1aeSAssar Westerlund     FILE *f;
435e9cd1aeSAssar Westerlund     char buf[256];
445e9cd1aeSAssar Westerlund     char **w = NULL;
455e9cd1aeSAssar Westerlund 
465e9cd1aeSAssar Westerlund     f = fopen (filename, "r");
475e9cd1aeSAssar Westerlund     if (f == NULL)
485e9cd1aeSAssar Westerlund 	err (1, "cannot open %s", filename);
495e9cd1aeSAssar Westerlund     alloc = n = 0;
505e9cd1aeSAssar Westerlund     while (fgets (buf, sizeof(buf), f) != NULL) {
51c19800e8SDoug Rabson 	buf[strcspn(buf, "\r\n")] = '\0';
525e9cd1aeSAssar Westerlund 	if (n >= alloc) {
535e9cd1aeSAssar Westerlund 	    alloc += 16;
545e9cd1aeSAssar Westerlund 	    w = erealloc (w, alloc * sizeof(char **));
555e9cd1aeSAssar Westerlund 	}
565e9cd1aeSAssar Westerlund 	w[n++] = estrdup (buf);
575e9cd1aeSAssar Westerlund     }
585e9cd1aeSAssar Westerlund     *ret_w = w;
59c19800e8SDoug Rabson     if (n == 0)
60c19800e8SDoug Rabson 	errx(1, "%s is an empty file, no words to try", filename);
61*ae771770SStanislav Sedov     fclose(f);
625e9cd1aeSAssar Westerlund     return n;
635e9cd1aeSAssar Westerlund }
645e9cd1aeSAssar Westerlund 
655e9cd1aeSAssar Westerlund static int
nop_prompter(krb5_context context,void * data,const char * name,const char * banner,int num_prompts,krb5_prompt prompts[])665e9cd1aeSAssar Westerlund nop_prompter (krb5_context context,
675e9cd1aeSAssar Westerlund 	      void *data,
68adb0ddaeSAssar Westerlund 	      const char *name,
695e9cd1aeSAssar Westerlund 	      const char *banner,
705e9cd1aeSAssar Westerlund 	      int num_prompts,
715e9cd1aeSAssar Westerlund 	      krb5_prompt prompts[])
725e9cd1aeSAssar Westerlund {
735e9cd1aeSAssar Westerlund     return 0;
745e9cd1aeSAssar Westerlund }
755e9cd1aeSAssar Westerlund 
765e9cd1aeSAssar Westerlund static void
generate_requests(const char * filename,unsigned nreq)775e9cd1aeSAssar Westerlund generate_requests (const char *filename, unsigned nreq)
785e9cd1aeSAssar Westerlund {
795e9cd1aeSAssar Westerlund     krb5_context context;
805e9cd1aeSAssar Westerlund     krb5_error_code ret;
815e9cd1aeSAssar Westerlund     int i;
825e9cd1aeSAssar Westerlund     char **words;
835e9cd1aeSAssar Westerlund     unsigned nwords;
845e9cd1aeSAssar Westerlund 
855e9cd1aeSAssar Westerlund     ret = krb5_init_context (&context);
865e9cd1aeSAssar Westerlund     if (ret)
875e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
885e9cd1aeSAssar Westerlund 
895e9cd1aeSAssar Westerlund     nwords = read_words (filename, &words);
905e9cd1aeSAssar Westerlund 
915e9cd1aeSAssar Westerlund     for (i = 0; i < nreq; ++i) {
925e9cd1aeSAssar Westerlund 	char *name = words[rand() % nwords];
93c19800e8SDoug Rabson 	krb5_get_init_creds_opt *opt;
945e9cd1aeSAssar Westerlund 	krb5_creds cred;
955e9cd1aeSAssar Westerlund 	krb5_principal principal;
965e9cd1aeSAssar Westerlund 	int result_code;
975e9cd1aeSAssar Westerlund 	krb5_data result_code_string, result_string;
985e9cd1aeSAssar Westerlund 	char *old_pwd, *new_pwd;
995e9cd1aeSAssar Westerlund 
100c19800e8SDoug Rabson 	krb5_get_init_creds_opt_alloc (context, &opt);
101c19800e8SDoug Rabson 	krb5_get_init_creds_opt_set_tkt_life (opt, 300);
102c19800e8SDoug Rabson 	krb5_get_init_creds_opt_set_forwardable (opt, FALSE);
103c19800e8SDoug Rabson 	krb5_get_init_creds_opt_set_proxiable (opt, FALSE);
1045e9cd1aeSAssar Westerlund 
1055e9cd1aeSAssar Westerlund 	ret = krb5_parse_name (context, name, &principal);
1065e9cd1aeSAssar Westerlund 	if (ret)
1075e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_parse_name %s", name);
1085e9cd1aeSAssar Westerlund 
1095e9cd1aeSAssar Westerlund 	asprintf (&old_pwd, "%s", name);
1105e9cd1aeSAssar Westerlund 	asprintf (&new_pwd, "%s2", name);
1115e9cd1aeSAssar Westerlund 
1125e9cd1aeSAssar Westerlund 	ret = krb5_get_init_creds_password (context,
1135e9cd1aeSAssar Westerlund 					    &cred,
1145e9cd1aeSAssar Westerlund 					    principal,
1155e9cd1aeSAssar Westerlund 					    old_pwd,
1165e9cd1aeSAssar Westerlund 					    nop_prompter,
1175e9cd1aeSAssar Westerlund 					    NULL,
1185e9cd1aeSAssar Westerlund 					    0,
1195e9cd1aeSAssar Westerlund 					    "kadmin/changepw",
120c19800e8SDoug Rabson 					    opt);
1215e9cd1aeSAssar Westerlund 	if( ret == KRB5KRB_AP_ERR_BAD_INTEGRITY
1225e9cd1aeSAssar Westerlund 	    || ret == KRB5KRB_AP_ERR_MODIFIED) {
1235e9cd1aeSAssar Westerlund 	    char *tmp;
1245e9cd1aeSAssar Westerlund 
1255e9cd1aeSAssar Westerlund 	    tmp = new_pwd;
1265e9cd1aeSAssar Westerlund 	    new_pwd = old_pwd;
1275e9cd1aeSAssar Westerlund 	    old_pwd = tmp;
1285e9cd1aeSAssar Westerlund 
1295e9cd1aeSAssar Westerlund 	    ret = krb5_get_init_creds_password (context,
1305e9cd1aeSAssar Westerlund 						&cred,
1315e9cd1aeSAssar Westerlund 						principal,
1325e9cd1aeSAssar Westerlund 						old_pwd,
1335e9cd1aeSAssar Westerlund 						nop_prompter,
1345e9cd1aeSAssar Westerlund 						NULL,
1355e9cd1aeSAssar Westerlund 						0,
1365e9cd1aeSAssar Westerlund 						"kadmin/changepw",
137c19800e8SDoug Rabson 						opt);
1385e9cd1aeSAssar Westerlund 	}
1395e9cd1aeSAssar Westerlund 	if (ret)
1405e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_get_init_creds_password");
1415e9cd1aeSAssar Westerlund 
1425e9cd1aeSAssar Westerlund 	krb5_free_principal (context, principal);
1435e9cd1aeSAssar Westerlund 
144*ae771770SStanislav Sedov 
145*ae771770SStanislav Sedov 	ret = krb5_set_password (context,
146*ae771770SStanislav Sedov 				 &cred,
147*ae771770SStanislav Sedov 				 new_pwd,
148*ae771770SStanislav Sedov 				 NULL,
1495e9cd1aeSAssar Westerlund 				 &result_code,
1505e9cd1aeSAssar Westerlund 				 &result_code_string,
1515e9cd1aeSAssar Westerlund 				 &result_string);
1525e9cd1aeSAssar Westerlund 	if (ret)
1535e9cd1aeSAssar Westerlund 	    krb5_err (context, 1, ret, "krb5_change_password");
1545e9cd1aeSAssar Westerlund 
1555e9cd1aeSAssar Westerlund 	free (old_pwd);
1565e9cd1aeSAssar Westerlund 	free (new_pwd);
157c19800e8SDoug Rabson 	krb5_free_cred_contents (context, &cred);
158c19800e8SDoug Rabson 	krb5_get_init_creds_opt_free(context, opt);
1595e9cd1aeSAssar Westerlund     }
1605e9cd1aeSAssar Westerlund }
1615e9cd1aeSAssar Westerlund 
1625e9cd1aeSAssar Westerlund static int version_flag	= 0;
1635e9cd1aeSAssar Westerlund static int help_flag	= 0;
1645e9cd1aeSAssar Westerlund 
1655e9cd1aeSAssar Westerlund static struct getargs args[] = {
1665e9cd1aeSAssar Westerlund     { "version", 	0,   arg_flag, &version_flag },
1675e9cd1aeSAssar Westerlund     { "help",		0,   arg_flag, &help_flag }
1685e9cd1aeSAssar Westerlund };
1695e9cd1aeSAssar Westerlund 
1705e9cd1aeSAssar Westerlund static void
usage(int ret)1715e9cd1aeSAssar Westerlund usage (int ret)
1725e9cd1aeSAssar Westerlund {
1735e9cd1aeSAssar Westerlund     arg_printusage (args,
1745e9cd1aeSAssar Westerlund 		    sizeof(args)/sizeof(*args),
1755e9cd1aeSAssar Westerlund 		    NULL,
1765e9cd1aeSAssar Westerlund 		    "file [number]");
1775e9cd1aeSAssar Westerlund     exit (ret);
1785e9cd1aeSAssar Westerlund }
1795e9cd1aeSAssar Westerlund 
1805e9cd1aeSAssar Westerlund int
main(int argc,char ** argv)1815e9cd1aeSAssar Westerlund main(int argc, char **argv)
1825e9cd1aeSAssar Westerlund {
1835e9cd1aeSAssar Westerlund     int optind = 0;
1845e9cd1aeSAssar Westerlund     int nreq;
1855e9cd1aeSAssar Westerlund     char *end;
1865e9cd1aeSAssar Westerlund 
187adb0ddaeSAssar Westerlund     setprogname(argv[0]);
1885e9cd1aeSAssar Westerlund     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
1895e9cd1aeSAssar Westerlund 	usage(1);
1904137ff4cSJacques Vidrine     if (help_flag)
1914137ff4cSJacques Vidrine 	usage (0);
1924137ff4cSJacques Vidrine     if (version_flag) {
1934137ff4cSJacques Vidrine 	print_version(NULL);
1944137ff4cSJacques Vidrine 	return 0;
1954137ff4cSJacques Vidrine     }
1965e9cd1aeSAssar Westerlund     argc -= optind;
1975e9cd1aeSAssar Westerlund     argv += optind;
1985e9cd1aeSAssar Westerlund 
1995e9cd1aeSAssar Westerlund     if (argc != 2)
2005e9cd1aeSAssar Westerlund 	usage (1);
2015e9cd1aeSAssar Westerlund     srand (0);
2025e9cd1aeSAssar Westerlund     nreq = strtol (argv[1], &end, 0);
2035e9cd1aeSAssar Westerlund     if (argv[1] == end || *end != '\0')
2045e9cd1aeSAssar Westerlund 	usage (1);
2055e9cd1aeSAssar Westerlund     generate_requests (argv[0], nreq);
2065e9cd1aeSAssar Westerlund     return 0;
2075e9cd1aeSAssar Westerlund }
208