6 * Copyright (c) 2002, Cray Inc. (Wendy Palm <wendyp@cray.com>)
7 * Significant portions provided by
8 * Wayne Schroeder, SDSC <schroeder@sdsc.edu>
9 * William Jones, UTexas <jones@tacc.utexas.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Created: Apr 22 16.34:00 2002 wp
33 * This file contains functions required for proper execution
42 #include <sys/category.h>
47 #include <sys/secparm.h>
51 #include <sys/sectab.h>
52 #include <sys/secstat.h>
54 #include <sys/session.h>
68 extern ServerOptions options;
70 char cray_tmpdir[TPATHSIZ + 1]; /* job TMPDIR path */
72 struct sysv sysv; /* system security structure */
73 struct usrv usrv; /* user security structure */
78 void cray_retain_utmp(struct utmp *, int);
79 void cray_delete_tmpdir(char *, int, uid_t);
80 void cray_init_job(struct passwd *);
81 void cray_set_tmpdir(struct utmp *);
82 void cray_login_failure(char *, int);
83 int cray_setup(uid_t, char *, const char *);
84 int cray_access_denied(char *);
87 cray_login_failure(char *username, int errcode)
89 struct udb *ueptr; /* UDB pointer for username */
90 ia_failure_t fsent; /* ia_failure structure */
91 ia_failure_ret_t fret; /* ia_failure return stuff */
92 struct jtab jtab; /* job table structure */
93 int jid = 0; /* job id */
95 if ((jid = getjtab(&jtab)) < 0)
96 debug("cray_login_failure(): getjtab error");
99 if ((ueptr = getudbnam(username)) == UDB_NULL)
100 debug("cray_login_failure(): getudbname() returned NULL");
103 memset(&fsent, '\0', sizeof(fsent));
105 fsent.uname = username;
106 fsent.host = (char *)get_canonical_hostname(options.use_dns);
108 fsent.caller = IA_SSHD;
109 fsent.flags = IA_INTERACTIVE;
112 fsent.errcode = errcode;
114 fsent.exitcode = 0; /* dont exit in ia_failure() */
120 * Call ia_failure because of an login failure.
122 ia_failure(&fsent, &fret);
129 cray_access_denied(char *username)
131 struct udb *ueptr; /* UDB pointer for username */
132 int errcode; /* IA errorcode */
136 if ((ueptr = getudbnam(username)) == UDB_NULL)
137 debug("cray_login_failure(): getudbname() returned NULL");
140 if (ueptr != NULL && ueptr->ue_disabled)
141 errcode = IA_DISABLED;
143 cray_login_failure(username, errcode);
149 * record_failed_login: generic "login failed" interface function
152 record_failed_login(const char *user, const char *ttyname)
154 cray_login_failure((char *)user, IA_UDBERR);
158 cray_setup (uid_t uid, char *username, const char *command)
160 extern struct udb *getudb();
161 extern char *setlimits();
163 int err; /* error return */
164 time_t system_time; /* current system clock */
165 time_t expiration_time; /* password expiration time */
166 int maxattempts; /* maximum no. of failed login attempts */
167 int SecureSys; /* unicos security flag */
168 int minslevel = 0; /* system minimum security level */
170 int valid_acct = -1; /* flag for reading valid acct */
171 char acct_name[MAXACID] = { "" }; /* used to read acct name */
172 struct jtab jtab; /* Job table struct */
173 struct udb ue; /* udb entry for logging-in user */
174 struct udb *up; /* pointer to UDB entry */
175 struct secstat secinfo; /* file security attributes */
176 struct servprov init_info; /* used for sesscntl() call */
177 int jid; /* job ID */
178 int pid; /* process ID */
179 char *sr; /* status return from setlimits() */
180 char *ttyn = NULL; /* ttyname or command name*/
181 char hostname[MAXHOSTNAMELEN];
182 /* passwd stuff for ia_user */
183 passwd_t pwdacm, pwddialup, pwdudb, pwdwal, pwddce;
184 ia_user_ret_t uret; /* stuff returned from ia_user */
185 ia_user_t usent /* ia_user main structure */
186 int ia_rcode; /* ia_user return code */
187 ia_failure_t fsent; /* ia_failure structure */
188 ia_failure_ret_t fret; /* ia_failure return stuff */
189 ia_success_t ssent; /* ia_success structure */
190 ia_success_ret_t sret; /* ia_success return stuff */
191 int ia_mlsrcode; /* ia_mlsuser return code */
192 int secstatrc; /* [f]secstat return code */
194 if (SecureSys = (int)sysconf(_SC_CRAY_SECURE_SYS)) {
195 getsysv(&sysv, sizeof(struct sysv));
196 minslevel = sysv.sy_minlvl;
197 if (getusrv(&usrv) < 0)
198 fatal("getusrv() failed, errno = %d", errno);
202 (char *)get_canonical_hostname(options.use_dns),
205 * Fetch user's UDB entry.
208 if ((up = getudbnam(username)) == UDB_NULL)
209 fatal("cannot fetch user's UDB entry");
212 * Prevent any possible fudging so perform a data
213 * safety check and compare the supplied uid against
216 if (up->ue_uid != uid)
217 fatal("IA uid missmatch");
220 if ((jid = getjtab(&jtab)) < 0) {
228 secstatrc = secstat(ttyn, &secinfo);
230 secstatrc = fsecstat(1, &secinfo);
233 debug("[f]secstat() successful");
235 fatal("[f]secstat() error, rc = %d", secstatrc);
237 if ((ttyn == NULL) && ((char *)command != NULL))
238 ttyn = (char *)command;
240 * Initialize all structures to call ia_user
243 usent.uname = username;
244 usent.host = hostname;
246 usent.caller = IA_SSHD;
247 usent.pswdlist = &pwdacm;
249 usent.flags = IA_INTERACTIVE | IA_FFLAG;
250 pwdacm.atype = IA_SECURID;
252 pwdacm.next = &pwdudb;
254 pwdudb.atype = IA_UDB;
256 pwdudb.next = &pwddce;
258 pwddce.atype = IA_DCE;
260 pwddce.next = &pwddialup;
262 pwddialup.atype = IA_DIALUP;
263 pwddialup.pwdp = NULL;
264 /* pwddialup.next = &pwdwal; */
265 pwddialup.next = NULL;
267 pwdwal.atype = IA_WAL;
275 ia_rcode = ia_user(&usent, &uret);
278 * These are acceptable return codes from ia_user()
280 case IA_UDBWEEK: /* Password Expires in 1 week */
281 expiration_time = ue.ue_pwage.time + ue.ue_pwage.maxage;
282 printf ("WARNING - your current password will expire %s\n",
283 ctime((const time_t *)&expiration_time));
286 if (ttyname(0) != NULL) {
287 /* Force a password change */
288 printf("Your password has expired; Choose a new one.\n");
289 execl("/bin/passwd", "passwd", username, 0);
293 case IA_NORMAL: /* Normal Return Code */
296 /* XXX: can we memset it to zero here so save some of this */
297 strlcpy(ue.ue_name, "root", sizeof(ue.ue_name));
298 strlcpy(ue.ue_dir, "/", sizeof(ue.ue_dir));
299 strlcpy(ue.ue_shell, "/bin/sh", sizeof(ue.ue_shell));
301 ue.ue_passwd[0] = '\0';
303 ue.ue_comment[0] = '\0';
304 ue.ue_loghost[0] = '\0';
305 ue.ue_logline[0] = '\0';
308 ue.ue_nice[UDBRC_INTER] = 0;
310 for (i = 0; i < MAXVIDS; i++)
314 ue.ue_minlvl = ue.ue_maxlvl = ue.ue_deflvl = minslevel;
322 case IA_CONSOLE: /* Superuser not from Console */
323 case IA_TRUSTED: /* Trusted user */
324 if (options.permit_root_login > PERMIT_NO)
325 break; /* Accept root login */
328 * These are failed return codes from ia_user()
333 printf("Bad authorization, access denied.\n");
336 printf("Your login has been disabled. Contact the system ");
337 printf("administrator for assistance.\n");
340 printf("getsysv() failed - errno = %d\n", errno);
343 printf("Maximum number of failed login attempts exceeded.\n");
344 printf("Access denied.\n");
348 printf("NULL Password not allowed on MLS systems.\n");
355 * Authentication failed.
357 printf("sshd: Login incorrect, (0%o)\n",
358 ia_rcode-IA_ERRORCODE);
361 * Initialize structure for ia_failure
365 fsent.uname = username;
366 fsent.host = hostname;
368 fsent.caller = IA_SSHD;
369 fsent.flags = IA_INTERACTIVE;
372 fsent.errcode = ia_rcode;
373 fsent.pwdp = uret.pswd;
380 * Call ia_failure because of an IA failure.
381 * There is no return because ia_failure exits.
383 ia_failure(&fsent, &fret);
388 ia_mlsrcode = IA_NORMAL;
390 debug("calling ia_mlsuser()");
391 ia_mlsrcode = ia_mlsuser(&ue, &secinfo, &usrv, NULL, 0);
393 if (ia_mlsrcode != IA_NORMAL) {
394 printf("sshd: Login incorrect, (0%o)\n",
395 ia_mlsrcode-IA_ERRORCODE);
397 * Initialize structure for ia_failure
401 fsent.uname = username;
402 fsent.host = hostname;
404 fsent.caller = IA_SSHD;
405 fsent.flags = IA_INTERACTIVE;
408 fsent.errcode = ia_mlsrcode;
409 fsent.pwdp = uret.pswd;
415 * Call ia_failure because of an IA failure.
416 * There is no return because ia_failure exits.
418 ia_failure(&fsent,&fret);
422 /* Provide login status information */
423 if (options.print_lastlog && ue.ue_logtime != 0) {
424 printf("Last successful login was : %.*s ", 19,
425 (char *)ctime(&ue.ue_logtime));
427 if (*ue.ue_loghost != '\0') {
428 printf("from %.*s\n", sizeof(ue.ue_loghost),
431 printf("on %.*s\n", sizeof(ue.ue_logline),
435 if (SecureSys && (ue.ue_logfails != 0)) {
436 printf(" followed by %d failed attempts\n",
442 * Call ia_success to process successful I/A.
445 ssent.uname = username;
446 ssent.host = hostname;
448 ssent.caller = IA_SSHD;
449 ssent.flags = IA_INTERACTIVE;
452 ssent.errcode = ia_rcode;
454 ssent.time = 1; /* Set ue_logtime */
459 ia_success(&ssent, &sret);
462 * Query for account, iff > 1 valid acid & askacid permbit
464 if (((ue.ue_permbits & PERMBITS_ACCTID) ||
465 (ue.ue_acids[0] >= 0) && (ue.ue_acids[1] >= 0)) &&
466 ue.ue_permbits & PERMBITS_ASKACID) {
467 if (ttyname(0) != NULL) {
468 debug("cray_setup: ttyname true case, %.100s", ttyname);
469 while (valid_acct == -1) {
470 printf("Account (? for available accounts)"
471 " [%s]: ", acid2nam(ue.ue_acids[0]));
472 fgets(acct_name, MAXACID, stdin);
473 switch (acct_name[0]) {
478 valid_acct = ue.ue_acids[0];
479 strlcpy(acct_name, acid2nam(valid_acct), MAXACID);
482 /* Print the list 3 wide */
483 for (i = 0, j = 0; i < MAXVIDS; i++) {
484 if (ue.ue_acids[i] == -1) {
493 acid2nam(ue.ue_acids[i]));
495 if (ue.ue_permbits & PERMBITS_ACCTID) {
496 printf("\"acctid\" permbit also allows"
497 " you to select any valid "
503 valid_acct = nam2acid(acct_name);
504 if (valid_acct == -1) {
506 "Account id not found for"
507 " account name \"%s\"\n\n",
512 * If an account was given, search the user's
513 * acids array to verify they can use this account.
515 if ((valid_acct != -1) &&
516 !(ue.ue_permbits & PERMBITS_ACCTID)) {
517 for (i = 0; i < MAXVIDS; i++) {
518 if (ue.ue_acids[i] == -1)
520 if (valid_acct == ue.ue_acids[i])
524 ue.ue_acids[i] == -1) {
525 fprintf(stderr, "Cannot set"
527 "\"%s\", permission "
528 "denied\n\n", acct_name);
535 * The client isn't connected to a terminal and can't
536 * respond to an acid prompt. Use default acid.
538 debug("cray_setup: ttyname false case, %.100s",
540 valid_acct = ue.ue_acids[0];
544 * The user doesn't have the askacid permbit set or
545 * only has one valid account to use.
547 valid_acct = ue.ue_acids[0];
549 if (acctid(0, valid_acct) < 0) {
550 printf ("Bad account id: %d\n", valid_acct);
555 * Now set shares, quotas, limits, including CPU time for the
556 * (interactive) job and process, and set up permissions
557 * (for chown etc), etc.
559 if (setshares(ue.ue_uid, valid_acct, printf, 0, 0)) {
560 printf("Unable to give %d shares to <%s>(%d/%d)\n",
561 ue.ue_shares, ue.ue_name, ue.ue_uid, valid_acct);
565 sr = setlimits(username, C_PROC, pid, UDBRC_INTER);
570 sr = setlimits(username, C_JOB, jid, UDBRC_INTER);
576 * Place the service provider information into
577 * the session table (Unicos) or job table (Unicos/mk).
578 * There exist double defines for the job/session table in
579 * unicos/mk (jtab.h) so no need for a compile time switch.
581 memset(&init_info, '\0', sizeof(init_info));
582 init_info.s_sessinit.si_id = URM_SPT_LOGIN;
583 init_info.s_sessinit.si_pid = getpid();
584 init_info.s_sessinit.si_sid = jid;
585 sesscntl(0, S_SETSERVPO, (int)&init_info);
588 * Set user and controlling tty security attributes.
591 if (setusrv(&usrv) == -1) {
592 debug("setusrv() failed, errno = %d",errno);
601 * The rc.* and /etc/sdaemon methods of starting a program on unicos/unicosmk
602 * can have pal privileges that sshd can inherit which
603 * could allow a user to su to root with out a password.
604 * This subroutine clears all privileges.
609 #if defined(_SC_CRAY_PRIV_SU)
610 priv_proc_t *privstate;
612 extern int priv_set_proc();
613 extern priv_proc_t *priv_init_proc();
616 * If ether of theses two flags are not set
617 * then don't allow this version of ssh to run.
619 if (!sysconf(_SC_CRAY_PRIV_SU))
620 fatal("Not PRIV_SU system.");
621 if (!sysconf(_SC_CRAY_POSIX_PRIV))
622 fatal("Not POSIX_PRIV.");
624 debug("Setting MLS labels.");;
626 if (sysconf(_SC_CRAY_SECURE_MAC)) {
627 usrv.sv_minlvl = SYSLOW;
628 usrv.sv_actlvl = SYSHIGH;
629 usrv.sv_maxlvl = SYSHIGH;
631 usrv.sv_minlvl = sysv.sy_minlvl;
632 usrv.sv_actlvl = sysv.sy_minlvl;
633 usrv.sv_maxlvl = sysv.sy_maxlvl;
636 usrv.sv_valcmp = sysv.sy_valcmp;
638 usrv.sv_intcat = TFM_SYSTEM;
639 usrv.sv_valcat |= (TFM_SYSTEM | TFM_SYSFILE);
641 if (setusrv(&usrv) < 0) {
642 fatal("%s(%d): setusrv(): %s", __FILE__, __LINE__,
646 if ((privstate = priv_init_proc()) != NULL) {
647 result = priv_set_proc(privstate);
649 fatal("%s(%d): priv_set_proc(): %s",
650 __FILE__, __LINE__, strerror(errno));
652 priv_free_proc(privstate);
654 debug ("Privileges should be cleared...");
656 /* XXX: do this differently */
657 # error Cray systems must be run with _SC_CRAY_PRIV_SU on!
663 * Retain utmp/wtmp information - used by cray accounting.
666 cray_retain_utmp(struct utmp *ut, int pid)
671 if ((fd = open(UTMP_FILE, O_RDONLY)) != -1) {
672 /* XXX use atomicio */
673 while (read(fd, (char *)&utmp, sizeof(utmp)) == sizeof(utmp)) {
674 if (pid == utmp.ut_pid) {
675 ut->ut_jid = utmp.ut_jid;
676 strncpy(ut->ut_tpath, utmp.ut_tpath, sizeof(utmp.ut_tpath));
677 strncpy(ut->ut_host, utmp.ut_host, sizeof(utmp.ut_host));
678 strncpy(ut->ut_name, utmp.ut_name, sizeof(utmp.ut_name));
684 fatal("Unable to open utmp file");
692 * find and delete jobs tmpdir.
695 cray_delete_tmpdir(char *login, int jid, uid_t uid)
697 static char jtmp[TPATHSIZ];
701 for (c = 'a'; c <= 'z'; c++) {
702 snprintf(jtmp, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
703 if (stat(jtmp, &statbuf) == 0 && statbuf.st_uid == uid)
710 if ((child = fork()) == 0) {
711 execl(CLEANTMPCMD, CLEANTMPCMD, login, jtmp, (char *)NULL);
712 fatal("cray_delete_tmpdir: execl of CLEANTMPCMD failed");
715 while (waitpid(child, &wstat, 0) == -1 && errno == EINTR)
720 * Remove tmpdir on job termination.
723 cray_job_termination_handler(int sig)
729 debug("received signal %d",sig);
731 if ((jid = waitjob(&jtab)) == -1 ||
732 (login = uid2nam(jtab.j_uid)) == NULL)
735 cray_delete_tmpdir(login, jid, jtab.j_uid);
739 * Set job id and create tmpdir directory.
742 cray_init_job(struct passwd *pw)
747 jid = setjob(pw->pw_uid, WJSIGNAL);
749 fatal("System call setjob failure");
751 for (c = 'a'; c <= 'z'; c++) {
752 snprintf(cray_tmpdir, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
753 if (mkdir(cray_tmpdir, JTMPMODE) != 0)
755 if (chown(cray_tmpdir, pw->pw_uid, pw->pw_gid) != 0) {
763 cray_tmpdir[0] = '\0';
767 cray_set_tmpdir(struct utmp *ut)
772 if ((jid = getjtab(&jbuf)) < 0)
776 * Set jid and tmpdir in utmp record.
779 strncpy(ut->ut_tpath, cray_tmpdir, TPATHSIZ);