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>
15 #include <sys/types.h>
26 #include <afs/com_err.h>
27 #include <afs/cellconfig.h>
34 /* This code borrowed heavily from the previous version of log. Here is the
35 intro comment for that program: */
38 log -- tell the Andrew Cache Manager your password
43 Further modified in August 1987 to understand cell IDs.
47 klog [principal [password]] [-t] [-c cellname] [-servers <hostlist>]
50 principal is of the form 'name' or 'name@cell' which provides the
51 cellname. See the -c option below.
52 password is the user's password. This form is NOT recommended for
54 -t advises klog to write a Kerberos style ticket file in /tmp.
55 -c identifies cellname as the cell in which authentication is to take
57 -servers allows the explicit specification of the hosts providing
58 authentication services for the cell being used for authentication.
61 #define KLOGEXIT(code) assert(!code || code >= KAMINERROR); \
63 (!code ? exit(0) : exit((code)-KAMINERROR+1))
64 int CommandProc(struct cmd_syndesc *as, void *arock);
67 static char **zero_argv;
76 main(int argc, char *argv[])
78 struct cmd_syndesc *ts;
82 * The following signal action for AIX is necessary so that in case of a
83 * crash (i.e. core is generated) we can include the user's data section
84 * in the core dump. Unfortunately, by default, only a partial core is
85 * generated which, in many cases, isn't too useful.
89 sigemptyset(&nsa.sa_mask);
90 nsa.sa_handler = SIG_DFL;
91 nsa.sa_flags = SA_FULLDUMP;
92 sigaction(SIGABRT, &nsa, NULL);
93 sigaction(SIGSEGV, &nsa, NULL);
98 ts = cmd_CreateSyntax(NULL, CommandProc, NULL,
99 "obtain Kerberos authentication");
113 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
114 cmd_Seek(ts, aPRINCIPAL);
115 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
116 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
117 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
118 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
119 "explicit list of servers");
120 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL,
121 "read password from stdin");
122 cmd_AddParm(ts, "-silent", CMD_FLAG, CMD_OPTIONAL, "silent operation");
123 cmd_AddParm(ts, "-lifetime", CMD_SINGLE, CMD_OPTIONAL,
124 "ticket lifetime in hh[:mm[:ss]]");
125 cmd_AddParm(ts, "-setpag", CMD_FLAG, CMD_OPTIONAL,
126 "Create a new setpag before authenticating");
127 cmd_AddParm(ts, "-tmp", CMD_FLAG, CMD_OPTIONAL,
128 "write Kerberos-style ticket file in /tmp");
130 code = cmd_Dispatch(argc, argv);
137 static char gpbuf[BUFSIZ];
138 /* read a password from stdin, stop on \n or eof */
140 memset(gpbuf, 0, sizeof(gpbuf));
141 for (i = 0; i < (sizeof(gpbuf) - 1); i++) {
143 if (tc == '\n' || tc == EOF)
151 CommandProc(struct cmd_syndesc *as, void *arock)
153 char name[MAXKTCNAMELEN];
154 char instance[MAXKTCNAMELEN];
155 char cell[MAXKTCREALMLEN];
156 char realm[MAXKTCREALMLEN];
157 afs_int32 serverList[MAXSERVERS];
158 char *lcell; /* local cellname */
159 char lrealm[MAXKTCREALMLEN]; /* uppercase copy of local cellname */
162 Date lifetime; /* requested ticket lifetime */
165 struct passwd *pw = &pwent;
166 struct passwd *lclpw = &pwent;
169 static char rn[] = "klog"; /*Routine name */
170 static int Pipe = 0; /* reading from a pipe */
171 static int Silent = 0; /* Don't want error messages */
173 int explicit; /* servers specified explicitly */
174 int local; /* explicit cell is same a local one */
175 int foundPassword = 0; /*Not yet, anyway */
176 int foundExplicitCell = 0; /*Not yet, anyway */
177 int writeTicketFile = 0; /* write ticket file to /tmp */
178 afs_int32 password_expires = -1;
180 char *reason; /* string describing errors */
182 /* blow away command line arguments */
183 for (i = 1; i < zero_argc; i++)
184 memset(zero_argv[i], 0, strlen(zero_argv[i]));
187 /* first determine quiet flag based on -silent switch */
188 Silent = (as->parms[aSILENT].items ? 1 : 0);
189 Pipe = (as->parms[aPIPE].items ? 1 : 0);
191 /* Determine if we should also do a setpag based on -setpag switch */
192 dosetpag = (as->parms[aSETPAG].items ? 1 : 0);
194 if (as->parms[aTMP].items) {
198 if (as->parms[aCELL].items) {
200 * cell name explicitly mentioned; take it in if no other cell name
201 * has already been specified and if the name actually appears. If
202 * the given cell name differs from our own, we don't do a lookup.
204 foundExplicitCell = 1;
205 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
206 /* XXX the following is just a hack to handle the afscell environment XXX */
207 (void)afsconf_GetCellInfo((struct afsconf_dir *)0, realm, 0,
208 (struct afsconf_cell *)0);
212 if (code || !(lcell = ka_LocalCell())) {
215 afs_com_err(rn, code, "Can't get local cell name!");
218 if (code = ka_CellToRealm(lcell, lrealm, 0))
221 strcpy(instance, "");
223 /* Parse our arguments. */
225 if (as->parms[aCELL].items) {
227 * cell name explicitly mentioned; take it in if no other cell name
228 * has already been specified and if the name actually appears. If
229 * the given cell name differs from our own, we don't do a lookup.
231 foundExplicitCell = 1;
232 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
235 if (as->parms[aSERVERS].items) {
236 /* explicit server list */
239 char *ap[MAXSERVERS + 2];
241 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
245 code = ubik_ParseClientList(i, ap, serverList);
248 afs_com_err(rn, code, "could not parse server list");
256 if (as->parms[aPRINCIPAL].items) {
257 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
259 if (strlen(instance) > 0)
262 "Non-null instance (%s) may cause strange behavior.\n",
265 if (strlen(cell) > 0) {
266 if (foundExplicitCell) {
269 "%s: May not specify an explicit cell twice.\n",
274 foundExplicitCell = 1;
275 strncpy(realm, cell, sizeof(realm));
277 lclpw->pw_name = name;
279 /* No explicit name provided: use Unix uid. */
280 pw = getpwuid(getuid());
284 "Can't figure out your name in local cell %s from your user id.\n",
286 fprintf(stderr, "Try providing the user name.\n");
288 KLOGEXIT(KABADARGUMENT);
293 if (as->parms[aPASSWORD].items) {
295 * Current argument is the desired password string. Remember it in
296 * our local buffer, and zero out the argument string - anyone can
297 * see it there with ps!
300 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
301 memset(as->parms[aPASSWORD].items->data, 0,
302 strlen(as->parms[aPASSWORD].items->data));
305 if (as->parms[aLIFETIME].items) {
306 char *life = as->parms[aLIFETIME].items->data;
307 char *sp; /* string ptr to rest of life */
308 lifetime = 3600 * strtol(life, &sp, 0); /* hours */
312 fprintf(stderr, "%s: translating '%s' to lifetime failed\n",
314 return KABADARGUMENT;
317 life = sp + 1; /* skip the colon */
318 lifetime += 60 * strtol(life, &sp, 0); /* minutes */
323 lifetime += strtol(life, &sp, 0); /* seconds */
332 if (lifetime > MAXKTCTICKETLIFETIME) {
335 "%s: a lifetime of %.2f hours is too long, must be less than %d.\n",
336 rn, (double)lifetime / 3600.0,
337 MAXKTCTICKETLIFETIME / 3600);
338 KLOGEXIT(KABADARGUMENT);
343 if (!foundExplicitCell)
344 strcpy(realm, lcell);
345 if (code = ka_CellToRealm(realm, realm, &local)) {
347 afs_com_err(rn, code, "Can't convert cell to realm");
351 /* Get the password if it wasn't provided. */
352 if (!foundPassword) {
354 strncpy(passwd, getpipepass(), sizeof(passwd));
356 if (ka_UserReadPassword
357 ("Password:", passwd, sizeof(passwd), &reason)) {
358 fprintf(stderr, "Unable to login because %s.\n", reason);
359 KLOGEXIT(KABADARGUMENT);
365 ka_ExplicitCell(realm, serverList);
368 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
369 (dosetpag ? KA_USERAUTH_DOSETPAG2 : 0),
370 pw->pw_name, instance, realm, passwd,
371 lifetime, &password_expires, 0, &reason);
372 memset(passwd, 0, sizeof(passwd));
375 fprintf(stderr, "Unable to authenticate to AFS because %s.\n",
380 #ifndef AFS_KERBEROS_ENV
381 if (writeTicketFile) {
382 code = krb_write_ticket_file(realm);
385 afs_com_err(rn, code, "writing Kerberos ticket file");
387 fprintf(stderr, "Wrote ticket file to /tmp\n");
393 if (password_expires >= 0) {
394 printf("password expires at %ld\n", password_expires);
396 #endif /* DEBUGEXPIRES */