2 * $Id: bsd-cray.c,v 1.13 2004/01/30 03:34:22 dtucker Exp $
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>
64 #include "sys/types.h"
66 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
67 # define _SS_MAXSIZE 128 /* Implementation specific max size */
68 # define _SS_PADSIZE (_SS_MAXSIZE - sizeof (struct sockaddr))
70 # define ss_family ss_sa.sa_family
71 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
73 #ifndef IN6_IS_ADDR_LOOPBACK
74 # define IN6_IS_ADDR_LOOPBACK(a) \
75 (((u_int32_t *) (a))[0] == 0 && ((u_int32_t *) (a))[1] == 0 && \
76 ((u_int32_t *) (a))[2] == 0 && ((u_int32_t *) (a))[3] == htonl (1))
77 #endif /* !IN6_IS_ADDR_LOOPBACK */
80 /* Define it to something that should never appear */
81 #define AF_INET6 AF_MAX
90 extern ServerOptions options;
92 char cray_tmpdir[TPATHSIZ + 1]; /* job TMPDIR path */
94 struct sysv sysv; /* system security structure */
95 struct usrv usrv; /* user security structure */
100 void cray_retain_utmp(struct utmp *, int);
101 void cray_delete_tmpdir(char *, int, uid_t);
102 void cray_init_job(struct passwd *);
103 void cray_set_tmpdir(struct utmp *);
104 void cray_login_failure(char *, int);
105 int cray_setup(uid_t, char *, const char *);
106 int cray_access_denied(char *);
109 cray_login_failure(char *username, int errcode)
111 struct udb *ueptr; /* UDB pointer for username */
112 ia_failure_t fsent; /* ia_failure structure */
113 ia_failure_ret_t fret; /* ia_failure return stuff */
114 struct jtab jtab; /* job table structure */
115 int jid = 0; /* job id */
117 if ((jid = getjtab(&jtab)) < 0)
118 debug("cray_login_failure(): getjtab error");
121 if ((ueptr = getudbnam(username)) == UDB_NULL)
122 debug("cray_login_failure(): getudbname() returned NULL");
125 memset(&fsent, '\0', sizeof(fsent));
127 fsent.uname = username;
128 fsent.host = (char *)get_canonical_hostname(options.use_dns);
130 fsent.caller = IA_SSHD;
131 fsent.flags = IA_INTERACTIVE;
134 fsent.errcode = errcode;
136 fsent.exitcode = 0; /* dont exit in ia_failure() */
142 * Call ia_failure because of an login failure.
144 ia_failure(&fsent, &fret);
151 cray_access_denied(char *username)
153 struct udb *ueptr; /* UDB pointer for username */
154 int errcode; /* IA errorcode */
158 if ((ueptr = getudbnam(username)) == UDB_NULL)
159 debug("cray_login_failure(): getudbname() returned NULL");
162 if (ueptr != NULL && ueptr->ue_disabled)
163 errcode = IA_DISABLED;
165 cray_login_failure(username, errcode);
171 * record_failed_login: generic "login failed" interface function
174 record_failed_login(const char *user, const char *ttyname)
176 cray_login_failure((char *)user, IA_UDBERR);
180 cray_setup (uid_t uid, char *username, const char *command)
182 extern struct udb *getudb();
183 extern char *setlimits();
185 int err; /* error return */
186 time_t system_time; /* current system clock */
187 time_t expiration_time; /* password expiration time */
188 int maxattempts; /* maximum no. of failed login attempts */
189 int SecureSys; /* unicos security flag */
190 int minslevel = 0; /* system minimum security level */
192 int valid_acct = -1; /* flag for reading valid acct */
193 char acct_name[MAXACID] = { "" }; /* used to read acct name */
194 struct jtab jtab; /* Job table struct */
195 struct udb ue; /* udb entry for logging-in user */
196 struct udb *up; /* pointer to UDB entry */
197 struct secstat secinfo; /* file security attributes */
198 struct servprov init_info; /* used for sesscntl() call */
199 int jid; /* job ID */
200 int pid; /* process ID */
201 char *sr; /* status return from setlimits() */
202 char *ttyn = NULL; /* ttyname or command name*/
203 char hostname[MAXHOSTNAMELEN];
204 /* passwd stuff for ia_user */
205 passwd_t pwdacm, pwddialup, pwdudb, pwdwal, pwddce;
206 ia_user_ret_t uret; /* stuff returned from ia_user */
207 ia_user_t usent; /* ia_user main structure */
208 int ia_rcode; /* ia_user return code */
209 ia_failure_t fsent; /* ia_failure structure */
210 ia_failure_ret_t fret; /* ia_failure return stuff */
211 ia_success_t ssent; /* ia_success structure */
212 ia_success_ret_t sret; /* ia_success return stuff */
213 int ia_mlsrcode; /* ia_mlsuser return code */
214 int secstatrc; /* [f]secstat return code */
216 if (SecureSys = (int)sysconf(_SC_CRAY_SECURE_SYS)) {
217 getsysv(&sysv, sizeof(struct sysv));
218 minslevel = sysv.sy_minlvl;
219 if (getusrv(&usrv) < 0)
220 fatal("getusrv() failed, errno = %d", errno);
224 (char *)get_canonical_hostname(options.use_dns),
227 * Fetch user's UDB entry.
230 if ((up = getudbnam(username)) == UDB_NULL)
231 fatal("cannot fetch user's UDB entry");
234 * Prevent any possible fudging so perform a data
235 * safety check and compare the supplied uid against
238 if (up->ue_uid != uid)
239 fatal("IA uid missmatch");
242 if ((jid = getjtab(&jtab)) < 0) {
250 secstatrc = secstat(ttyn, &secinfo);
252 secstatrc = fsecstat(1, &secinfo);
255 debug("[f]secstat() successful");
257 fatal("[f]secstat() error, rc = %d", secstatrc);
259 if ((ttyn == NULL) && ((char *)command != NULL))
260 ttyn = (char *)command;
262 * Initialize all structures to call ia_user
265 usent.uname = username;
266 usent.host = hostname;
268 usent.caller = IA_SSHD;
269 usent.pswdlist = &pwdacm;
271 usent.flags = IA_INTERACTIVE | IA_FFLAG;
272 pwdacm.atype = IA_SECURID;
274 pwdacm.next = &pwdudb;
276 pwdudb.atype = IA_UDB;
278 pwdudb.next = &pwddce;
280 pwddce.atype = IA_DCE;
282 pwddce.next = &pwddialup;
284 pwddialup.atype = IA_DIALUP;
285 pwddialup.pwdp = NULL;
286 /* pwddialup.next = &pwdwal; */
287 pwddialup.next = NULL;
289 pwdwal.atype = IA_WAL;
297 ia_rcode = ia_user(&usent, &uret);
300 * These are acceptable return codes from ia_user()
302 case IA_UDBWEEK: /* Password Expires in 1 week */
303 expiration_time = ue.ue_pwage.time + ue.ue_pwage.maxage;
304 printf ("WARNING - your current password will expire %s\n",
305 ctime((const time_t *)&expiration_time));
308 if (ttyname(0) != NULL) {
309 /* Force a password change */
310 printf("Your password has expired; Choose a new one.\n");
311 execl("/bin/passwd", "passwd", username, 0);
315 case IA_NORMAL: /* Normal Return Code */
318 /* XXX: can we memset it to zero here so save some of this */
319 strlcpy(ue.ue_name, "root", sizeof(ue.ue_name));
320 strlcpy(ue.ue_dir, "/", sizeof(ue.ue_dir));
321 strlcpy(ue.ue_shell, "/bin/sh", sizeof(ue.ue_shell));
323 ue.ue_passwd[0] = '\0';
325 ue.ue_comment[0] = '\0';
326 ue.ue_loghost[0] = '\0';
327 ue.ue_logline[0] = '\0';
330 ue.ue_nice[UDBRC_INTER] = 0;
332 for (i = 0; i < MAXVIDS; i++)
336 ue.ue_minlvl = ue.ue_maxlvl = ue.ue_deflvl = minslevel;
344 case IA_CONSOLE: /* Superuser not from Console */
345 case IA_TRUSTED: /* Trusted user */
346 if (options.permit_root_login > PERMIT_NO)
347 break; /* Accept root login */
350 * These are failed return codes from ia_user()
355 printf("Bad authorization, access denied.\n");
358 printf("Your login has been disabled. Contact the system ");
359 printf("administrator for assistance.\n");
362 printf("getsysv() failed - errno = %d\n", errno);
365 printf("Maximum number of failed login attempts exceeded.\n");
366 printf("Access denied.\n");
370 printf("NULL Password not allowed on MLS systems.\n");
377 * Authentication failed.
379 printf("sshd: Login incorrect, (0%o)\n",
380 ia_rcode-IA_ERRORCODE);
383 * Initialize structure for ia_failure
387 fsent.uname = username;
388 fsent.host = hostname;
390 fsent.caller = IA_SSHD;
391 fsent.flags = IA_INTERACTIVE;
394 fsent.errcode = ia_rcode;
395 fsent.pwdp = uret.pswd;
402 * Call ia_failure because of an IA failure.
403 * There is no return because ia_failure exits.
405 ia_failure(&fsent, &fret);
410 ia_mlsrcode = IA_NORMAL;
412 debug("calling ia_mlsuser()");
413 ia_mlsrcode = ia_mlsuser(&ue, &secinfo, &usrv, NULL, 0);
415 if (ia_mlsrcode != IA_NORMAL) {
416 printf("sshd: Login incorrect, (0%o)\n",
417 ia_mlsrcode-IA_ERRORCODE);
419 * Initialize structure for ia_failure
423 fsent.uname = username;
424 fsent.host = hostname;
426 fsent.caller = IA_SSHD;
427 fsent.flags = IA_INTERACTIVE;
430 fsent.errcode = ia_mlsrcode;
431 fsent.pwdp = uret.pswd;
437 * Call ia_failure because of an IA failure.
438 * There is no return because ia_failure exits.
440 ia_failure(&fsent,&fret);
444 /* Provide login status information */
445 if (options.print_lastlog && ue.ue_logtime != 0) {
446 printf("Last successful login was : %.*s ", 19,
447 (char *)ctime(&ue.ue_logtime));
449 if (*ue.ue_loghost != '\0') {
450 printf("from %.*s\n", sizeof(ue.ue_loghost),
453 printf("on %.*s\n", sizeof(ue.ue_logline),
457 if (SecureSys && (ue.ue_logfails != 0)) {
458 printf(" followed by %d failed attempts\n",
464 * Call ia_success to process successful I/A.
467 ssent.uname = username;
468 ssent.host = hostname;
470 ssent.caller = IA_SSHD;
471 ssent.flags = IA_INTERACTIVE;
474 ssent.errcode = ia_rcode;
476 ssent.time = 1; /* Set ue_logtime */
481 ia_success(&ssent, &sret);
484 * Query for account, iff > 1 valid acid & askacid permbit
486 if (((ue.ue_permbits & PERMBITS_ACCTID) ||
487 (ue.ue_acids[0] >= 0) && (ue.ue_acids[1] >= 0)) &&
488 ue.ue_permbits & PERMBITS_ASKACID) {
489 if (ttyname(0) != NULL) {
490 debug("cray_setup: ttyname true case, %.100s", ttyname);
491 while (valid_acct == -1) {
492 printf("Account (? for available accounts)"
493 " [%s]: ", acid2nam(ue.ue_acids[0]));
494 fgets(acct_name, MAXACID, stdin);
495 switch (acct_name[0]) {
500 valid_acct = ue.ue_acids[0];
501 strlcpy(acct_name, acid2nam(valid_acct), MAXACID);
504 /* Print the list 3 wide */
505 for (i = 0, j = 0; i < MAXVIDS; i++) {
506 if (ue.ue_acids[i] == -1) {
515 acid2nam(ue.ue_acids[i]));
517 if (ue.ue_permbits & PERMBITS_ACCTID) {
518 printf("\"acctid\" permbit also allows"
519 " you to select any valid "
525 valid_acct = nam2acid(acct_name);
526 if (valid_acct == -1)
528 "Account id not found for"
529 " account name \"%s\"\n\n",
534 * If an account was given, search the user's
535 * acids array to verify they can use this account.
537 if ((valid_acct != -1) &&
538 !(ue.ue_permbits & PERMBITS_ACCTID)) {
539 for (i = 0; i < MAXVIDS; i++) {
540 if (ue.ue_acids[i] == -1)
542 if (valid_acct == ue.ue_acids[i])
546 ue.ue_acids[i] == -1) {
547 fprintf(stderr, "Cannot set"
549 "\"%s\", permission "
550 "denied\n\n", acct_name);
557 * The client isn't connected to a terminal and can't
558 * respond to an acid prompt. Use default acid.
560 debug("cray_setup: ttyname false case, %.100s",
562 valid_acct = ue.ue_acids[0];
566 * The user doesn't have the askacid permbit set or
567 * only has one valid account to use.
569 valid_acct = ue.ue_acids[0];
571 if (acctid(0, valid_acct) < 0) {
572 printf ("Bad account id: %d\n", valid_acct);
577 * Now set shares, quotas, limits, including CPU time for the
578 * (interactive) job and process, and set up permissions
579 * (for chown etc), etc.
581 if (setshares(ue.ue_uid, valid_acct, printf, 0, 0)) {
582 printf("Unable to give %d shares to <%s>(%d/%d)\n",
583 ue.ue_shares, ue.ue_name, ue.ue_uid, valid_acct);
587 sr = setlimits(username, C_PROC, pid, UDBRC_INTER);
592 sr = setlimits(username, C_JOB, jid, UDBRC_INTER);
598 * Place the service provider information into
599 * the session table (Unicos) or job table (Unicos/mk).
600 * There exist double defines for the job/session table in
601 * unicos/mk (jtab.h) so no need for a compile time switch.
603 memset(&init_info, '\0', sizeof(init_info));
604 init_info.s_sessinit.si_id = URM_SPT_LOGIN;
605 init_info.s_sessinit.si_pid = getpid();
606 init_info.s_sessinit.si_sid = jid;
607 sesscntl(0, S_SETSERVPO, (int)&init_info);
610 * Set user and controlling tty security attributes.
613 if (setusrv(&usrv) == -1) {
614 debug("setusrv() failed, errno = %d",errno);
623 * The rc.* and /etc/sdaemon methods of starting a program on unicos/unicosmk
624 * can have pal privileges that sshd can inherit which
625 * could allow a user to su to root with out a password.
626 * This subroutine clears all privileges.
631 #if defined(_SC_CRAY_PRIV_SU)
632 priv_proc_t *privstate;
634 extern int priv_set_proc();
635 extern priv_proc_t *priv_init_proc();
638 * If ether of theses two flags are not set
639 * then don't allow this version of ssh to run.
641 if (!sysconf(_SC_CRAY_PRIV_SU))
642 fatal("Not PRIV_SU system.");
643 if (!sysconf(_SC_CRAY_POSIX_PRIV))
644 fatal("Not POSIX_PRIV.");
646 debug("Setting MLS labels.");;
648 if (sysconf(_SC_CRAY_SECURE_MAC)) {
649 usrv.sv_minlvl = SYSLOW;
650 usrv.sv_actlvl = SYSHIGH;
651 usrv.sv_maxlvl = SYSHIGH;
653 usrv.sv_minlvl = sysv.sy_minlvl;
654 usrv.sv_actlvl = sysv.sy_minlvl;
655 usrv.sv_maxlvl = sysv.sy_maxlvl;
658 usrv.sv_valcmp = sysv.sy_valcmp;
660 usrv.sv_intcat = TFM_SYSTEM;
661 usrv.sv_valcat |= (TFM_SYSTEM | TFM_SYSFILE);
663 if (setusrv(&usrv) < 0) {
664 fatal("%s(%d): setusrv(): %s", __FILE__, __LINE__,
668 if ((privstate = priv_init_proc()) != NULL) {
669 result = priv_set_proc(privstate);
671 fatal("%s(%d): priv_set_proc(): %s",
672 __FILE__, __LINE__, strerror(errno));
674 priv_free_proc(privstate);
676 debug ("Privileges should be cleared...");
678 /* XXX: do this differently */
679 # error Cray systems must be run with _SC_CRAY_PRIV_SU on!
685 * Retain utmp/wtmp information - used by cray accounting.
688 cray_retain_utmp(struct utmp *ut, int pid)
693 if ((fd = open(UTMP_FILE, O_RDONLY)) != -1) {
694 /* XXX use atomicio */
695 while (read(fd, (char *)&utmp, sizeof(utmp)) == sizeof(utmp)) {
696 if (pid == utmp.ut_pid) {
697 ut->ut_jid = utmp.ut_jid;
698 strncpy(ut->ut_tpath, utmp.ut_tpath, sizeof(utmp.ut_tpath));
699 strncpy(ut->ut_host, utmp.ut_host, sizeof(utmp.ut_host));
700 strncpy(ut->ut_name, utmp.ut_name, sizeof(utmp.ut_name));
706 fatal("Unable to open utmp file");
714 * find and delete jobs tmpdir.
717 cray_delete_tmpdir(char *login, int jid, uid_t uid)
719 static char jtmp[TPATHSIZ];
723 for (c = 'a'; c <= 'z'; c++) {
724 snprintf(jtmp, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
725 if (stat(jtmp, &statbuf) == 0 && statbuf.st_uid == uid)
732 if ((child = fork()) == 0) {
733 execl(CLEANTMPCMD, CLEANTMPCMD, login, jtmp, (char *)NULL);
734 fatal("cray_delete_tmpdir: execl of CLEANTMPCMD failed");
737 while (waitpid(child, &wstat, 0) == -1 && errno == EINTR)
742 * Remove tmpdir on job termination.
745 cray_job_termination_handler(int sig)
751 debug("received signal %d",sig);
753 if ((jid = waitjob(&jtab)) == -1 ||
754 (login = uid2nam(jtab.j_uid)) == NULL)
757 cray_delete_tmpdir(login, jid, jtab.j_uid);
761 * Set job id and create tmpdir directory.
764 cray_init_job(struct passwd *pw)
769 jid = setjob(pw->pw_uid, WJSIGNAL);
771 fatal("System call setjob failure");
773 for (c = 'a'; c <= 'z'; c++) {
774 snprintf(cray_tmpdir, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
775 if (mkdir(cray_tmpdir, JTMPMODE) != 0)
777 if (chown(cray_tmpdir, pw->pw_uid, pw->pw_gid) != 0) {
785 cray_tmpdir[0] = '\0';
789 cray_set_tmpdir(struct utmp *ut)
794 if ((jid = getjtab(&jbuf)) < 0)
798 * Set jid and tmpdir in utmp record.
801 strncpy(ut->ut_tpath, cray_tmpdir, TPATHSIZ);
808 * Set job id and create tmpdir directory.
811 cray_init_job(struct passwd *pw)
813 initrm_silent(pw->pw_uid);
816 #endif /* _UNICOSMP */