1 | /* Copyright 1989,1999 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | /* rkinit: a remote kinit client */ |
---|
17 | |
---|
18 | static const char rcsid[] = "$Id: rkinit.c,v 1.3 2000-02-26 23:10:26 ghudson Exp $"; |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <string.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <netdb.h> |
---|
24 | #include <pwd.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <com_err.h> |
---|
28 | #include <krb.h> |
---|
29 | #include <des.h> |
---|
30 | |
---|
31 | #include <rkinit.h> |
---|
32 | #include <rkinit_err.h> |
---|
33 | |
---|
34 | #ifndef TRUE |
---|
35 | #define TRUE 1 |
---|
36 | #endif |
---|
37 | |
---|
38 | #ifndef FALSE |
---|
39 | #define FALSE 0 |
---|
40 | #endif |
---|
41 | |
---|
42 | static void usage(void) |
---|
43 | { |
---|
44 | fprintf(stderr,"Usage: rkinit [host] options\n"); |
---|
45 | fprintf(stderr, |
---|
46 | "Options: [-l username] [-k krb_realm] [-p principal] [-f tktfile]\n"); |
---|
47 | fprintf(stderr, " [-t lifetime] [-h host] [-notimeout]\n"); |
---|
48 | fprintf(stderr, "A host must be specified either with the -h option "); |
---|
49 | fprintf(stderr, "or as the first argument.\n"); |
---|
50 | |
---|
51 | exit(1); |
---|
52 | } |
---|
53 | |
---|
54 | int main(int argc, char *argv[]) |
---|
55 | { |
---|
56 | char *whoami; /* Name of this program */ |
---|
57 | |
---|
58 | char principal[MAX_K_NAME_SZ]; /* Principal for which to get tickets */ |
---|
59 | char *host = NULL; /* Remote host */ |
---|
60 | char *username = 0; /* Username of owner of ticket */ |
---|
61 | char r_krealm[REALM_SZ]; /* Kerberos realm of remote host */ |
---|
62 | char aname[ANAME_SZ]; /* Aname of remote ticket file */ |
---|
63 | char inst[INST_SZ]; /* Instance of remote ticket file */ |
---|
64 | char realm[REALM_SZ]; /* Realm of remote ticket file */ |
---|
65 | char *tktfilename = NULL; /* Name of ticket file on remote host */ |
---|
66 | u_long lifetime = DEFAULT_TKT_LIFE; /* Lifetime of remote tickets */ |
---|
67 | int timeout = TRUE; /* Should we time out? */ |
---|
68 | rkinit_info info; /* Information needed by rkinit */ |
---|
69 | |
---|
70 | struct passwd *localid; /* To determine local id */ |
---|
71 | |
---|
72 | int status = 0; /* general error number */ |
---|
73 | |
---|
74 | int i; |
---|
75 | |
---|
76 | memset(principal, 0, sizeof(principal)); |
---|
77 | memset(aname, 0, sizeof(aname)); |
---|
78 | memset(inst, 0, sizeof(inst)); |
---|
79 | memset(realm, 0, sizeof(realm)); |
---|
80 | memset(r_krealm, 0, sizeof(r_krealm)); |
---|
81 | /* Parse commandline arguements. */ |
---|
82 | whoami = strrchr(argv[0], '/'); |
---|
83 | if (whoami) |
---|
84 | whoami++; |
---|
85 | else |
---|
86 | whoami = argv[0]; |
---|
87 | |
---|
88 | if (argc < 2) usage(); |
---|
89 | |
---|
90 | if (argv[1][0] != '-') { |
---|
91 | host = argv[1]; |
---|
92 | i = 2; |
---|
93 | } |
---|
94 | else |
---|
95 | i = 1; |
---|
96 | |
---|
97 | for (/* i initialized above */; i < argc; i++) { |
---|
98 | if (strcmp(argv[i], "-h") == 0) { |
---|
99 | if (++i >= argc) |
---|
100 | usage(); |
---|
101 | else |
---|
102 | host = argv[i]; |
---|
103 | } |
---|
104 | else if (strcmp(argv[i], "-l") == 0) { |
---|
105 | if (++i >= argc) |
---|
106 | usage(); |
---|
107 | else |
---|
108 | username = argv[i]; |
---|
109 | } |
---|
110 | else if (strcmp(argv[i], "-k") == 0) { |
---|
111 | if (++i >= argc) |
---|
112 | usage(); |
---|
113 | else |
---|
114 | strncpy(r_krealm, argv[i], sizeof(r_krealm) - 1); |
---|
115 | } |
---|
116 | else if (strcmp(argv[i], "-p") == 0) { |
---|
117 | if (++i >= argc) |
---|
118 | usage(); |
---|
119 | else |
---|
120 | strncpy(principal, argv[i], sizeof(principal) - 1); |
---|
121 | } |
---|
122 | else if (strcmp(argv[i], "-f") == 0) { |
---|
123 | if (++i >= argc) |
---|
124 | usage(); |
---|
125 | else |
---|
126 | tktfilename = argv[i]; |
---|
127 | } |
---|
128 | else if (strcmp(argv[i], "-t") == 0) { |
---|
129 | if (++i >= argc) |
---|
130 | usage(); |
---|
131 | else { |
---|
132 | lifetime = atoi(argv[i])/5; |
---|
133 | if (lifetime == 0) |
---|
134 | lifetime = 1; |
---|
135 | else if (lifetime > 255) |
---|
136 | lifetime = 255; |
---|
137 | } |
---|
138 | } |
---|
139 | else if (strcmp(argv[i], "-notimeout") == 0) |
---|
140 | timeout = FALSE; |
---|
141 | else |
---|
142 | usage(); |
---|
143 | } |
---|
144 | |
---|
145 | if (host == NULL) |
---|
146 | usage(); |
---|
147 | |
---|
148 | /* Initialize the realm of the remote host if necessary */ |
---|
149 | if (r_krealm[0] == 0) { |
---|
150 | /* |
---|
151 | * Try to figure out the realm of the remote host. If the |
---|
152 | * remote host is unknown, don't worry about it; the library |
---|
153 | * will handle the error better and print a good error message. |
---|
154 | */ |
---|
155 | struct hostent *hp; |
---|
156 | hp = gethostbyname(host); |
---|
157 | if (hp) |
---|
158 | strcpy(r_krealm, krb_realmofhost(hp->h_name)); |
---|
159 | } |
---|
160 | |
---|
161 | /* If no username was specified, use local id on client host */ |
---|
162 | if (username == 0) { |
---|
163 | localid = getpwuid(getuid()); |
---|
164 | if (localid == 0) { |
---|
165 | fprintf(stderr, "You can not be found in the password file.\n"); |
---|
166 | exit(1); |
---|
167 | } |
---|
168 | username = localid->pw_name; |
---|
169 | } |
---|
170 | |
---|
171 | /* Find out who will go in the ticket file */ |
---|
172 | if (! principal[0]) { |
---|
173 | status = krb_get_tf_fullname(TKT_FILE, aname, inst, realm); |
---|
174 | if (status != KSUCCESS) { |
---|
175 | /* |
---|
176 | * If user has no ticket file and principal was not specified, |
---|
177 | * we will try to get tickets for username@remote_realm |
---|
178 | */ |
---|
179 | strcpy(aname, username); |
---|
180 | strcpy(realm, r_krealm); |
---|
181 | } |
---|
182 | } |
---|
183 | else { |
---|
184 | status = kname_parse(aname, inst, realm, principal); |
---|
185 | if (status != KSUCCESS) { |
---|
186 | fprintf(stderr, "%s\n", krb_err_txt[status]); |
---|
187 | exit(1); |
---|
188 | } |
---|
189 | if (strlen(realm) == 0) { |
---|
190 | if (krb_get_lrealm(realm, 1) != KSUCCESS) |
---|
191 | strcpy(realm, KRB_REALM); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | memset(&info, 0, sizeof(info)); |
---|
196 | |
---|
197 | strcpy(info.aname, aname); |
---|
198 | strcpy(info.inst, inst); |
---|
199 | strcpy(info.realm, realm); |
---|
200 | strcpy(info.sname, "krbtgt"); |
---|
201 | strcpy(info.sinst, realm); |
---|
202 | strncpy(info.username, username, sizeof(info.username) - 1); |
---|
203 | if (tktfilename) |
---|
204 | strncpy(info.tktfilename, tktfilename, sizeof(info.tktfilename) - 1); |
---|
205 | info.lifetime = lifetime; |
---|
206 | |
---|
207 | status = rkinit(host, r_krealm, &info, timeout); |
---|
208 | if (status) { |
---|
209 | com_err(whoami, status, "while obtaining remote tickets:"); |
---|
210 | fprintf(stderr, "%s\n", rkinit_errmsg(0)); |
---|
211 | exit(1); |
---|
212 | } |
---|
213 | |
---|
214 | exit(0); |
---|
215 | } |
---|