/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
/*
* This code has a lot in common with the original sys-suspend
* code. Windowing facilities have been removed, and it has been
* updated to use more recent API's.
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#include <utility.h>
#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include <pwd.h>
#include <syslog.h>
#include <auth_attr.h>
#include <auth_list.h>
#include <secdb.h>
#include <security/pam_appl.h>
#include <utmpx.h>
/* For audit */
#include <bsm/adt_event.h>
#include <dirent.h>
/* STATICUSED */
/*
* Authorizations used by Power Management
*/
/* Platform specific definitions */
#ifdef i386
#else
#endif
static int flags = 0;
static int no_tty = 0;
/*
* Flag definitions - could go in a header file, but there are just a few
*/
static char **argvl;
/*
* Forward Declarations.
*/
static void pm_poweroff(void);
static int bringto_lowpower(void);
static int is_mou3(void);
static void suspend_error(int);
static int pm_check_suspend(void);
static void pm_suspend(void);
static void pm_do_auth(adt_session_data_t *);
/*
* External Declarations.
*/
extern int pam_tty_conv(int, struct pam_message **,
struct pam_response **, void *);
extern char *optarg;
/*
* Audit related code. I would also think that some of this could be
* in external code, as they could be useful of other apps.
*/
/*
* Write audit event. Could be useful in the PM library, so it is
* included here. For the most part it is only used by the PAM code.
*/
static void
{
return;
}
(void) adt_put_event(event,
}
static int
change_audit_file(void)
{
if (!adt_audit_state(AUC_AUDITING)) {
/* auditd not running, just return */
return (0);
}
_exit(-1);
} else if (pid == -1) {
return (-1);
} else {
/*
* Wait for audit(1M) -n process to complete
*
*/
do {
return (0);
} else if (rc == -1) {
return (-1);
} else {
(void) sleep(1);
retries--;
}
} while (retries != 0);
}
return (-1);
}
static void
{
break;
}
(void) sleep(1);
}
}
/* End of Audit-related code */
/* ARGSUSED0 */
static void
{
}
/*
* These are functions that would be candidates for moving to a library.
*/
/*
* pm_poweroff - similar to poweroff(1M)
* This should do the same auditing as poweroff(1m) would do when it
* becomes a libpower function. Till then we use poweroff(1m).
*/
static void
pm_poweroff(void)
{
"authorizations to shutdown this machine.\n"), user);
exit(1);
}
closelog();
/*
* Call poweroff(1m) to shut down the system.
*/
}
/*
* on this machine.
* Ultimately, we would prefer to get the "default" suspend type from
* a PM property or some other API, but for now, we know that STR is
* only available on x86 and STD is only available on Sparc. It does
* make this function quite easy, though.
*/
static int
pm_check_suspend(void) {
/*
* Use the uadmin(2) "CHECK" command to see if suspend is supported
*/
}
/*
* This entry point _should_ be the common entry to suspend. It is in
* it's entirety here, but would be best moved to libpower when that
* is available.
*/
static void
pm_suspend(void)
{
/*
* Does the user have permission to use this command?
*/
"authorizations to suspend this machine.\n"), user);
exit(1);
}
if (bringto_lowpower() == -1) {
exit(1);
}
/*
* Test mode, do checks as if a real suspend, but
* don't actually do the suspend.
*/
/* Check if suspend is supported */
if (pm_check_suspend() == -1) {
}
" performed\n"));
} else {
/* Check if suspend is supported */
if (pm_check_suspend() == -1) {
}
/*
* We are about to suspend this machine, try and
* lock the screen. We don't really care if this
* succeeds or not, but that we actually tried. We
* also know that we have sufficient privileges to
* be here, so we lock the screen now, even if
* suspend actually fails.
* Note that garbage is sometimes displayed, and
* we don't really care about it, so we toss all
* text response.
* it would also be good if there were another option
* instead of launcing a file, as the disk might be
* spun down if we are suspending due to idle.
*/
(void) system("/usr/bin/xdg-screensaver lock "
}
/* Time to do the actual deed! */
/*
* Before we actually suspend, we need to audit and
* "suspend" the audit files.
*/
/* set up audit session and event */
"%s: can't put audit event\n"),
argvl[0]);
} else {
}
}
(void) change_audit_file();
} else {
"%s: can't start audit session\n"), argvl[0]);
}
/*
* Note, that if we actually poweroff,
* that the poweroff function will handle
* that audit trail, and the resume
* trail is effectively done.
*/
pm_poweroff();
} else {
/* suspend_error() will exit. */
/*
* Audit the suspend failure and
* reuse the event, but don't create one
* if we don't already have one.
*/
(void) adt_put_event(event,
ADT_FAILURE, 0);
}
}
}
/*
* Write the thaw event.
*/
== NULL)) {
"%s: can't allocate thaw audit event\n"),
argvl[0]);
} else {
"%s: can't put thaw audit event\n"),
argvl[0]);
}
(void) adt_free_event(event);
}
}
}
pm_do_auth(ah);
}
(void) adt_end_session(ah);
}
/* End of "library" functions */
/*
* Print an appropriate error message and exit.
*/
static void
switch (error) {
case EBUSY:
"Suspend already in progress.\n\n"));
exit(1);
/*NOTREACHED*/
case ENOMEM:
/*FALLTHROUGH*/
case ENOSPC:
"Not enough resources to suspend.\n\n"));
exit(1);
/*NOTREACHED*/
case ENOTSUP:
"Suspend is not supported.\n\n"));
exit(1);
/*NOTREACHED*/
case EPERM:
"Not sufficient privileges.\n\n"));
exit(1);
/*NOTREACHED*/
default:
"unknown error.\n\n"));
exit(1);
}
}
/*
* refresh_dt() - Refresh screen when 'dtgreet' is running.
* This is here for compatibility reasons, and could be removed once
* dtgreet is no longer part of the system.
*/
static int
{
int status;
/*
* If dtgreet exists, HUP it, otherwise just let screenlock
* do it's thing.
*/
switch (fork()) {
case -1:
break;
case 0:
(void) close(1);
break;
default:
}
}
return (0);
}
/*
* On enter, the "xauthority" string has the value "XAUTHORITY=". On
* return, if a Xauthority file is found, concatenate it to this string,
* otherwise, return "xauthority" as it is.
*/
static char *
{
char *home_dir;
/*
* Determine home directory of the user.
*/
"entry for user.\n"));
exit(1);
}
}
"long.\n"));
exit(1);
}
/*
* If there is a .Xauthority file in home directory, reference it.
*/
/*LINTED*/
/*
* If Xsession can not access user's home directory, it creates the
* name of the Xauthority is not known, search the directory and
* find the last changed file that starts with ".Xauth" and owned
* by the user. Hopefully, that is the valid Xauthority file for
* the current X session.
*/
return (xauthority);
/*LINTED*/
continue;
continue;
}
}
}
}
/*
* suspend can be called in following ways:
* 1. from daemon (powerd) for auto-shutdown.
* b. there might not be any windowing environment
* 2. by a user entered command.
* b. the command can be entered by a user logged in on a dumb
* terminal.
* and we have permission to talk to it.
* don't have permission to talk to it or console
* itself is the dumb terminal we have logged into.
*
* In main(), we decide on the correct case and call appropriate functions.
*/
int
{
int c;
/*
* If suspend is invoked from a daemon (case 1 above), it
* will not have a working stdin, stdout and stderr. We need
* these to print proper error messages and possibly get user
* input. We attach them to console and hope that attachment
* works.
*/
no_tty = 1;
}
switch (c) {
case 'f':
/*
* Force machine to poweroff if
* suspend fails
*/
break;
case 'n':
/* No warning popups - Obsolete */
break;
case 'x':
/* Don't try to screenlock */
break;
case 'h':
/* Do a shutdown instead of suspend */
break;
case 'd':
/* Needswork */
/* Set the DISPLAY value in the environment */
"display name is too long.\n"));
return (1);
}
if (putenv(display_name) != 0) {
"unable to set DISPLAY "
"environment variable.\n"));
return (1);
}
break;
case 't':
/* Test, don't actually do any operation */
break;
default:
"[-fnxh] [-d <display>]\n"));
return (1);
}
}
/*
* The action of pressing power key and power button on a MOU-3 machine
* causes suspend being invoked with SYSSUSPENDDODEFAULT
* enviromental variable set - indicating the default action is machine
* dependent: for MOU-3 type machine, "LowPower" mode is the default,
* for all the rest, "Suspend" is the default. Existing suspend
* flags works the same.
*/
if (getenv("SYSSUSPENDDODEFAULT"))
if (is_mou3())
/*
* Flag "-h" overrides flag "-f".
*/
/*
* Check initally if the user has the authorizations to
* do either a suspend or shutdown. pm_suspend() will also
* make this test, so we could defer till then, but if we
* do it now, we at least prevent a lot of unneeded setup.
*/
"authorizations to shutdown the machine.\n"));
exit(1);
}
"authorizations to suspend.\n"));
exit(1);
}
/*
* If we are only shutting down, there isn't much to do, just
* call pm_poweroff(), and let it do all the work.
*/
/*
* pm_poweroff either powers off or exits,
* so there is no return.
*/
(void) printf("TEST: This machine would have "
"powered off\n");
exit(1);
} else {
pm_poweroff();
}
/* NOTREACHED */
}
/*
* If XAUTHORITY environment variable is not set, try to set
* one up.
*/
/*
* In case of "suspend" being called from daemon "powerd",
* signal SIGALRM is blocked so use "sigset()" instead of "signal()".
*/
/* Call the "suspend" function to do the last of the work */
pm_suspend();
if (refresh_dt() == -1) {
return (1);
}
return (0);
}
/*
* Note that some of these functions are more relevant to Sparc platforms,
* but they do function properly on other platforms, they just don't do
* as much.
*/
/*
* bringto_lowpower()
* This tells the PM framework to put the devices it controls in an idle
* state. The framework only complains if a device that *must* be idle
* doesn't succeed in getting there.
*/
static int
{
int fd;
return (-1);
}
"to low power mode.\n"));
return (-1);
}
return (0);
}
/*
* Though this test is predominantly used on Sparc, it will run on other
* platforms, and might be usefull one day on those.
*/
static int
is_mou3()
{
int fd;
int found = 0;
return (found);
}
} else {
}
return (found);
}
/*
* Reauthenticate the user on return from suspend.
* This is here and not in the PAM-specific file, as there are
* items specific to sys-suspend, and not generic to PAM. This may
* become part of a future PM library. The audit handle is passed,
* as the pm_suspend code actually starts an audit session, so it
* makes sense to just continue to use it. If it were separated
* from the pm_suspend code, it will need to open a new session.
*/
static void
{
int err;
int pam_flag = 0;
int chpasswd_tries;
if (user[0] == '\0')
return;
&pm_pamh)) != PAM_SUCCESS)
return;
do {
if (err == PAM_SUCCESS) {
if (err == PAM_NEW_AUTHTOK_REQD) {
chpasswd_tries = 0;
do {
} while ((err == PAM_AUTHTOK_ERR ||
err == PAM_TRY_AGAIN) &&
}
}
if (err != PAM_SUCCESS) {
}
} while (err != PAM_SUCCESS);
}