Path: blob/main/crypto/heimdal/kpasswd/kpasswd-generator.c
34860 views
/*1* Copyright (c) 2000 - 2004 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 "kpasswd_locl.h"3435RCSID("$Id$");3637static unsigned38read_words (const char *filename, char ***ret_w)39{40unsigned n, alloc;41FILE *f;42char buf[256];43char **w = NULL;4445f = fopen (filename, "r");46if (f == NULL)47err (1, "cannot open %s", filename);48alloc = n = 0;49while (fgets (buf, sizeof(buf), f) != NULL) {50buf[strcspn(buf, "\r\n")] = '\0';51if (n >= alloc) {52alloc += 16;53w = erealloc (w, alloc * sizeof(char **));54}55w[n++] = estrdup (buf);56}57*ret_w = w;58if (n == 0)59errx(1, "%s is an empty file, no words to try", filename);60fclose(f);61return n;62}6364static int65nop_prompter (krb5_context context,66void *data,67const char *name,68const char *banner,69int num_prompts,70krb5_prompt prompts[])71{72return 0;73}7475static void76generate_requests (const char *filename, unsigned nreq)77{78krb5_context context;79krb5_error_code ret;80int i;81char **words;82unsigned nwords;8384ret = krb5_init_context (&context);85if (ret)86errx (1, "krb5_init_context failed: %d", ret);8788nwords = read_words (filename, &words);8990for (i = 0; i < nreq; ++i) {91char *name = words[rand() % nwords];92krb5_get_init_creds_opt *opt;93krb5_creds cred;94krb5_principal principal;95int result_code;96krb5_data result_code_string, result_string;97char *old_pwd, *new_pwd;9899krb5_get_init_creds_opt_alloc (context, &opt);100krb5_get_init_creds_opt_set_tkt_life (opt, 300);101krb5_get_init_creds_opt_set_forwardable (opt, FALSE);102krb5_get_init_creds_opt_set_proxiable (opt, FALSE);103104ret = krb5_parse_name (context, name, &principal);105if (ret)106krb5_err (context, 1, ret, "krb5_parse_name %s", name);107108asprintf (&old_pwd, "%s", name);109asprintf (&new_pwd, "%s2", name);110111ret = krb5_get_init_creds_password (context,112&cred,113principal,114old_pwd,115nop_prompter,116NULL,1170,118"kadmin/changepw",119opt);120if( ret == KRB5KRB_AP_ERR_BAD_INTEGRITY121|| ret == KRB5KRB_AP_ERR_MODIFIED) {122char *tmp;123124tmp = new_pwd;125new_pwd = old_pwd;126old_pwd = tmp;127128ret = krb5_get_init_creds_password (context,129&cred,130principal,131old_pwd,132nop_prompter,133NULL,1340,135"kadmin/changepw",136opt);137}138if (ret)139krb5_err (context, 1, ret, "krb5_get_init_creds_password");140141krb5_free_principal (context, principal);142143144ret = krb5_set_password (context,145&cred,146new_pwd,147NULL,148&result_code,149&result_code_string,150&result_string);151if (ret)152krb5_err (context, 1, ret, "krb5_change_password");153154free (old_pwd);155free (new_pwd);156krb5_free_cred_contents (context, &cred);157krb5_get_init_creds_opt_free(context, opt);158}159}160161static int version_flag = 0;162static int help_flag = 0;163164static struct getargs args[] = {165{ "version", 0, arg_flag, &version_flag },166{ "help", 0, arg_flag, &help_flag }167};168169static void170usage (int ret)171{172arg_printusage (args,173sizeof(args)/sizeof(*args),174NULL,175"file [number]");176exit (ret);177}178179int180main(int argc, char **argv)181{182int optind = 0;183int nreq;184char *end;185186setprogname(argv[0]);187if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))188usage(1);189if (help_flag)190usage (0);191if (version_flag) {192print_version(NULL);193return 0;194}195argc -= optind;196argv += optind;197198if (argc != 2)199usage (1);200srand (0);201nreq = strtol (argv[1], &end, 0);202if (argv[1] == end || *end != '\0')203usage (1);204generate_requests (argv[0], nreq);205return 0;206}207208209