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>
19 #include <afs/com_err.h>
20 #include <afs/cellconfig.h>
25 #include "kauth_internal.h"
29 /* This code borrowed heavily from the previous version of log. Here is the
30 intro comment for that program: */
33 log -- tell the Andrew Cache Manager your password
38 Further modified in August 1987 to understand cell IDs.
42 klog [principal [password]] [-t] [-c cellname] [-servers <hostlist>]
45 principal is of the form 'name' or 'name@cell' which provides the
46 cellname. See the -c option below.
47 password is the user's password. This form is NOT recommended for
49 -t advises klog to write a Kerberos style ticket file in /tmp.
50 -c identifies cellname as the cell in which authentication is to take
52 -servers allows the explicit specification of the hosts providing
53 authentication services for the cell being used for authentication.
56 #define KLOGEXIT(code) assert(!code || code >= KAMINERROR); \
58 (!code ? exit(0) : exit((code)-KAMINERROR+1))
59 int CommandProc(struct cmd_syndesc *as, void *arock);
62 static char **zero_argv;
71 main(int argc, char *argv[])
73 struct cmd_syndesc *ts;
77 * The following signal action for AIX is necessary so that in case of a
78 * crash (i.e. core is generated) we can include the user's data section
79 * in the core dump. Unfortunately, by default, only a partial core is
80 * generated which, in many cases, isn't too useful.
84 sigemptyset(&nsa.sa_mask);
85 nsa.sa_handler = SIG_DFL;
86 nsa.sa_flags = SA_FULLDUMP;
87 sigaction(SIGABRT, &nsa, NULL);
88 sigaction(SIGSEGV, &nsa, NULL);
93 ts = cmd_CreateSyntax(NULL, CommandProc, NULL,
94 "obtain Kerberos authentication");
108 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
109 cmd_Seek(ts, aPRINCIPAL);
110 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
111 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
112 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
113 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
114 "explicit list of servers");
115 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL,
116 "read password from stdin");
117 cmd_AddParm(ts, "-silent", CMD_FLAG, CMD_OPTIONAL, "silent operation");
118 cmd_AddParm(ts, "-lifetime", CMD_SINGLE, CMD_OPTIONAL,
119 "ticket lifetime in hh[:mm[:ss]]");
120 cmd_AddParm(ts, "-setpag", CMD_FLAG, CMD_OPTIONAL,
121 "Create a new setpag before authenticating");
122 cmd_AddParm(ts, "-tmp", CMD_FLAG, CMD_OPTIONAL,
123 "write Kerberos-style ticket file in /tmp");
125 code = cmd_Dispatch(argc, argv);
132 static char gpbuf[BUFSIZ];
133 /* read a password from stdin, stop on \n or eof */
135 memset(gpbuf, 0, sizeof(gpbuf));
136 for (i = 0; i < (sizeof(gpbuf) - 1); i++) {
138 if (tc == '\n' || tc == EOF)
146 CommandProc(struct cmd_syndesc *as, void *arock)
148 char name[MAXKTCNAMELEN];
149 char instance[MAXKTCNAMELEN];
150 char cell[MAXKTCREALMLEN];
151 char realm[MAXKTCREALMLEN];
152 afs_uint32 serverList[MAXSERVERS];
153 char *lcell; /* local cellname */
154 char lrealm[MAXKTCREALMLEN]; /* uppercase copy of local cellname */
157 Date lifetime; /* requested ticket lifetime */
160 struct passwd *pw = &pwent;
161 struct passwd *lclpw = &pwent;
164 static char rn[] = "klog"; /*Routine name */
165 static int Pipe = 0; /* reading from a pipe */
166 static int Silent = 0; /* Don't want error messages */
168 int explicit; /* servers specified explicitly */
169 int local; /* explicit cell is same a local one */
170 int foundPassword = 0; /*Not yet, anyway */
171 int foundExplicitCell = 0; /*Not yet, anyway */
172 int writeTicketFile = 0; /* write ticket file to /tmp */
173 afs_int32 password_expires = -1;
175 char *reason; /* string describing errors */
177 /* blow away command line arguments */
178 for (i = 1; i < zero_argc; i++)
179 memset(zero_argv[i], 0, strlen(zero_argv[i]));
182 /* first determine quiet flag based on -silent switch */
183 Silent = (as->parms[aSILENT].items ? 1 : 0);
184 Pipe = (as->parms[aPIPE].items ? 1 : 0);
186 /* Determine if we should also do a setpag based on -setpag switch */
187 dosetpag = (as->parms[aSETPAG].items ? 1 : 0);
189 if (as->parms[aTMP].items) {
193 if (as->parms[aCELL].items) {
195 * cell name explicitly mentioned; take it in if no other cell name
196 * has already been specified and if the name actually appears. If
197 * the given cell name differs from our own, we don't do a lookup.
199 foundExplicitCell = 1;
200 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
201 /* XXX the following is just a hack to handle the afscell environment XXX */
202 (void)afsconf_GetCellInfo((struct afsconf_dir *)0, realm, 0,
203 (struct afsconf_cell *)0);
207 if (code || !(lcell = ka_LocalCell())) {
210 afs_com_err(rn, code, "Can't get local cell name!");
213 if ((code = ka_CellToRealm(lcell, lrealm, 0)))
216 strcpy(instance, "");
218 /* Parse our arguments. */
220 if (as->parms[aCELL].items) {
222 * cell name explicitly mentioned; take it in if no other cell name
223 * has already been specified and if the name actually appears. If
224 * the given cell name differs from our own, we don't do a lookup.
226 foundExplicitCell = 1;
227 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
230 if (as->parms[aSERVERS].items) {
231 /* explicit server list */
234 char *ap[MAXSERVERS + 2];
236 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
240 code = ubik_ParseClientList(i, ap, serverList);
243 afs_com_err(rn, code, "could not parse server list");
251 if (as->parms[aPRINCIPAL].items) {
252 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
254 if (strlen(instance) > 0)
257 "Non-null instance (%s) may cause strange behavior.\n",
260 if (strlen(cell) > 0) {
261 if (foundExplicitCell) {
264 "%s: May not specify an explicit cell twice.\n",
269 foundExplicitCell = 1;
270 strncpy(realm, cell, sizeof(realm));
272 lclpw->pw_name = name;
274 /* No explicit name provided: use Unix uid. */
275 pw = getpwuid(getuid());
279 "Can't figure out your name in local cell %s from your user id.\n",
281 fprintf(stderr, "Try providing the user name.\n");
283 KLOGEXIT(KABADARGUMENT);
288 if (as->parms[aPASSWORD].items) {
290 * Current argument is the desired password string. Remember it in
291 * our local buffer, and zero out the argument string - anyone can
292 * see it there with ps!
295 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
296 memset(as->parms[aPASSWORD].items->data, 0,
297 strlen(as->parms[aPASSWORD].items->data));
300 if (as->parms[aLIFETIME].items) {
301 char *life = as->parms[aLIFETIME].items->data;
302 char *sp; /* string ptr to rest of life */
303 lifetime = 3600 * strtol(life, &sp, 0); /* hours */
307 fprintf(stderr, "%s: translating '%s' to lifetime failed\n",
309 return KABADARGUMENT;
312 life = sp + 1; /* skip the colon */
313 lifetime += 60 * strtol(life, &sp, 0); /* minutes */
318 lifetime += strtol(life, &sp, 0); /* seconds */
327 if (lifetime > MAXKTCTICKETLIFETIME) {
330 "%s: a lifetime of %.2f hours is too long, must be less than %d.\n",
331 rn, (double)lifetime / 3600.0,
332 MAXKTCTICKETLIFETIME / 3600);
333 KLOGEXIT(KABADARGUMENT);
338 if (!foundExplicitCell)
339 strcpy(realm, lcell);
340 if ((code = ka_CellToRealm(realm, realm, &local))) {
342 afs_com_err(rn, code, "Can't convert cell to realm");
346 /* Get the password if it wasn't provided. */
347 if (!foundPassword) {
349 strncpy(passwd, getpipepass(), sizeof(passwd));
351 if (ka_UserReadPassword
352 ("Password:", passwd, sizeof(passwd), &reason)) {
353 fprintf(stderr, "Unable to login because %s.\n", reason);
354 KLOGEXIT(KABADARGUMENT);
360 ka_ExplicitCell(realm, serverList);
363 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
364 (dosetpag ? KA_USERAUTH_DOSETPAG2 : 0),
365 pw->pw_name, instance, realm, passwd,
366 lifetime, &password_expires, 0, &reason);
367 memset(passwd, 0, sizeof(passwd));
370 fprintf(stderr, "Unable to authenticate to AFS because %s.\n",
375 #ifndef AFS_KERBEROS_ENV
376 if (writeTicketFile) {
377 code = krb_write_ticket_file(realm);
380 afs_com_err(rn, code, "writing Kerberos ticket file");
382 fprintf(stderr, "Wrote ticket file to /tmp\n");
388 if (password_expires >= 0) {
389 printf("password expires at %ld\n", password_expires);
391 #endif /* DEBUGEXPIRES */