~ubuntu-branches/ubuntu/raring/vlock/raring

« back to all changes in this revision

Viewing changes to src/vlock-current.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2008-06-17 17:13:25 UTC
  • mfrom: (1.1.2 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080617171325-ic8yy6tol0165i96
Tags: 2.2.2-3
* Don't try to chgrp to "vlock" during build time (Closes: #486665)
* Bump standards version (No changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vlock-current.c -- locking routine for vlock,
2
 
 *                    the VT locking program for linux
3
 
 *
4
 
 * This program is copyright (C) 2007 Frank Benkstein, and is free
5
 
 * software which is freely distributable under the terms of the
6
 
 * GNU General Public License version 2, included as the file COPYING in this
7
 
 * distribution.  It is NOT public domain software, and any
8
 
 * redistribution not permitted by the GNU General Public License is
9
 
 * expressly forbidden without prior written permission from
10
 
 * the author.
11
 
 *
12
 
 */
13
 
 
14
 
#include <stdlib.h>
15
 
#include <string.h>
16
 
#include <stdio.h>
17
 
 
18
 
#include <pwd.h>
19
 
 
20
 
#include <termios.h>
21
 
#include <unistd.h>
22
 
#include <sys/types.h>
23
 
#include <signal.h>
24
 
#include <errno.h>
25
 
#include <time.h>
26
 
 
27
 
#include "vlock.h"
28
 
 
29
 
/* Lock the current terminal until proper authentication is received. */
30
 
int main(void) {
31
 
  char user[40];
32
 
  char *vlock_message;
33
 
  char *vlock_prompt_timeout;
34
 
  struct timespec timeout;
35
 
  struct timespec *timeout_p = NULL;
36
 
  struct termios term, term_bak;
37
 
  struct sigaction sa;
38
 
  /* get the user id */
39
 
  uid_t uid = getuid();
40
 
  /* get the user name from the environment */
41
 
  char *envuser = getenv("USER"); 
42
 
 
43
 
  /* ignore some signals */
44
 
  /* these signals shouldn't be delivered anyway, because
45
 
   * terminal signals are disabled below */
46
 
  (void) sigemptyset(&(sa.sa_mask));
47
 
  sa.sa_flags = SA_RESTART;
48
 
  sa.sa_handler = SIG_IGN;
49
 
  (void) sigaction(SIGINT, &sa, NULL);
50
 
  (void) sigaction(SIGQUIT, &sa, NULL);
51
 
  (void) sigaction(SIGTSTP, &sa, NULL);
52
 
 
53
 
  if (uid > 0 || envuser == NULL) {
54
 
    errno = 0;
55
 
 
56
 
    /* get the password entry */
57
 
    struct passwd *pw = getpwuid(uid);
58
 
 
59
 
    if (pw == NULL) {
60
 
      if (errno != 0)
61
 
        perror("vlock-current: getpwuid() failed");
62
 
      else
63
 
        fprintf(stderr, "vlock-current: getpwuid() failed\n");
64
 
 
65
 
      exit (111);
66
 
    }
67
 
 
68
 
    /* copy the username */
69
 
    strncpy(user, pw->pw_name, sizeof user - 1);
70
 
    user[sizeof user - 1] = '\0';
71
 
  } else {
72
 
    /* copy the username */
73
 
    strncpy(user, envuser, sizeof user - 1);
74
 
    user[sizeof user - 1] = '\0';
75
 
  }
76
 
 
77
 
  /* get the vlock message from the environment */
78
 
  vlock_message = getenv("VLOCK_MESSAGE");
79
 
 
80
 
  /* get the timeout from the environment */
81
 
  vlock_prompt_timeout = getenv("VLOCK_PROMPT_TIMEOUT");
82
 
 
83
 
  if (vlock_prompt_timeout != NULL) {
84
 
    char *n;
85
 
    timeout.tv_sec = strtol(vlock_prompt_timeout, &n, 10);
86
 
    timeout.tv_nsec = 0;
87
 
 
88
 
 
89
 
    if (*n == '\0' && timeout.tv_sec > 0)
90
 
      timeout_p = &timeout;
91
 
  }
92
 
 
93
 
  /* disable terminal echoing and signals */
94
 
  if (tcgetattr(STDIN_FILENO, &term) == 0) {
95
 
    term_bak = term;
96
 
    term.c_lflag &= ~(ECHO|ISIG);
97
 
    (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);
98
 
  }
99
 
 
100
 
  for (;;) {
101
 
    char c = 0;
102
 
 
103
 
    if (vlock_message) {
104
 
      /* print vlock message */
105
 
      fputs(vlock_message, stderr);
106
 
      fputc('\n', stderr);
107
 
    }
108
 
 
109
 
    /* wait for enter to be pressed */
110
 
    while (read(STDIN_FILENO, &c, 1) >= 0)
111
 
      if (c == '\n')
112
 
        break;
113
 
 
114
 
    if (auth(user, timeout_p))
115
 
      break;
116
 
    else
117
 
      sleep(1);
118
 
 
119
 
#ifndef NO_ROOT_PASS
120
 
    if (auth("root", timeout_p))
121
 
      break;
122
 
    else
123
 
      sleep(1);
124
 
#endif
125
 
  }
126
 
 
127
 
  /* restore the terminal */
128
 
  (void) tcsetattr(STDIN_FILENO, TCSANOW, &term_bak);
129
 
 
130
 
  exit (0);
131
 
}