xref: /original-bsd/local/kerberosIV/kinit/kinit.c (revision 51dd4909)
1 /*
2  * $Source: /usr/src/kerberosIV/kinit/RCS/kinit.c,v $
3  * $Author: kfall $
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: /usr/src/kerberosIV/kinit/RCS/kinit.c,v 4.15 90/06/25 21:01:06 kfall Exp $";
25 #endif	lint
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <mit-copyright.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <des.h>
33 #include <krb.h>
34 #include <pwd.h>
35 #include <paths.h>
36 
37 #define	LEN		MAXHOSTNAMELEN
38 #define	LIFE		DEFAULT_TKT_LIFE /* in 5-minute units */
39 #define	INITIAL_TICKET	"krbtgt"
40 
41 char	*progname;
42 
43 char    aname[ANAME_SZ];
44 char    inst[INST_SZ];
45 char    realm[REALM_SZ];
46 
47 #define	gets(buf) _gets(buf, sizeof(buf))	/* hack */
48 
49 char *
50 _gets(p, n)
51 	char *p;
52 	int n;
53 {
54 	char *rv, *fgets();
55 
56 	if ((rv = fgets(p, n, stdin)) == NULL)
57 		return (rv);
58 	if (p = index(p, '\n'))
59 		*p = '\0';
60 	return (rv);
61 }
62 
63 main(argc, argv)
64     char   *argv[];
65 {
66     char    buf[LEN];
67     char   *username = NULL;
68     int     iflag, rflag, vflag, lflag, lifetime, k_errno;
69     register char *cp;
70     register i;
71 
72     *inst = *realm = '\0';
73     iflag = rflag = vflag = lflag = 0;
74     lifetime = LIFE;
75     progname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
76 
77     while (--argc) {
78 	if ((*++argv)[0] != '-') {
79 	    if (username)
80 		usage();
81 	    username = *argv;
82 	    continue;
83 	}
84 	for (i = 1; (*argv)[i] != '\0'; i++)
85 	    switch ((*argv)[i]) {
86 	    case 'i':		/* Instance */
87 		++iflag;
88 		continue;
89 	    case 'r':		/* Realm */
90 		++rflag;
91 		continue;
92 	    case 'v':		/* Verbose */
93 		++vflag;
94 		continue;
95 	    case 'l':
96 		++lflag;
97 		continue;
98 	    default:
99 		usage();
100 		exit(1);
101 	    }
102     }
103     if (username && (k_errno = kname_parse(aname, inst, realm, username)) !=
104 	KSUCCESS) {
105 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
106 	iflag = rflag = 1;
107 	username = NULL;
108     }
109     if (k_gethostname(buf, LEN)) {
110 	fprintf(stderr, "%s: k_gethostname failed\n", progname);
111 	exit(1);
112     }
113 
114     if (username) {
115 	printf("Kerberos Initialization for \"%s", aname);
116 	if (*inst)
117 	    printf(".%s", inst);
118 	if (*realm)
119 	    printf("@%s", realm);
120 	printf("\"\n");
121     } else {
122 	if (iflag) {
123 		printf("Kerberos Initialization\n");
124 		printf("Kerberos name: ");
125 		gets(aname);
126 	} else {
127 		int uid = getuid();
128 		char *getenv();
129 		struct passwd *pwd;
130 
131 		/* default to current user name unless running as root */
132 		if (uid == 0 && (username = getenv("USER")) &&
133 		    strcmp(username, "root") != 0) {
134 			strncpy(aname, username, sizeof(aname));
135 			strncpy(inst, "root", sizeof(inst));
136 		} else {
137 			pwd = getpwuid(uid);
138 
139 			if (pwd == (struct passwd *) NULL) {
140 				fprintf(stderr, "Unknown name for your uid\n");
141 				printf("Kerberos name: ");
142 				gets(aname);
143 			} else
144 				strncpy(aname, pwd->pw_name, sizeof(aname));
145 		}
146 	}
147 
148 	if (!*aname)
149 	    exit(0);
150 	if (!k_isname(aname)) {
151 	    fprintf(stderr, "%s: bad Kerberos name format\n",
152 		    progname);
153 	    exit(1);
154 	}
155     }
156     /* optional instance */
157     if (iflag) {
158 	printf("Kerberos instance: ");
159 	gets(inst);
160 	if (!k_isinst(inst)) {
161 	    fprintf(stderr, "%s: bad Kerberos instance format\n",
162 		    progname);
163 	    exit(1);
164 	}
165     }
166     if (rflag) {
167 	printf("Kerberos realm: ");
168 	gets(realm);
169 	if (!k_isrealm(realm)) {
170 	    fprintf(stderr, "%s: bad Kerberos realm format\n",
171 		    progname);
172 	    exit(1);
173 	}
174     }
175     if (lflag) {
176 	 printf("Kerberos ticket lifetime (minutes): ");
177 	 gets(buf);
178 	 lifetime = atoi(buf);
179 	 if (lifetime < 5)
180 	      lifetime = 1;
181 	 else
182 	      lifetime /= 5;
183 	 /* This should be changed if the maximum ticket lifetime */
184 	 /* changes */
185 	 if (lifetime > 255)
186 	      lifetime = 255;
187     }
188     if (!*realm && krb_get_lrealm(realm, 1)) {
189 	fprintf(stderr, "%s: krb_get_lrealm failed\n", progname);
190 	exit(1);
191     }
192 
193     k_errno = krb_get_pw_in_tkt(aname, inst, realm, INITIAL_TICKET,
194 		realm, lifetime, 0);
195 
196     if (vflag) {
197 	printf("Kerberos realm %s:\n", realm);
198 	printf("%s\n", krb_err_txt[k_errno]);
199     } else if (k_errno) {
200 	fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
201 	exit(1);
202     }
203     exit(0);
204 }
205 
206 usage()
207 {
208     fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname);
209     exit(1);
210 }
211