/* $Id: kinit.c,v 1.1 1995/12/14 06:52:51 tholo Exp $ */ /*- * Copyright 1987, 1988 by the Student Information Processing Board * of the Massachusetts Institute of Technology * * Permission to use, copy, modify, and distribute this software * and its documentation for any purpose and without fee is * hereby granted, provided that the above copyright notice * appear in all copies and that both that copyright notice and * this permission notice appear in supporting documentation, * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be * used in advertising or publicity pertaining to distribution * of the software without specific, written prior permission. * M.I.T. and the M.I.T. S.I.P.B. make no representations about * the suitability of this software for any purpose. It is * provided "as is" without express or implied warranty. */ /* * Routine to initialize user to Kerberos. Prompts optionally for * user, instance and realm. Authenticates user and gets a ticket * for the Kerberos ticket-granting service for future use. * * Options are: * * -i[instance] * -r[realm] * -v[erbose] * -l[ifetime] */ #include #include #define LIFE DEFAULT_TKT_LIFE /* lifetime of ticket in 5-minute units */ char *progname; static void get_input(s, size, stream) char *s; int size; FILE *stream; { char *p; if (fgets(s, size, stream) == NULL) exit(1); if ( (p = strchr(s, '\n')) != NULL) *p = '\0'; } static void usage() { fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname); exit(1); } int main(argc, argv) int argc; char *argv[]; { char aname[ANAME_SZ]; char inst[INST_SZ]; char realm[REALM_SZ]; char buf[MAXHOSTNAMELEN]; char *username = NULL; int iflag, rflag, vflag, lflag, lifetime, k_errno; register char *cp; register i; *inst = *realm = '\0'; iflag = rflag = vflag = lflag = 0; lifetime = LIFE; progname = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv; while (--argc) { if ((*++argv)[0] != '-') { if (username) usage(); username = *argv; continue; } for (i = 1; (*argv)[i] != '\0'; i++) switch ((*argv)[i]) { case 'i': /* Instance */ ++iflag; continue; case 'r': /* Realm */ ++rflag; continue; case 'v': /* Verbose */ ++vflag; continue; case 'l': ++lflag; continue; default: usage(); exit(1); } } if (username && (k_errno = kname_parse(aname, inst, realm, username)) != KSUCCESS) { fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]); iflag = rflag = 1; username = NULL; } if (gethostname(buf, MAXHOSTNAMELEN)) { fprintf(stderr, "%s: gethostname failed\n", progname); exit(1); } printf("%s (%s)\n", ORGANIZATION, buf); if (username) { printf("Kerberos Initialization for \"%s", aname); if (*inst) printf(".%s", inst); if (*realm) printf("@%s", realm); printf("\"\n"); } else { printf("Kerberos Initialization\n"); printf("Kerberos name: "); get_input(aname, sizeof(aname), stdin); if (!*aname) exit(0); if (!k_isname(aname)) { fprintf(stderr, "%s: bad Kerberos name format\n", progname); exit(1); } } /* optional instance */ if (iflag) { printf("Kerberos instance: "); get_input(inst, sizeof(inst), stdin); if (!k_isinst(inst)) { fprintf(stderr, "%s: bad Kerberos instance format\n", progname); exit(1); } } if (rflag) { printf("Kerberos realm: "); get_input(realm, sizeof(realm), stdin); if (!k_isrealm(realm)) { fprintf(stderr, "%s: bad Kerberos realm format\n", progname); exit(1); } } if (lflag) { printf("Kerberos ticket lifetime (minutes): "); get_input(buf, sizeof(buf), stdin); lifetime = atoi(buf); if (lifetime < 5) lifetime = 1; else lifetime /= krb_time_to_life(0, lifetime*60); /* This should be changed if the maximum ticket lifetime */ /* changes */ if (lifetime > 255) lifetime = 255; } if (!*realm && krb_get_lrealm(realm, 1)) { fprintf(stderr, "%s: krb_get_lrealm failed\n", progname); exit(1); } k_errno = krb_get_pw_in_tkt(aname, inst, realm, "krbtgt", realm, lifetime, 0); if (vflag) { printf("Kerberos realm %s:\n", realm); printf("%s\n", krb_err_txt[k_errno]); } else if (k_errno) { fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]); exit(1); } exit(0); }