2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <sys/types.h>
28 #include <afs/com_err.h>
29 #include <afs/cellconfig.h>
36 /* This code borrowed heavily from the previous version of log. Here is the
37 intro comment for that program: */
40 log -- tell the Andrew Cache Manager your password
45 Further modified in August 1987 to understand cell IDs.
49 klog [principal [password]] [-t] [-c cellname] [-servers <hostlist>]
52 principal is of the form 'name' or 'name@cell' which provides the
53 cellname. See the -c option below.
54 password is the user's password. This form is NOT recommended for
56 -t advises klog to write a Kerberos style ticket file in /tmp.
57 -c identifies cellname as the cell in which authentication is to take
59 -servers allows the explicit specification of the hosts providing
60 authentication services for the cell being used for authentication.
63 #define KLOGEXIT(code) assert(!code || code >= KAMINERROR); \
65 (!code ? exit(0) : exit((code)-KAMINERROR+1))
66 extern int CommandProc(struct cmd_syndesc *as, char *arock);
69 static char **zero_argv;
78 main(int argc, char *argv[])
80 struct cmd_syndesc *ts;
84 * The following signal action for AIX is necessary so that in case of a
85 * crash (i.e. core is generated) we can include the user's data section
86 * in the core dump. Unfortunately, by default, only a partial core is
87 * generated which, in many cases, isn't too useful.
91 sigemptyset(&nsa.sa_mask);
92 nsa.sa_handler = SIG_DFL;
93 nsa.sa_flags = SA_FULLDUMP;
94 sigaction(SIGABRT, &nsa, NULL);
95 sigaction(SIGSEGV, &nsa, NULL);
100 ts = cmd_CreateSyntax(NULL, CommandProc, 0,
101 "obtain Kerberos authentication");
115 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
116 cmd_Seek(ts, aPRINCIPAL);
117 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
118 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
119 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
120 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
121 "explicit list of servers");
122 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL,
123 "read password from stdin");
124 cmd_AddParm(ts, "-silent", CMD_FLAG, CMD_OPTIONAL, "silent operation");
125 cmd_AddParm(ts, "-lifetime", CMD_SINGLE, CMD_OPTIONAL,
126 "ticket lifetime in hh[:mm[:ss]]");
127 cmd_AddParm(ts, "-setpag", CMD_FLAG, CMD_OPTIONAL,
128 "Create a new setpag before authenticating");
129 cmd_AddParm(ts, "-tmp", CMD_FLAG, CMD_OPTIONAL,
130 "write Kerberos-style ticket file in /tmp");
132 code = cmd_Dispatch(argc, argv);
139 static char gpbuf[BUFSIZ];
140 /* read a password from stdin, stop on \n or eof */
142 memset(gpbuf, 0, sizeof(gpbuf));
143 for (i = 0; i < (sizeof(gpbuf) - 1); i++) {
145 if (tc == '\n' || tc == EOF)
153 CommandProc(struct cmd_syndesc *as, char *arock)
155 char name[MAXKTCNAMELEN];
156 char instance[MAXKTCNAMELEN];
157 char cell[MAXKTCREALMLEN];
158 char realm[MAXKTCREALMLEN];
159 afs_int32 serverList[MAXSERVERS];
160 char *lcell; /* local cellname */
161 char lrealm[MAXKTCREALMLEN]; /* uppercase copy of local cellname */
164 Date lifetime; /* requested ticket lifetime */
167 struct passwd *pw = &pwent;
168 struct passwd *lclpw = &pwent;
171 static char rn[] = "klog"; /*Routine name */
172 static int Pipe = 0; /* reading from a pipe */
173 static int Silent = 0; /* Don't want error messages */
175 int explicit; /* servers specified explicitly */
176 int local; /* explicit cell is same a local one */
177 int foundPassword = 0; /*Not yet, anyway */
178 int foundExplicitCell = 0; /*Not yet, anyway */
179 int writeTicketFile = 0; /* write ticket file to /tmp */
180 afs_int32 password_expires = -1;
182 char *reason; /* string describing errors */
184 /* blow away command line arguments */
185 for (i = 1; i < zero_argc; i++)
186 memset(zero_argv[i], 0, strlen(zero_argv[i]));
189 /* first determine quiet flag based on -silent switch */
190 Silent = (as->parms[aSILENT].items ? 1 : 0);
191 Pipe = (as->parms[aPIPE].items ? 1 : 0);
193 /* Determine if we should also do a setpag based on -setpag switch */
194 dosetpag = (as->parms[aSETPAG].items ? 1 : 0);
196 if (as->parms[aTMP].items) {
200 if (as->parms[aCELL].items) {
202 * cell name explicitly mentioned; take it in if no other cell name
203 * has already been specified and if the name actually appears. If
204 * the given cell name differs from our own, we don't do a lookup.
206 foundExplicitCell = 1;
207 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
208 /* XXX the following is just a hack to handle the afscell environment XXX */
209 (void)afsconf_GetCellInfo((struct afsconf_dir *)0, realm, 0,
210 (struct afsconf_cell *)0);
214 if (code || !(lcell = ka_LocalCell())) {
217 afs_com_err(rn, code, "Can't get local cell name!");
220 if (code = ka_CellToRealm(lcell, lrealm, 0))
223 strcpy(instance, "");
225 /* Parse our arguments. */
227 if (as->parms[aCELL].items) {
229 * cell name explicitly mentioned; take it in if no other cell name
230 * has already been specified and if the name actually appears. If
231 * the given cell name differs from our own, we don't do a lookup.
233 foundExplicitCell = 1;
234 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
237 if (as->parms[aSERVERS].items) {
238 /* explicit server list */
241 char *ap[MAXSERVERS + 2];
243 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
247 code = ubik_ParseClientList(i, ap, serverList);
250 afs_com_err(rn, code, "could not parse server list");
258 if (as->parms[aPRINCIPAL].items) {
259 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
261 if (strlen(instance) > 0)
264 "Non-null instance (%s) may cause strange behavior.\n",
267 if (strlen(cell) > 0) {
268 if (foundExplicitCell) {
271 "%s: May not specify an explicit cell twice.\n",
276 foundExplicitCell = 1;
277 strncpy(realm, cell, sizeof(realm));
279 lclpw->pw_name = name;
281 /* No explicit name provided: use Unix uid. */
282 pw = getpwuid(getuid());
286 "Can't figure out your name in local cell %s from your user id.\n",
288 fprintf(stderr, "Try providing the user name.\n");
290 KLOGEXIT(KABADARGUMENT);
295 if (as->parms[aPASSWORD].items) {
297 * Current argument is the desired password string. Remember it in
298 * our local buffer, and zero out the argument string - anyone can
299 * see it there with ps!
302 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
303 memset(as->parms[aPASSWORD].items->data, 0,
304 strlen(as->parms[aPASSWORD].items->data));
307 if (as->parms[aLIFETIME].items) {
308 char *life = as->parms[aLIFETIME].items->data;
309 char *sp; /* string ptr to rest of life */
310 lifetime = 3600 * strtol(life, &sp, 0); /* hours */
314 fprintf(stderr, "%s: translating '%s' to lifetime failed\n",
316 return KABADARGUMENT;
319 life = sp + 1; /* skip the colon */
320 lifetime += 60 * strtol(life, &sp, 0); /* minutes */
325 lifetime += strtol(life, &sp, 0); /* seconds */
334 if (lifetime > MAXKTCTICKETLIFETIME) {
337 "%s: a lifetime of %.2f hours is too long, must be less than %d.\n",
338 rn, (double)lifetime / 3600.0,
339 MAXKTCTICKETLIFETIME / 3600);
340 KLOGEXIT(KABADARGUMENT);
345 if (!foundExplicitCell)
346 strcpy(realm, lcell);
347 if (code = ka_CellToRealm(realm, realm, &local)) {
349 afs_com_err(rn, code, "Can't convert cell to realm");
353 /* Get the password if it wasn't provided. */
354 if (!foundPassword) {
356 strncpy(passwd, getpipepass(), sizeof(passwd));
358 if (ka_UserReadPassword
359 ("Password:", passwd, sizeof(passwd), &reason)) {
360 fprintf(stderr, "Unable to login because %s.\n", reason);
361 KLOGEXIT(KABADARGUMENT);
367 ka_ExplicitCell(realm, serverList);
370 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
371 (dosetpag ? KA_USERAUTH_DOSETPAG2 : 0),
372 pw->pw_name, instance, realm, passwd,
373 lifetime, &password_expires, 0, &reason);
374 memset(passwd, 0, sizeof(passwd));
377 fprintf(stderr, "Unable to authenticate to AFS because %s.\n",
382 #ifndef AFS_KERBEROS_ENV
383 if (writeTicketFile) {
384 code = krb_write_ticket_file(realm);
387 afs_com_err(rn, code, "writing Kerberos ticket file");
389 fprintf(stderr, "Wrote ticket file to /tmp\n");
395 if (password_expires >= 0) {
396 printf("password expires at %ld\n", password_expires);
398 #endif /* DEBUGEXPIRES */