xref: /original-bsd/old/athena/kinit/kinit.c (revision ea3a8ee8)
1 /*
2  * $Source: /mit/kerberos/src/kuser/RCS/kinit.c,v $
3  * $Author: jtkohl $
4  *
5  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
6  *
7  * For copying and distribution information, please see the file
8  * <mit-copyright.h>.
9  *
10  * Routine to initialize user to Kerberos.  Prompts optionally for
11  * user, instance and realm.  Authenticates user and gets a ticket
12  * for the Kerberos ticket-granting service for future use.
13  *
14  * Options are:
15  *
16  *   -i[instance]
17  *   -r[realm]
18  *   -v[erbose]
19  *   -l[ifetime]
20  */
21 
22 #ifndef	lint
23 static char rcsid_kinit_c[] =
24 "$Header: kinit.c,v 4.11 89/01/23 09:34:49 jtkohl Exp $";
25 #endif	lint
26 
27 #include <kerberos/mit-copyright.h>
28 #include <stdio.h>
29 #include <pwd.h>
30 #include <kerberos/krb.h>
31 
32 #include <strings.h>
33 #include <sys/param.h>
34 
35 #define	LEN		MAXHOSTNAMELEN
36 #define	LIFE		96 	/* tick lifetime in 5-min units<8hrs> */
37 #define	MAX_LIFE	255	/* maximum life in 5-min units */
38 
39 char   *progname;
40 
41 main(argc, argv)
42     char   *argv[];
43 {
44     char    aname[ANAME_SZ];
45     char    inst[INST_SZ];
46     char    realm[REALM_SZ];
47     char    buf[LEN];
48     char   *username = NULL;
49     int     iflag, rflag, vflag, lflag, lifetime, k_errno;
50     register char *cp;
51     register i;
52     extern int krb_debug;
53     krb_debug = 1;
54 
55     *inst = *realm = '\0';
56     iflag = rflag = vflag = lflag = 0;
57     lifetime = LIFE;
58     progname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
59 
60     while (--argc) {
61 	if ((*++argv)[0] != '-') {
62 	    if (username)
63 		usage();
64 	    username = *argv;
65 	    continue;
66 	}
67 	for (i = 1; (*argv)[i] != '\0'; i++)
68 	    switch ((*argv)[i]) {
69 	    case 'i':		/* Instance */
70 		++iflag;
71 		continue;
72 	    case 'r':		/* Realm */
73 		++rflag;
74 		continue;
75 	    case 'v':		/* Verbose */
76 		++vflag;
77 		continue;
78 	    case 'l':
79 		++lflag;
80 		continue;
81 	    default:
82 		usage();
83 		exit(1);
84 	    }
85     }
86     if (username &&
87 	(k_errno = kname_parse(aname, inst, realm, username))
88 	!= KSUCCESS) {
89 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
90 	iflag = rflag = 1;
91 	username = NULL;
92     }
93     if (k_gethostname(buf, LEN)) {
94 	fprintf(stderr, "%s: k_gethostname failed\n", progname);
95 	exit(1);
96     }
97     printf("MIT Project Athena/UC Berkeley (%s)\n", buf);
98     if (username) {
99 	printf("Kerberos Initialization for \"%s", aname);
100 	if (*inst)
101 	    printf(".%s", inst);
102 	if (*realm)
103 	    printf("@%s", realm);
104 	printf("\"\n");
105     } else {
106 	printf("Kerberos Initialization\n");
107 	printf("Kerberos name: ");
108 	getstr(aname, ANAME_SZ);
109 	if (!*aname)
110 	    exit(0);
111 	if (!k_isname(aname)) {
112 	    fprintf(stderr, "%s: bad Kerberos name format\n",
113 		    progname);
114 	    exit(1);
115 	}
116     }
117     /* optional instance */
118     if (iflag) {
119 	printf("Kerberos instance: ");
120 	getstr(inst, INST_SZ);
121 	if (!k_isinst(inst)) {
122 	    fprintf(stderr, "%s: bad Kerberos instance format\n",
123 		    progname);
124 	    exit(1);
125 	}
126     }
127     if (rflag) {
128 	printf("Kerberos realm: ");
129 	getstr(realm, REALM_SZ);
130 	if (!k_isrealm(realm)) {
131 	    fprintf(stderr, "%s: bad Kerberos realm format\n",
132 		    progname);
133 	    exit(1);
134 	}
135     }
136     if (lflag) {
137 	 printf("Kerberos ticket lifetime (minutes): ");
138 	 getstr(buf, LEN);
139 	 lifetime = atoi(buf);
140 	 if (lifetime < 5)
141 	      lifetime = 1;
142 	 else
143 	      lifetime /= 5;
144 	 /* This should be changed if the maximum ticket lifetime */
145 	 /* changes */
146 	 if (lifetime > MAX_LIFE)
147 	      lifetime = MAX_LIFE;
148     }
149     if (!*realm && krb_get_lrealm(realm, 1)) {
150 	fprintf(stderr, "%s: krb_get_lrealm failed\n", progname);
151 	exit(1);
152     }
153     printf("Getting initial ticket for %s.%s@%s\n",
154 	aname, inst, realm);
155     k_errno = krb_get_pw_in_tkt(aname, inst, realm, "krbtgt", realm,
156 				lifetime, 0);
157     if (vflag) {
158 	printf("Kerberos realm %s:\n", realm);
159 	printf("%s\n", krb_err_txt[k_errno]);
160     } else if (k_errno) {
161 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
162 	exit(1);
163     }
164 }
165 
166 usage()
167 {
168     fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname);
169     exit(1);
170 }
171 
172 getstr(p, len)
173 	register char	*p;
174 	int		len;
175 {
176 	while(((*p++ = getchar()) != '\n') && --len)
177 		;
178 	*--p = '\0';
179 }
180