xref: /csrg-svn/old/athena/kinit/kinit.c (revision 55382)
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 
main(argc,argv)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;
52*55382Sbostic     extern int krb_debug;
53*55382Sbostic     krb_debug = 1;
5436654Skfall 
5536654Skfall     *inst = *realm = '\0';
5636654Skfall     iflag = rflag = vflag = lflag = 0;
5736654Skfall     lifetime = LIFE;
5836654Skfall     progname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
5936654Skfall 
6036654Skfall     while (--argc) {
6136654Skfall 	if ((*++argv)[0] != '-') {
6236654Skfall 	    if (username)
6336654Skfall 		usage();
6436654Skfall 	    username = *argv;
6536654Skfall 	    continue;
6636654Skfall 	}
6736654Skfall 	for (i = 1; (*argv)[i] != '\0'; i++)
6836654Skfall 	    switch ((*argv)[i]) {
6936654Skfall 	    case 'i':		/* Instance */
7036654Skfall 		++iflag;
7136654Skfall 		continue;
7236654Skfall 	    case 'r':		/* Realm */
7336654Skfall 		++rflag;
7436654Skfall 		continue;
7536654Skfall 	    case 'v':		/* Verbose */
7636654Skfall 		++vflag;
7736654Skfall 		continue;
7836654Skfall 	    case 'l':
7936654Skfall 		++lflag;
8036654Skfall 		continue;
8136654Skfall 	    default:
8236654Skfall 		usage();
8336654Skfall 		exit(1);
8436654Skfall 	    }
8536654Skfall     }
8636654Skfall     if (username &&
8736654Skfall 	(k_errno = kname_parse(aname, inst, realm, username))
8836654Skfall 	!= KSUCCESS) {
8936654Skfall 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
9036654Skfall 	iflag = rflag = 1;
9136654Skfall 	username = NULL;
9236654Skfall     }
9336654Skfall     if (k_gethostname(buf, LEN)) {
9436654Skfall 	fprintf(stderr, "%s: k_gethostname failed\n", progname);
9536654Skfall 	exit(1);
9636654Skfall     }
9736664Skfall     printf("MIT Project Athena/UC Berkeley (%s)\n", buf);
9836654Skfall     if (username) {
9936654Skfall 	printf("Kerberos Initialization for \"%s", aname);
10036654Skfall 	if (*inst)
10136654Skfall 	    printf(".%s", inst);
10236654Skfall 	if (*realm)
10336654Skfall 	    printf("@%s", realm);
10436654Skfall 	printf("\"\n");
10536654Skfall     } else {
10636654Skfall 	printf("Kerberos Initialization\n");
10736654Skfall 	printf("Kerberos name: ");
10836664Skfall 	getstr(aname, ANAME_SZ);
10936654Skfall 	if (!*aname)
11036654Skfall 	    exit(0);
11136654Skfall 	if (!k_isname(aname)) {
11236654Skfall 	    fprintf(stderr, "%s: bad Kerberos name format\n",
11336654Skfall 		    progname);
11436654Skfall 	    exit(1);
11536654Skfall 	}
11636654Skfall     }
11736654Skfall     /* optional instance */
11836654Skfall     if (iflag) {
11936654Skfall 	printf("Kerberos instance: ");
12036664Skfall 	getstr(inst, INST_SZ);
12136654Skfall 	if (!k_isinst(inst)) {
12236654Skfall 	    fprintf(stderr, "%s: bad Kerberos instance format\n",
12336654Skfall 		    progname);
12436654Skfall 	    exit(1);
12536654Skfall 	}
12636654Skfall     }
12736654Skfall     if (rflag) {
12836654Skfall 	printf("Kerberos realm: ");
12938040Skfall 	getstr(realm, REALM_SZ);
13036654Skfall 	if (!k_isrealm(realm)) {
13136654Skfall 	    fprintf(stderr, "%s: bad Kerberos realm format\n",
13236654Skfall 		    progname);
13336654Skfall 	    exit(1);
13436654Skfall 	}
13536654Skfall     }
13636654Skfall     if (lflag) {
13736654Skfall 	 printf("Kerberos ticket lifetime (minutes): ");
13836664Skfall 	 getstr(buf, LEN);
13936654Skfall 	 lifetime = atoi(buf);
14036654Skfall 	 if (lifetime < 5)
14136654Skfall 	      lifetime = 1;
14236654Skfall 	 else
14336654Skfall 	      lifetime /= 5;
14436654Skfall 	 /* This should be changed if the maximum ticket lifetime */
14536654Skfall 	 /* changes */
14636664Skfall 	 if (lifetime > MAX_LIFE)
14736664Skfall 	      lifetime = MAX_LIFE;
14836654Skfall     }
14936654Skfall     if (!*realm && krb_get_lrealm(realm, 1)) {
15036654Skfall 	fprintf(stderr, "%s: krb_get_lrealm failed\n", progname);
15136654Skfall 	exit(1);
15236654Skfall     }
15336664Skfall     printf("Getting initial ticket for %s.%s@%s\n",
15436664Skfall 	aname, inst, realm);
15536654Skfall     k_errno = krb_get_pw_in_tkt(aname, inst, realm, "krbtgt", realm,
15636654Skfall 				lifetime, 0);
15736654Skfall     if (vflag) {
15836654Skfall 	printf("Kerberos realm %s:\n", realm);
15936654Skfall 	printf("%s\n", krb_err_txt[k_errno]);
16036654Skfall     } else if (k_errno) {
16136654Skfall 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
16236654Skfall 	exit(1);
16336654Skfall     }
16436654Skfall }
16536654Skfall 
usage()16636654Skfall usage()
16736654Skfall {
16836654Skfall     fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname);
16936654Skfall     exit(1);
17036654Skfall }
17136664Skfall 
getstr(p,len)17236664Skfall getstr(p, len)
17336664Skfall 	register char	*p;
17436664Skfall 	int		len;
17536664Skfall {
17636664Skfall 	while(((*p++ = getchar()) != '\n') && --len)
17736664Skfall 		;
17836664Skfall 	*--p = '\0';
17936664Skfall }
180