Sisyphus repository
Last update: 1 october 2023 | SRPMs: 18631 | Visits: 37377023
en ru br
ALT Linux repos
S:1.1.0-alt1
5.0: 0.0.1-alt4
4.1: 0.0.1-alt3
4.0: 0.0.1-alt3

Group :: Terminals
RPM: consolelocker

 Main   Changelog   Spec   Patches   Sources   Download   Gear   Bugs and FR  Repocop 

pax_global_header00006660000000000000000000000064000000000000014053gustar00gitgit0000000000000052 comment=123b2b86825a1b25914e50e87514599a2b511030
consolelocker-0.0.1/000075500000000000000000000000001051711573600137565ustar00gitgit00000000000000consolelocker-0.0.1/.gear-rules000064400000000000000000000000071051711573600160220ustar00gitgit00000000000000tar: .
consolelocker-0.0.1/COPYING000064400000000000000000000431051051711573600150140ustar00gitgit00000000000000 GNU GENERAL PUBLIC LICENSE
Version 2, June 1991

Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Preamble

The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.

When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.

We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

The precise terms and conditions for copying, distribution and
modification follow.

GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.

b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.

c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)

These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,

b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,

c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.

9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

NO WARRANTY

11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

END OF TERMS AND CONDITIONS

How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:

Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.

<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
consolelocker-0.0.1/Makefile000064400000000000000000000025241051711573600154210ustar00gitgit00000000000000PROJECT = consolelocker
VERSION = 0.0.1
WRAPPERS = vlocka
MAN8PAGES = $(PROJECT).8
TARGETS = $(PROJECT) $(WRAPPERS) $(MAN8PAGES)

sbindir = /usr/sbin
helperdir = /usr/libexec/consolelocker
DESTDIR =

CC = gcc
MKDIR_P = mkdir -p
INSTALL = install
HELP2MAN8 = help2man -N -s8

WARNINGS = -Wall -Wextra -Werror -W -Wshadow -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wconversion -Waggregate-return -Wstrict-prototypes \
-Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn \
-Wmissing-format-attribute -Wredundant-decls -Wdisabled-optimization

CPPFLAGS = -std=gnu99 $(WARNINGS) -D_GNU_SOURCE -DPROJECT_VERSION=\"$(VERSION)\" \
-DVLOCK_WRAPPER=\"$(helperdir)/vlocka\"

CFLAGS = -I. -pipe -O2

SRC = pidfile.c consolelocker.c
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)

.PHONY: all install clean

all: $(TARGETS)

$(PROJECT): $(OBJ)
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

install: all
$(MKDIR_P) -m755 $(DESTDIR)$(sbindir)
$(INSTALL) -p -m700 $(PROJECT) $(DESTDIR)$(sbindir)/
$(MKDIR_P) -m755 $(DESTDIR)$(helperdir)
$(INSTALL) -p -m700 $(WRAPPERS) $(DESTDIR)$(helperdir)/

clean:
$(RM) $(TARGETS) $(DEP) $(OBJ) core *~

%.8: % %.8.inc Makefile
$(HELP2MAN8) -i $@.inc ./$< >$@

ifneq ($(MAKECMDGOALS),clean)
%.d: %.c Makefile
@echo Making dependences for $<
@$(CC) -MM $(CPPFLAGS) $< |sed -e 's,\($*\)\.o[ :]*,\1.o $@: Makefile ,g' >$@
endif
consolelocker-0.0.1/TODO000064400000000000000000000000151051711573600144420ustar00gitgit00000000000000- Add syslog.consolelocker-0.0.1/consolelock000075500000000000000000000000641051711573600162170ustar00gitgit00000000000000#!/bin/sh -efu
echo > "/var/run/consolelocker.fifo"
consolelocker-0.0.1/consolelocker.8.inc000064400000000000000000000017541051711573600174700ustar00gitgit00000000000000.\" Copyright (C) 2006 Alexey Gladkov <legion@altlinux.org>
.\"
.\" This file is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 2 of the License, or
.\" (at your option) any later version.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

[NAME]
\fBconsolelocker\fR \- virtual console lock daemon

[FILES]
.TP
.I /var/run/consolelocker.fifo
.TP
\fI/var/run/consolelocker.pid
.TP
\fI/var/run/console.lock

[SEE ALSO]
.BR vlock (1),
.BR pam_console (8).
consolelocker-0.0.1/consolelocker.c000064400000000000000000000221531051711573600167670ustar00gitgit00000000000000/* -*- mode: Shell-script; tab-width: 8; fill-column: 70; -*- */
/***** BEGIN LICENSE BLOCK *****
* Copyright (C) 2006 Alexey Gladkov <legion@altlinux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
***** END LICENSE BLOCK ******/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <getopt.h>
#include <sys/wait.h>

#include <errno.h>
#include <error.h>

#include "pidfile.h"

#define setprogname(pn)
extern const char *__progname;

enum states {
STATE_READY, // program ready for commands.
STATE_PASSIVE, // program not listen any commands.
STATE_LOCKED, // program was lock console.
};

const char fifo_path[] = "/var/run/consolelocker.fifo";
const char consolelock_path[] = "/var/run/console/console.lock";
const char pidfile_path[] = "/var/run/consolelocker.pid";
const char sysrq_path[] = "/proc/sys/kernel/sysrq";

volatile pid_t lock_process;
volatile enum states state;
volatile int goto_finish;
volatile int sysrq;

const uid_t def_fifo_owner = 0;
const gid_t def_fifo_group = 0;

char *consoleowner = NULL;
uid_t fifo_owner;
gid_t fifo_group;

static void __attribute__ ((noreturn))
print_help(int ret) {
printf("Usage: %s [OPTIONS]\n\n"
"This is a program to lock sessions on the Linux console\n"
"and virtual consoles. After startup it open FIFO to read\n"
"events from console user.\n"
"This program is simple wrapper for vlock(1).\n\n"
"Options:\n"
" -D, --nodaemon When this option is specified sshd will\n"
" not detach and does not become a daemon.\n"
" -V, --version print program version and exit.\n"
" -h, --help output a brief help message.\n\n",
__progname);
exit(ret);
}

static void __attribute__ ((noreturn))
print_version(void) {
printf("%s version %s\n\n"
"Copyright (C) 2006 Alexey Gladkov <legion@altlinux.org>\n\n"
"This is free software; see the source for copying conditions.\n"
"There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
"Written by Alexey Gladkov <legion@altlinux.org>\n",
__progname, PROJECT_VERSION);
exit(EXIT_SUCCESS);
}

static int
get_sysrq(void) {
FILE *fd;
int cur_sysrq;

if ((fd = fopen(sysrq_path, "r")) == NULL) {
fprintf(stderr, "fopen: %s\n", strerror(errno));
return -1;
}

if ((fscanf(fd, "%d", &cur_sysrq)) != 1) {
fprintf(stderr, "fscanf: %s\n", strerror(errno));
cur_sysrq = -1;
}

if ((fclose(fd)) != 0) {
fprintf(stderr, "fclose: %s\n", strerror(errno));
cur_sysrq = -1;
}
return cur_sysrq;
}

static int
set_sysrq(int new_sysrq) {
FILE *fd;
int old_sysrq = -1;

if ((old_sysrq = get_sysrq()) == -1)
return -1;

if (new_sysrq == old_sysrq)
return 1;

if ((fd = fopen(sysrq_path, "w")) == NULL) {
fprintf(stderr, "fopen: %s\n", strerror(errno));
return -1;
}

if (!fprintf(fd,"%d\n", new_sysrq)) {
fprintf(stderr, "fprintf: %s\n", strerror(errno));
fclose(fd);
return -1;
}
fflush(fd);

if ((fclose(fd)) != 0) {
fprintf(stderr, "fclose: %s\n", strerror(errno));
return -1;
}
return 1;
}

static void
sigchld_handler(int sig __attribute__((__unused__))) {
int status, retval;

if ((retval = waitpid(lock_process, &status, WNOHANG|WUNTRACED)) == -1)
error(EXIT_FAILURE, errno, "waitpid");

if (retval == 0)
return;

if ((set_sysrq(sysrq)) == -1)
error(EXIT_FAILURE, errno, "set_sysrq");

if (state != STATE_READY)
state = STATE_READY;

if (lock_process)
lock_process = -1;
}

static void
exit_handler(int sig __attribute__((__unused__))) {
goto_finish = 1;
}

static void
readgroup(const char *filename) {
FILE *fd;
struct passwd *pw = NULL;
size_t len = 0;

if ((fd = fopen(filename, "r")) == NULL)
error(EXIT_FAILURE, errno, "fopen");

if (consoleowner)
free(consoleowner);

if ((getline(&consoleowner, &len, fd) == -1))
error(EXIT_FAILURE, 0, "getline: unable read username");

if ((pw = getpwnam(consoleowner)) != NULL) {
fifo_group = pw->pw_gid;
} else
error(EXIT_FAILURE, errno, "getpwnam");

if ((fclose(fd)) != 0)
error(EXIT_FAILURE, errno, "fclose");
}

static pid_t
authorize(void) {
pid_t pid;

if ((pid = fork()) == -1)
error(EXIT_FAILURE, errno, "fork");
if (pid == 0) {
if ((setsid()) == -1)
error(EXIT_FAILURE, errno, "setsid");

if ((execl("/usr/bin/openvt",
"openvt", "-s", "-l", "-w", "--", VLOCK_WRAPPER, consoleowner,
(char *) NULL)) == -1)
error(EXIT_FAILURE, errno, "execl");
}
return pid;
}

static int
exist(const char *filename) {
if (access(filename, R_OK) == -1) {
if (errno == ENOENT)
return 0;
error(EXIT_FAILURE, errno, "access");
}
return 1;
}

static long int
read_retry(int fd) {
char c;
long int retval;
do retval = read(fd, &c, sizeof(c));
while (retval == -1 && (errno == EINTR || errno == EAGAIN));
return retval;
}

static int
daemonize(void) {
int reopen_fifo = 1, fd = -1, rc = EXIT_SUCCESS;
fd_set rfds;
struct timeval tv;

/* Create pidfile */
if ((write_pid(pidfile_path)) == 0)
error(EXIT_FAILURE, 0, "write_pid failed");

/* Register SIGCHLD handler */
signal(SIGCHLD, sigchld_handler);

/* Register SIGTERM handler */
signal(SIGTERM, exit_handler);

/* Block other signals */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGPIPE, SIG_IGN);

goto_finish = 0;
lock_process = -1;
state = STATE_PASSIVE;

#define xassert_loop(x, y) if (x == -1) { \
error(0, errno, y); \
rc = EXIT_FAILURE; \
goto fail; \
}

while (goto_finish == 0) {
long int retval;

if (!exist(consolelock_path)) {
if (state == STATE_LOCKED && lock_process > 0) {
#if 0
/* Kill vlock process, if nobody logged in. */
pid_t pg_pid;
xassert_loop((pg_pid = getpgid(lock_process)), "getpgid");
xassert_loop ((killpg(pg_pid, SIGKILL)), "killpg");
#endif
lock_process = -1;
}

xassert_loop((chown(fifo_path, def_fifo_owner, def_fifo_group)), "chown");
state = STATE_PASSIVE;
} else if (state == STATE_PASSIVE) {
readgroup(consolelock_path);

xassert_loop((chown(fifo_path, fifo_owner, fifo_group)), "chown");
state = STATE_READY;
}

/* Open the FIFO */
if (reopen_fifo == 1) {
xassert_loop((fd != -1 && (close(fd)) == -1), "close");
xassert_loop((fd = open(fifo_path, O_RDONLY | O_NONBLOCK)), "open");
reopen_fifo = 0;
}

/* Watch to see when it has input. */
FD_ZERO(&rfds);
FD_SET(fd, &rfds);

/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;

retval = -1;
errno = EINTR;
while (retval < 0 && errno == EINTR)
retval = select((fd + 1), &rfds, NULL, NULL, &tv);

xassert_loop(retval, "select");

if (retval == 0)
continue;

while ((read_retry(fd)) == 1) {
if (state != STATE_READY)
continue;
state = STATE_LOCKED;

xassert_loop((sysrq = get_sysrq()), "get_sysrq");
if (sysrq > 0)
set_sysrq(0);

lock_process = authorize();
}

xassert_loop(retval, "read");
reopen_fifo = 1;
}
fail:
if (consoleowner)
free(consoleowner);

if ((set_sysrq(sysrq)) == -1)
error(EXIT_FAILURE, errno, "set_sysrq");

if ((chown(fifo_path, def_fifo_owner, def_fifo_group)) == -1)
error(EXIT_FAILURE, errno, "chown");

if ((remove_pid(pidfile_path)) == -1)
error(EXIT_FAILURE, errno, "remove_pid");

return rc;
}

int
main(int argc, char ** argv) {
int c, nodaemon = 0;
mode_t prev_umask;

static struct option long_options[] = {
{"nodaemon", 0, 0, 'D'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{0, 0, 0, 0}
};

setprogname(argv[0]);
while (1) {
int option_index = 0;
c = getopt_long (argc, argv, "DhV", long_options, &option_index);
if (c == -1)
break;

switch (c) {
case 'D': nodaemon = 1; break;
case 'V': print_version(); break;
default:
case 'h': print_help(EXIT_SUCCESS); break;
}
}

if (getuid())
error(EXIT_FAILURE, 0, "must be root");

if (check_pid(pidfile_path))
error(EXIT_FAILURE, 0, "pidfile already exist");

/* Remove old FIFO */
if (exist(fifo_path) && (unlink(fifo_path)) == -1)
error(EXIT_FAILURE, errno, "unlink");

/* Create the FIFO */
prev_umask = umask(046);
if ((mkfifo(fifo_path, 0620)) == -1)
error(EXIT_FAILURE, errno, "mkfifo");
umask(prev_umask);

/* Change owner/group to default values */
if ((chown(fifo_path, def_fifo_owner, def_fifo_group)) == -1)
error(EXIT_FAILURE, errno, "chown");

if (nodaemon == 0 && daemon(0, 0) == -1)
error(EXIT_FAILURE, errno, "daemon");

return daemonize();
}
consolelocker-0.0.1/consolelocker.cronjob000064400000000000000000000000361051711573600201750ustar00gitgit00000000000000*/45 * * * * root consolelock
consolelocker-0.0.1/consolelocker.service000075500000000000000000000014431051711573600202070ustar00gitgit00000000000000#!/bin/sh
#
# Initialize consolelocker.
#
# chkconfig: 345 39 55
# description: This package loads consolelocker.

# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/consolelocker.pid
RETVAL=0

start()
{
start_daemon --pidfile "$PIDFILE" --user root --name consolelocker -- consolelocker
RETVAL=$?
return $RETVAL
}

stop()
{
msg_stopping $"consolelocker"
stop_daemon --pidfile "$PIDFILE" --name consolelocker -TERM -- consolelocker
RETVAL=$?
return $RETVAL
}

restart()
{
stop
start
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload|restart)
restart
;;
status)
status --pidfile "$PIDFILE" --name consolelocker -- consolelocker
RETVAL=$?
;;
*)
msg_usage "${0##*/} {start|stop|restart|status}"
RETVAL=1
esac

exit $RETVAL
consolelocker-0.0.1/consolelocker.spec000064400000000000000000000022601051711573600174740ustar00gitgit00000000000000Name: consolelocker
Version: 0.0.1
Release: alt3

Summary: Daemon for lock console terminal and virtual consoles.
License: GPL
Group: Terminals
Packager: Alexey Gladkov <legion@altlinux.org>

Source: %name-%version.tar

Requires: vlock console-vt-tools

# Automatically added by buildreq on Fri Oct 20 2006 (-bi)
BuildRequires: help2man

%description
This package contains a daemon for lock console terminal and virtual consoles.

%prep
%setup -q

%build
%make_build

%install
%make_install install DESTDIR=%buildroot
%__mkdir_p %buildroot/%_bindir
%__install -D -m755 consolelock %buildroot/%_bindir/
%__install -D -m644 %name.cronjob %buildroot/%_sysconfdir/cron.d/%name
%__install -D -m755 %name.service %buildroot/%_initrddir/%name

%files
%_bindir/*
%_sbindir/*
%_initrddir/*
%_usr/libexec/%name
%config(noreplace) %_sysconfdir/cron.d/%name

%changelog
* Mon Oct 23 2006 Alexey Gladkov <legion@altlinux.ru> 0.0.1-alt3
- Add wrapper for vlock.
- consolelocker.service: Change start and stop priority levels.

* Sun Oct 22 2006 Alexey Gladkov <legion@altlinux.ru> 0.0.1-alt2
- Add block sysrq.
- Fix copyright.

* Tue Oct 10 2006 Alexey Gladkov <legion@altlinux.ru> 0.0.1-alt1
- Initial revision.
consolelocker-0.0.1/pidfile.c000064400000000000000000000062661051711573600155500ustar00gitgit00000000000000/*
pidfile.c - interact with pidfiles
Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>

This file is part of the sysklogd package, a kernel and system log daemon.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
*/

/*
* Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze
* First version (v0.2) released
*/

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <string.h>
#include <errno.h>
#include <signal.h>

#include "pidfile.h"

/* read_pid
*
* Reads the specified pidfile and returns the read pid.
* 0 is returned if either there's no pidfile, it's empty
* or no pid can be read.
*/
int read_pid (const char *pidfile)
{
FILE *f;
int pid;

if (!(f=fopen(pidfile,"r")))
return 0;
if (fscanf(f, "%d", &pid) != 1)
pid = 0;
fclose(f);
return pid;
}

/* check_pid
*
* Reads the pid using read_pid and looks up the pid in the process
* table (using /proc) to determine if the process already exists. If
* so 1 is returned, otherwise 0.
*/
int check_pid (const char *pidfile)
{
int pid = read_pid(pidfile);

/* Amazing ! _I_ am already holding the pid file... */
if ((!pid) || (pid == getpid ()))
return 0;

/*
* The 'standard' method of doing this is to try and do a 'fake' kill
* of the process. If an ESRCH error is returned the process cannot
* be found -- GW
*/
/* But... errno is usually changed only on error.. */
if (kill(pid, 0) && errno == ESRCH)
return(0);

return pid;
}

/* write_pid
*
* Writes the pid to the specified file. If that fails 0 is
* returned, otherwise the pid.
*/
int write_pid (const char *pidfile)
{
FILE *f;
int fd;
int pid;

if ( ((fd = open(pidfile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1)
|| ((f = fdopen(fd, "r+")) == NULL) ) {
fprintf(stderr, "Can't open or create %s.\n", pidfile);
return 0;
}

if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
if (fscanf(f, "%d", &pid) != 1)
pid = 0;
fclose(f);
printf("Can't lock, lock is held by pid %d.\n", pid);
return 0;
}

pid = getpid();
if (!fprintf(f,"%d\n", pid)) {
printf("Can't write pid , %s.\n", strerror(errno));
close(fd);
return 0;
}
fflush(f);

if (flock(fd, LOCK_UN) == -1) {
printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
close(fd);
return 0;
}
close(fd);

return pid;
}

/* remove_pid
*
* Remove the the specified file. The result from unlink(2)
* is returned
*/
int remove_pid (const char *pidfile)
{
return unlink (pidfile);
}

consolelocker-0.0.1/pidfile.h000064400000000000000000000031231051711573600155420ustar00gitgit00000000000000/*
pidfile.h - interact with pidfiles
Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>

This file is part of the sysklogd package, a kernel and system log daemon.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/

/* read_pid
*
* Reads the specified pidfile and returns the read pid.
* 0 is returned if either there's no pidfile, it's empty
* or no pid can be read.
*/
int read_pid (const char *pidfile);

/* check_pid
*
* Reads the pid using read_pid and looks up the pid in the process
* table (using /proc) to determine if the process already exists. If
* so 1 is returned, otherwise 0.
*/
int check_pid (const char *pidfile);

/* write_pid
*
* Writes the pid to the specified file. If that fails 0 is
* returned, otherwise the pid.
*/
int write_pid (const char *pidfile);

/* remove_pid
*
* Remove the the specified file. The result from unlink(2)
* is returned
*/
int remove_pid (const char *pidfile);
consolelocker-0.0.1/vlocka.c000064400000000000000000000032521051711573600154030ustar00gitgit00000000000000#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <error.h>

extern const char *__progname;

static void
clear(void) {
int retval;
pid_t pid;

if ((pid = fork()) == -1)
error(EXIT_FAILURE, errno, "fork");

if (pid == 0) {
if ((execl("/usr/bin/clear", "clear", (char *) NULL)) == -1)
error(EXIT_FAILURE, errno, "execl");
} else {
if ((retval = wait(NULL)) == -1)
error(EXIT_FAILURE, errno, "wait");
}
}

int
main(int argc, char ** argv) {
const char *user = NULL, *term = NULL;
struct passwd *pw = NULL;

if (argc < 2)
error(EXIT_FAILURE, 0, "Usage: %s <username>", __progname);

user = argv[1];
term = getenv("TERM");

if (getuid())
error(EXIT_FAILURE, 0, "must be root");

if ((pw = getpwnam(user)) == NULL)
error(EXIT_FAILURE, errno, "getpwnam");

if (clearenv() < 0)
error(EXIT_FAILURE, errno, "clearenv");

if (setenv("USER", pw->pw_name, 1) < 0)
error(EXIT_FAILURE, errno, "setenv: %s", "USER");

if (setenv("LOGNAME", pw->pw_name, 1) < 0)
error(EXIT_FAILURE, errno, "setenv: %s", "LOGNAME");

if (setenv("HOME", pw->pw_dir, 1) < 0)
error(EXIT_FAILURE, errno, "setenv: %s", "HOME");

if (setenv("PATH", "/bin:/usr/bin", 1) < 0)
error(EXIT_FAILURE, errno, "setenv: %s", "PATH");

if (setenv("TERM", term, 1) < 0)
error(EXIT_FAILURE, errno, "setenv: %s", "TERM");

if ((setgid(pw->pw_gid)) == -1)
error(EXIT_FAILURE, errno, "setgid");

if ((setuid(pw->pw_uid)) == -1)
error(EXIT_FAILURE, errno, "setuid");

clear();

if ((execl("/usr/bin/vlock", "vlocka", "-a", (char *) NULL)) == -1)
error(EXIT_FAILURE, errno, "execl");
}
 
design & coding: Vladimir Lettiev aka crux © 2004-2005, Andrew Avramenko aka liks © 2007-2008
current maintainer: Michael Shigorin