]>
git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - initscripts/src/securetty.c
11 #include <sys/types.h>
14 void alarm_handler(int num
) {
18 int open_and_lock_securetty() {
21 struct sigaction act
, oldact
;
23 lock
.l_type
= F_WRLCK
;
24 lock
.l_whence
= SEEK_SET
;
28 fd
= open("/etc/securetty", O_RDWR
);
30 syslog(LOG_ERR
, "Couldn't open /etc/securetty: %s",strerror(errno
));
33 act
.sa_handler
= alarm_handler
;
35 sigaction(SIGALRM
, &act
, &oldact
);
37 while (fcntl(fd
, F_SETLKW
, &lock
) == -1) {
39 syslog(LOG_ERR
, "Couldn't lock /etc/securetty: Timeout exceeded");
41 syslog(LOG_ERR
, "Couldn't lock /etc/securetty: %s",strerror(errno
));
46 sigaction(SIGALRM
, &oldact
, NULL
);
50 int rewrite_securetty(char *terminal
) {
55 fd
= open_and_lock_securetty();
58 if (fstat(fd
, &sbuf
) == -1) {
60 syslog(LOG_ERR
, "Couldn't stat /etc/securetty: %s",strerror(errno
));
63 buf
= malloc(sbuf
.st_size
+ 1);
64 if (read(fd
, buf
, sbuf
.st_size
) != sbuf
.st_size
) {
66 syslog(LOG_ERR
, "Couldn't read /etc/securetty: %s",strerror(errno
));
69 if (!strncmp(buf
,terminal
,strlen(terminal
)) && buf
[strlen(terminal
)] == '\n')
71 if ((pos
= strstr(buf
, terminal
))) {
72 if (pos
[strlen(terminal
)] == '\n' && *(pos
-1) == '\n')
75 if (lseek(fd
, 0, SEEK_END
) == -1) {
77 syslog(LOG_ERR
, "Couldn't seek to end of /etc/securetty: %s",strerror(errno
));
80 write(fd
, terminal
, strlen(terminal
));
87 int main(int argc
, char **argv
) {
89 fprintf(stderr
, "Usage: securetty <device>\n");
92 openlog("securetty", LOG_CONS
, LOG_DAEMON
);
93 return rewrite_securetty(argv
[1]);