136654Skfall /* 236654Skfall * $Source: /mit/kerberos/src/kuser/RCS/kinit.c,v $ 336654Skfall * $Author: jtkohl $ 436654Skfall * 536654Skfall * Copyright 1987, 1988 by the Massachusetts Institute of Technology. 636654Skfall * 736654Skfall * For copying and distribution information, please see the file 836654Skfall * <mit-copyright.h>. 936654Skfall * 1036654Skfall * Routine to initialize user to Kerberos. Prompts optionally for 1136654Skfall * user, instance and realm. Authenticates user and gets a ticket 1236654Skfall * for the Kerberos ticket-granting service for future use. 1336654Skfall * 1436654Skfall * Options are: 1536654Skfall * 1636654Skfall * -i[instance] 1736654Skfall * -r[realm] 1836654Skfall * -v[erbose] 1936654Skfall * -l[ifetime] 2036654Skfall */ 2136654Skfall 2236654Skfall #ifndef lint 2336654Skfall static char rcsid_kinit_c[] = 2436654Skfall "$Header: kinit.c,v 4.11 89/01/23 09:34:49 jtkohl Exp $"; 2536654Skfall #endif lint 2636654Skfall 2736664Skfall #include <kerberos/mit-copyright.h> 2836654Skfall #include <stdio.h> 2936654Skfall #include <pwd.h> 3036664Skfall #include <kerberos/krb.h> 3136654Skfall 3236654Skfall #include <strings.h> 3336654Skfall #include <sys/param.h> 3436654Skfall 3536664Skfall #define LEN MAXHOSTNAMELEN 3636664Skfall #define LIFE 96 /* tick lifetime in 5-min units<8hrs> */ 3736664Skfall #define MAX_LIFE 255 /* maximum life in 5-min units */ 3836654Skfall 3936654Skfall char *progname; 4036654Skfall 4136654Skfall main(argc, argv) 4236654Skfall char *argv[]; 4336654Skfall { 4436654Skfall char aname[ANAME_SZ]; 4536654Skfall char inst[INST_SZ]; 4636654Skfall char realm[REALM_SZ]; 4736654Skfall char buf[LEN]; 4836654Skfall char *username = NULL; 4936654Skfall int iflag, rflag, vflag, lflag, lifetime, k_errno; 5036654Skfall register char *cp; 5136654Skfall register i; 5236654Skfall 5336654Skfall *inst = *realm = '\0'; 5436654Skfall iflag = rflag = vflag = lflag = 0; 5536654Skfall lifetime = LIFE; 5636654Skfall progname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv; 5736654Skfall 5836654Skfall while (--argc) { 5936654Skfall if ((*++argv)[0] != '-') { 6036654Skfall if (username) 6136654Skfall usage(); 6236654Skfall username = *argv; 6336654Skfall continue; 6436654Skfall } 6536654Skfall for (i = 1; (*argv)[i] != '\0'; i++) 6636654Skfall switch ((*argv)[i]) { 6736654Skfall case 'i': /* Instance */ 6836654Skfall ++iflag; 6936654Skfall continue; 7036654Skfall case 'r': /* Realm */ 7136654Skfall ++rflag; 7236654Skfall continue; 7336654Skfall case 'v': /* Verbose */ 7436654Skfall ++vflag; 7536654Skfall continue; 7636654Skfall case 'l': 7736654Skfall ++lflag; 7836654Skfall continue; 7936654Skfall default: 8036654Skfall usage(); 8136654Skfall exit(1); 8236654Skfall } 8336654Skfall } 8436654Skfall if (username && 8536654Skfall (k_errno = kname_parse(aname, inst, realm, username)) 8636654Skfall != KSUCCESS) { 8736654Skfall fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]); 8836654Skfall iflag = rflag = 1; 8936654Skfall username = NULL; 9036654Skfall } 9136654Skfall if (k_gethostname(buf, LEN)) { 9236654Skfall fprintf(stderr, "%s: k_gethostname failed\n", progname); 9336654Skfall exit(1); 9436654Skfall } 9536664Skfall printf("MIT Project Athena/UC Berkeley (%s)\n", buf); 9636654Skfall if (username) { 9736654Skfall printf("Kerberos Initialization for \"%s", aname); 9836654Skfall if (*inst) 9936654Skfall printf(".%s", inst); 10036654Skfall if (*realm) 10136654Skfall printf("@%s", realm); 10236654Skfall printf("\"\n"); 10336654Skfall } else { 10436654Skfall printf("Kerberos Initialization\n"); 10536654Skfall printf("Kerberos name: "); 10636664Skfall getstr(aname, ANAME_SZ); 10736654Skfall if (!*aname) 10836654Skfall exit(0); 10936654Skfall if (!k_isname(aname)) { 11036654Skfall fprintf(stderr, "%s: bad Kerberos name format\n", 11136654Skfall progname); 11236654Skfall exit(1); 11336654Skfall } 11436654Skfall } 11536654Skfall /* optional instance */ 11636654Skfall if (iflag) { 11736654Skfall printf("Kerberos instance: "); 11836664Skfall getstr(inst, INST_SZ); 11936654Skfall if (!k_isinst(inst)) { 12036654Skfall fprintf(stderr, "%s: bad Kerberos instance format\n", 12136654Skfall progname); 12236654Skfall exit(1); 12336654Skfall } 12436654Skfall } 12536654Skfall if (rflag) { 12636654Skfall printf("Kerberos realm: "); 127*38040Skfall getstr(realm, REALM_SZ); 12836654Skfall if (!k_isrealm(realm)) { 12936654Skfall fprintf(stderr, "%s: bad Kerberos realm format\n", 13036654Skfall progname); 13136654Skfall exit(1); 13236654Skfall } 13336654Skfall } 13436654Skfall if (lflag) { 13536654Skfall printf("Kerberos ticket lifetime (minutes): "); 13636664Skfall getstr(buf, LEN); 13736654Skfall lifetime = atoi(buf); 13836654Skfall if (lifetime < 5) 13936654Skfall lifetime = 1; 14036654Skfall else 14136654Skfall lifetime /= 5; 14236654Skfall /* This should be changed if the maximum ticket lifetime */ 14336654Skfall /* changes */ 14436664Skfall if (lifetime > MAX_LIFE) 14536664Skfall lifetime = MAX_LIFE; 14636654Skfall } 14736654Skfall if (!*realm && krb_get_lrealm(realm, 1)) { 14836654Skfall fprintf(stderr, "%s: krb_get_lrealm failed\n", progname); 14936654Skfall exit(1); 15036654Skfall } 15136664Skfall printf("Getting initial ticket for %s.%s@%s\n", 15236664Skfall aname, inst, realm); 15336654Skfall k_errno = krb_get_pw_in_tkt(aname, inst, realm, "krbtgt", realm, 15436654Skfall lifetime, 0); 15536654Skfall if (vflag) { 15636654Skfall printf("Kerberos realm %s:\n", realm); 15736654Skfall printf("%s\n", krb_err_txt[k_errno]); 15836654Skfall } else if (k_errno) { 15936654Skfall fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]); 16036654Skfall exit(1); 16136654Skfall } 16236654Skfall } 16336654Skfall 16436654Skfall usage() 16536654Skfall { 16636654Skfall fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname); 16736654Skfall exit(1); 16836654Skfall } 16936664Skfall 17036664Skfall getstr(p, len) 17136664Skfall register char *p; 17236664Skfall int len; 17336664Skfall { 17436664Skfall while(((*p++ = getchar()) != '\n') && --len) 17536664Skfall ; 17636664Skfall *--p = '\0'; 17736664Skfall } 178