2 * $Id: bsd-cray.c,v 1.17 2007/08/15 09:17:43 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>
67 #include "sys/types.h"
69 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
70 # define _SS_MAXSIZE 128 /* Implementation specific max size */
71 # define _SS_PADSIZE (_SS_MAXSIZE - sizeof (struct sockaddr))
73 # define ss_family ss_sa.sa_family
74 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
76 #ifndef IN6_IS_ADDR_LOOPBACK
77 # define IN6_IS_ADDR_LOOPBACK(a) \
78 (((u_int32_t *) (a))[0] == 0 && ((u_int32_t *) (a))[1] == 0 && \
79 ((u_int32_t *) (a))[2] == 0 && ((u_int32_t *) (a))[3] == htonl (1))
80 #endif /* !IN6_IS_ADDR_LOOPBACK */
83 /* Define it to something that should never appear */
84 #define AF_INET6 AF_MAX
93 extern ServerOptions options
;
95 char cray_tmpdir
[TPATHSIZ
+ 1]; /* job TMPDIR path */
97 struct sysv sysv
; /* system security structure */
98 struct usrv usrv
; /* user security structure */
103 void cray_retain_utmp(struct utmp
*, int);
104 void cray_delete_tmpdir(char *, int, uid_t
);
105 void cray_init_job(struct passwd
*);
106 void cray_set_tmpdir(struct utmp
*);
107 void cray_login_failure(char *, int);
108 int cray_setup(uid_t
, char *, const char *);
109 int cray_access_denied(char *);
112 cray_login_failure(char *username
, int errcode
)
114 struct udb
*ueptr
; /* UDB pointer for username */
115 ia_failure_t fsent
; /* ia_failure structure */
116 ia_failure_ret_t fret
; /* ia_failure return stuff */
117 struct jtab jtab
; /* job table structure */
118 int jid
= 0; /* job id */
120 if ((jid
= getjtab(&jtab
)) < 0)
121 debug("cray_login_failure(): getjtab error");
124 if ((ueptr
= getudbnam(username
)) == UDB_NULL
)
125 debug("cray_login_failure(): getudbname() returned NULL");
128 memset(&fsent
, '\0', sizeof(fsent
));
130 fsent
.uname
= username
;
131 fsent
.host
= (char *)get_canonical_hostname(options
.use_dns
);
133 fsent
.caller
= IA_SSHD
;
134 fsent
.flags
= IA_INTERACTIVE
;
137 fsent
.errcode
= errcode
;
139 fsent
.exitcode
= 0; /* dont exit in ia_failure() */
145 * Call ia_failure because of an login failure.
147 ia_failure(&fsent
, &fret
);
154 cray_access_denied(char *username
)
156 struct udb
*ueptr
; /* UDB pointer for username */
157 int errcode
; /* IA errorcode */
161 if ((ueptr
= getudbnam(username
)) == UDB_NULL
)
162 debug("cray_login_failure(): getudbname() returned NULL");
165 if (ueptr
!= NULL
&& ueptr
->ue_disabled
)
166 errcode
= IA_DISABLED
;
168 cray_login_failure(username
, errcode
);
174 * record_failed_login: generic "login failed" interface function
177 record_failed_login(const char *user
, const char *hostname
, const char *ttyname
)
179 cray_login_failure((char *)user
, IA_UDBERR
);
183 cray_setup (uid_t uid
, char *username
, const char *command
)
185 extern struct udb
*getudb();
186 extern char *setlimits();
188 int err
; /* error return */
189 time_t system_time
; /* current system clock */
190 time_t expiration_time
; /* password expiration time */
191 int maxattempts
; /* maximum no. of failed login attempts */
192 int SecureSys
; /* unicos security flag */
193 int minslevel
= 0; /* system minimum security level */
195 int valid_acct
= -1; /* flag for reading valid acct */
196 char acct_name
[MAXACID
] = { "" }; /* used to read acct name */
197 struct jtab jtab
; /* Job table struct */
198 struct udb ue
; /* udb entry for logging-in user */
199 struct udb
*up
; /* pointer to UDB entry */
200 struct secstat secinfo
; /* file security attributes */
201 struct servprov init_info
; /* used for sesscntl() call */
202 int jid
; /* job ID */
203 int pid
; /* process ID */
204 char *sr
; /* status return from setlimits() */
205 char *ttyn
= NULL
; /* ttyname or command name*/
206 char hostname
[MAXHOSTNAMELEN
];
207 /* passwd stuff for ia_user */
208 passwd_t pwdacm
, pwddialup
, pwdudb
, pwdwal
, pwddce
;
209 ia_user_ret_t uret
; /* stuff returned from ia_user */
210 ia_user_t usent
; /* ia_user main structure */
211 int ia_rcode
; /* ia_user return code */
212 ia_failure_t fsent
; /* ia_failure structure */
213 ia_failure_ret_t fret
; /* ia_failure return stuff */
214 ia_success_t ssent
; /* ia_success structure */
215 ia_success_ret_t sret
; /* ia_success return stuff */
216 int ia_mlsrcode
; /* ia_mlsuser return code */
217 int secstatrc
; /* [f]secstat return code */
219 if (SecureSys
= (int)sysconf(_SC_CRAY_SECURE_SYS
)) {
220 getsysv(&sysv
, sizeof(struct sysv
));
221 minslevel
= sysv
.sy_minlvl
;
222 if (getusrv(&usrv
) < 0)
223 fatal("getusrv() failed, errno = %d", errno
);
227 (char *)get_canonical_hostname(options
.use_dns
),
230 * Fetch user's UDB entry.
233 if ((up
= getudbnam(username
)) == UDB_NULL
)
234 fatal("cannot fetch user's UDB entry");
237 * Prevent any possible fudging so perform a data
238 * safety check and compare the supplied uid against
241 if (up
->ue_uid
!= uid
)
242 fatal("IA uid missmatch");
245 if ((jid
= getjtab(&jtab
)) < 0) {
253 secstatrc
= secstat(ttyn
, &secinfo
);
255 secstatrc
= fsecstat(1, &secinfo
);
258 debug("[f]secstat() successful");
260 fatal("[f]secstat() error, rc = %d", secstatrc
);
262 if ((ttyn
== NULL
) && ((char *)command
!= NULL
))
263 ttyn
= (char *)command
;
265 * Initialize all structures to call ia_user
268 usent
.uname
= username
;
269 usent
.host
= hostname
;
271 usent
.caller
= IA_SSHD
;
272 usent
.pswdlist
= &pwdacm
;
274 usent
.flags
= IA_INTERACTIVE
| IA_FFLAG
;
275 pwdacm
.atype
= IA_SECURID
;
277 pwdacm
.next
= &pwdudb
;
279 pwdudb
.atype
= IA_UDB
;
281 pwdudb
.next
= &pwddce
;
283 pwddce
.atype
= IA_DCE
;
285 pwddce
.next
= &pwddialup
;
287 pwddialup
.atype
= IA_DIALUP
;
288 pwddialup
.pwdp
= NULL
;
289 /* pwddialup.next = &pwdwal; */
290 pwddialup
.next
= NULL
;
292 pwdwal
.atype
= IA_WAL
;
300 ia_rcode
= ia_user(&usent
, &uret
);
303 * These are acceptable return codes from ia_user()
305 case IA_UDBWEEK
: /* Password Expires in 1 week */
306 expiration_time
= ue
.ue_pwage
.time
+ ue
.ue_pwage
.maxage
;
307 printf ("WARNING - your current password will expire %s\n",
308 ctime((const time_t *)&expiration_time
));
311 if (ttyname(0) != NULL
) {
312 /* Force a password change */
313 printf("Your password has expired; Choose a new one.\n");
314 execl("/bin/passwd", "passwd", username
, 0);
318 case IA_NORMAL
: /* Normal Return Code */
321 /* XXX: can we memset it to zero here so save some of this */
322 strlcpy(ue
.ue_name
, "root", sizeof(ue
.ue_name
));
323 strlcpy(ue
.ue_dir
, "/", sizeof(ue
.ue_dir
));
324 strlcpy(ue
.ue_shell
, "/bin/sh", sizeof(ue
.ue_shell
));
326 ue
.ue_passwd
[0] = '\0';
328 ue
.ue_comment
[0] = '\0';
329 ue
.ue_loghost
[0] = '\0';
330 ue
.ue_logline
[0] = '\0';
333 ue
.ue_nice
[UDBRC_INTER
] = 0;
335 for (i
= 0; i
< MAXVIDS
; i
++)
339 ue
.ue_minlvl
= ue
.ue_maxlvl
= ue
.ue_deflvl
= minslevel
;
347 case IA_CONSOLE
: /* Superuser not from Console */
348 case IA_TRUSTED
: /* Trusted user */
349 if (options
.permit_root_login
> PERMIT_NO
)
350 break; /* Accept root login */
353 * These are failed return codes from ia_user()
358 printf("Bad authorization, access denied.\n");
361 printf("Your login has been disabled. Contact the system ");
362 printf("administrator for assistance.\n");
365 printf("getsysv() failed - errno = %d\n", errno
);
368 printf("Maximum number of failed login attempts exceeded.\n");
369 printf("Access denied.\n");
373 printf("NULL Password not allowed on MLS systems.\n");
380 * Authentication failed.
382 printf("sshd: Login incorrect, (0%o)\n",
383 ia_rcode
-IA_ERRORCODE
);
386 * Initialize structure for ia_failure
390 fsent
.uname
= username
;
391 fsent
.host
= hostname
;
393 fsent
.caller
= IA_SSHD
;
394 fsent
.flags
= IA_INTERACTIVE
;
397 fsent
.errcode
= ia_rcode
;
398 fsent
.pwdp
= uret
.pswd
;
405 * Call ia_failure because of an IA failure.
406 * There is no return because ia_failure exits.
408 ia_failure(&fsent
, &fret
);
413 ia_mlsrcode
= IA_NORMAL
;
415 debug("calling ia_mlsuser()");
416 ia_mlsrcode
= ia_mlsuser(&ue
, &secinfo
, &usrv
, NULL
, 0);
418 if (ia_mlsrcode
!= IA_NORMAL
) {
419 printf("sshd: Login incorrect, (0%o)\n",
420 ia_mlsrcode
-IA_ERRORCODE
);
422 * Initialize structure for ia_failure
426 fsent
.uname
= username
;
427 fsent
.host
= hostname
;
429 fsent
.caller
= IA_SSHD
;
430 fsent
.flags
= IA_INTERACTIVE
;
433 fsent
.errcode
= ia_mlsrcode
;
434 fsent
.pwdp
= uret
.pswd
;
440 * Call ia_failure because of an IA failure.
441 * There is no return because ia_failure exits.
443 ia_failure(&fsent
,&fret
);
447 /* Provide login status information */
448 if (options
.print_lastlog
&& ue
.ue_logtime
!= 0) {
449 printf("Last successful login was : %.*s ", 19,
450 (char *)ctime(&ue
.ue_logtime
));
452 if (*ue
.ue_loghost
!= '\0') {
453 printf("from %.*s\n", sizeof(ue
.ue_loghost
),
456 printf("on %.*s\n", sizeof(ue
.ue_logline
),
460 if (SecureSys
&& (ue
.ue_logfails
!= 0)) {
461 printf(" followed by %d failed attempts\n",
467 * Call ia_success to process successful I/A.
470 ssent
.uname
= username
;
471 ssent
.host
= hostname
;
473 ssent
.caller
= IA_SSHD
;
474 ssent
.flags
= IA_INTERACTIVE
;
477 ssent
.errcode
= ia_rcode
;
479 ssent
.time
= 1; /* Set ue_logtime */
484 ia_success(&ssent
, &sret
);
487 * Query for account, iff > 1 valid acid & askacid permbit
489 if (((ue
.ue_permbits
& PERMBITS_ACCTID
) ||
490 (ue
.ue_acids
[0] >= 0) && (ue
.ue_acids
[1] >= 0)) &&
491 ue
.ue_permbits
& PERMBITS_ASKACID
) {
492 if (ttyname(0) != NULL
) {
493 debug("cray_setup: ttyname true case, %.100s", ttyname
);
494 while (valid_acct
== -1) {
495 printf("Account (? for available accounts)"
496 " [%s]: ", acid2nam(ue
.ue_acids
[0]));
497 fgets(acct_name
, MAXACID
, stdin
);
498 switch (acct_name
[0]) {
503 valid_acct
= ue
.ue_acids
[0];
504 strlcpy(acct_name
, acid2nam(valid_acct
), MAXACID
);
507 /* Print the list 3 wide */
508 for (i
= 0, j
= 0; i
< MAXVIDS
; i
++) {
509 if (ue
.ue_acids
[i
] == -1) {
518 acid2nam(ue
.ue_acids
[i
]));
520 if (ue
.ue_permbits
& PERMBITS_ACCTID
) {
521 printf("\"acctid\" permbit also allows"
522 " you to select any valid "
528 valid_acct
= nam2acid(acct_name
);
529 if (valid_acct
== -1)
531 "Account id not found for"
532 " account name \"%s\"\n\n",
537 * If an account was given, search the user's
538 * acids array to verify they can use this account.
540 if ((valid_acct
!= -1) &&
541 !(ue
.ue_permbits
& PERMBITS_ACCTID
)) {
542 for (i
= 0; i
< MAXVIDS
; i
++) {
543 if (ue
.ue_acids
[i
] == -1)
545 if (valid_acct
== ue
.ue_acids
[i
])
549 ue
.ue_acids
[i
] == -1) {
550 fprintf(stderr
, "Cannot set"
552 "\"%s\", permission "
553 "denied\n\n", acct_name
);
560 * The client isn't connected to a terminal and can't
561 * respond to an acid prompt. Use default acid.
563 debug("cray_setup: ttyname false case, %.100s",
565 valid_acct
= ue
.ue_acids
[0];
569 * The user doesn't have the askacid permbit set or
570 * only has one valid account to use.
572 valid_acct
= ue
.ue_acids
[0];
574 if (acctid(0, valid_acct
) < 0) {
575 printf ("Bad account id: %d\n", valid_acct
);
580 * Now set shares, quotas, limits, including CPU time for the
581 * (interactive) job and process, and set up permissions
582 * (for chown etc), etc.
584 if (setshares(ue
.ue_uid
, valid_acct
, printf
, 0, 0)) {
585 printf("Unable to give %d shares to <%s>(%d/%d)\n",
586 ue
.ue_shares
, ue
.ue_name
, ue
.ue_uid
, valid_acct
);
590 sr
= setlimits(username
, C_PROC
, pid
, UDBRC_INTER
);
595 sr
= setlimits(username
, C_JOB
, jid
, UDBRC_INTER
);
601 * Place the service provider information into
602 * the session table (Unicos) or job table (Unicos/mk).
603 * There exist double defines for the job/session table in
604 * unicos/mk (jtab.h) so no need for a compile time switch.
606 memset(&init_info
, '\0', sizeof(init_info
));
607 init_info
.s_sessinit
.si_id
= URM_SPT_LOGIN
;
608 init_info
.s_sessinit
.si_pid
= getpid();
609 init_info
.s_sessinit
.si_sid
= jid
;
610 sesscntl(0, S_SETSERVPO
, (int)&init_info
);
613 * Set user and controlling tty security attributes.
616 if (setusrv(&usrv
) == -1) {
617 debug("setusrv() failed, errno = %d",errno
);
626 * The rc.* and /etc/sdaemon methods of starting a program on unicos/unicosmk
627 * can have pal privileges that sshd can inherit which
628 * could allow a user to su to root with out a password.
629 * This subroutine clears all privileges.
634 #if defined(_SC_CRAY_PRIV_SU)
635 priv_proc_t
*privstate
;
637 extern int priv_set_proc();
638 extern priv_proc_t
*priv_init_proc();
641 * If ether of theses two flags are not set
642 * then don't allow this version of ssh to run.
644 if (!sysconf(_SC_CRAY_PRIV_SU
))
645 fatal("Not PRIV_SU system.");
646 if (!sysconf(_SC_CRAY_POSIX_PRIV
))
647 fatal("Not POSIX_PRIV.");
649 debug("Setting MLS labels.");;
651 if (sysconf(_SC_CRAY_SECURE_MAC
)) {
652 usrv
.sv_minlvl
= SYSLOW
;
653 usrv
.sv_actlvl
= SYSHIGH
;
654 usrv
.sv_maxlvl
= SYSHIGH
;
656 usrv
.sv_minlvl
= sysv
.sy_minlvl
;
657 usrv
.sv_actlvl
= sysv
.sy_minlvl
;
658 usrv
.sv_maxlvl
= sysv
.sy_maxlvl
;
661 usrv
.sv_valcmp
= sysv
.sy_valcmp
;
663 usrv
.sv_intcat
= TFM_SYSTEM
;
664 usrv
.sv_valcat
|= (TFM_SYSTEM
| TFM_SYSFILE
);
666 if (setusrv(&usrv
) < 0) {
667 fatal("%s(%d): setusrv(): %s", __FILE__
, __LINE__
,
671 if ((privstate
= priv_init_proc()) != NULL
) {
672 result
= priv_set_proc(privstate
);
674 fatal("%s(%d): priv_set_proc(): %s",
675 __FILE__
, __LINE__
, strerror(errno
));
677 priv_free_proc(privstate
);
679 debug ("Privileges should be cleared...");
681 /* XXX: do this differently */
682 # error Cray systems must be run with _SC_CRAY_PRIV_SU on!
688 * Retain utmp/wtmp information - used by cray accounting.
691 cray_retain_utmp(struct utmp
*ut
, int pid
)
696 if ((fd
= open(UTMP_FILE
, O_RDONLY
)) != -1) {
697 /* XXX use atomicio */
698 while (read(fd
, (char *)&utmp
, sizeof(utmp
)) == sizeof(utmp
)) {
699 if (pid
== utmp
.ut_pid
) {
700 ut
->ut_jid
= utmp
.ut_jid
;
701 strncpy(ut
->ut_tpath
, utmp
.ut_tpath
, sizeof(utmp
.ut_tpath
));
702 strncpy(ut
->ut_host
, utmp
.ut_host
, sizeof(utmp
.ut_host
));
703 strncpy(ut
->ut_name
, utmp
.ut_name
, sizeof(utmp
.ut_name
));
709 fatal("Unable to open utmp file");
717 * find and delete jobs tmpdir.
720 cray_delete_tmpdir(char *login
, int jid
, uid_t uid
)
722 static char jtmp
[TPATHSIZ
];
726 for (c
= 'a'; c
<= 'z'; c
++) {
727 snprintf(jtmp
, TPATHSIZ
, "%s/jtmp.%06d%c", JTMPDIR
, jid
, c
);
728 if (stat(jtmp
, &statbuf
) == 0 && statbuf
.st_uid
== uid
)
735 if ((child
= fork()) == 0) {
736 execl(CLEANTMPCMD
, CLEANTMPCMD
, login
, jtmp
, (char *)NULL
);
737 fatal("cray_delete_tmpdir: execl of CLEANTMPCMD failed");
740 while (waitpid(child
, &wstat
, 0) == -1 && errno
== EINTR
)
745 * Remove tmpdir on job termination.
748 cray_job_termination_handler(int sig
)
754 if ((jid
= waitjob(&jtab
)) == -1 ||
755 (login
= uid2nam(jtab
.j_uid
)) == NULL
)
758 cray_delete_tmpdir(login
, jid
, jtab
.j_uid
);
762 * Set job id and create tmpdir directory.
765 cray_init_job(struct passwd
*pw
)
770 jid
= setjob(pw
->pw_uid
, WJSIGNAL
);
772 fatal("System call setjob failure");
774 for (c
= 'a'; c
<= 'z'; c
++) {
775 snprintf(cray_tmpdir
, TPATHSIZ
, "%s/jtmp.%06d%c", JTMPDIR
, jid
, c
);
776 if (mkdir(cray_tmpdir
, JTMPMODE
) != 0)
778 if (chown(cray_tmpdir
, pw
->pw_uid
, pw
->pw_gid
) != 0) {
786 cray_tmpdir
[0] = '\0';
790 cray_set_tmpdir(struct utmp
*ut
)
795 if ((jid
= getjtab(&jbuf
)) < 0)
799 * Set jid and tmpdir in utmp record.
802 strncpy(ut
->ut_tpath
, cray_tmpdir
, TPATHSIZ
);
809 * Set job id and create tmpdir directory.
812 cray_init_job(struct passwd
*pw
)
814 initrm_silent(pw
->pw_uid
);
817 #endif /* _UNICOSMP */