File: checkapoppw.c

package info (click to toggle)
checkpw 1.02-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 552 kB
  • ctags: 290
  • sloc: ansic: 2,169; makefile: 396; sh: 189
file content (135 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <sys/stat.h>
#include "error.h"
#include "pathexec.h"
#include "prot.h"
#include "stralloc.h"
#include "openreadclose.h"
#include "env.h"

#include "global.h"
#include "md5.h"

#include "auto_maildir.h"
#include "auto_password.h"
#include "auto_patrn.h"

#include <pwd.h>
static struct passwd *pw;

static char up[513];
static int uplen;

static stralloc stored = {0};
static stralloc pwfile = {0};

static char *hextable = "0123456789abcdef";

void cleanup()
{
  int i;
  for (i = 0;i < sizeof(up);++i) up[i] = 0;
  for (i = 0;i < stored.len;++i) stored.s[i] = 0;
}

void die(int x)
{
  cleanup();
  _exit(x);
}

main(int argc,char **argv)
{
  char *login;
  char *dash;
  char *ext;
  char *password;
  char *timestamp;
  struct stat st;
  int r;
  int i;

  MD5_CTX context;
  unsigned char digest[16];
  char encrypted[16*2+1];
  char *s;
 
  if (!argv[1]) _exit(2);
  dash = env_get("DASH");
 
  uplen = 0;
  for (;;) {
    do
      r = read(3,up + uplen,sizeof(up) - uplen);
    while ((r == -1) && (errno == error_intr));
    if (r == -1) _exit(111);
    if (r == 0) break;
    uplen += r;
    if (uplen >= sizeof(up)) _exit(1);
  }
  close(3);

  i = 0;
  if (i >= uplen) _exit(2);
  login = up + i;
  while (up[i++]) if (i >= uplen) _exit(2);
  password = up + i;
  if (i >= uplen) _exit(2);
  while (up[i++]) if (i >= uplen) _exit(2);
  timestamp = up + i;
  if (i >= uplen) _exit(2);
  while (up[i++]) if (i >= uplen) _exit(2);

  i = 0;
  ext = login + str_len(login);
  for (;;) {
    pw = getpwnam(login);
    if (pw) break;
    if (errno == error_txtbsy) die(111);
    for (; ext != login && *ext != '-'; --ext);
    if (ext == login) die(1);
    if (i) login[i] = '-';
    i = ext - login;
    login[i] = 0;
    ++ext;
  }
  if (chdir(pw->pw_dir) == -1) die(111);

  if (!stralloc_copys(&pwfile, auto_maildir)) die(111);
  if (dash && *ext) {
    if (!stralloc_cats(&pwfile, dash)) die(111);
    if (!stralloc_cats(&pwfile, ext)) die(111);
  }
  if (!stralloc_append(&pwfile, "/")) die(111);
  if (!stralloc_cats(&pwfile, auto_password)) die(111);
  if (!stralloc_0(&pwfile)) die(111);
  if (stat(pwfile.s,&st) == -1) die(1);
  if (st.st_uid != pw->pw_uid) die(1);
  if (st.st_mode & auto_patrn) die(111);
  if (openreadclose(pwfile.s,&stored,32) != 1) die(111);
  if (!stralloc_0(&stored)) die(111);
  stored.s[str_chr(stored.s,'\n')] = 0;

  MD5Init(&context);
  MD5Update(&context, timestamp, str_len(timestamp));
  MD5Update(&context, stored.s, str_len(stored.s));
  MD5Final(digest, &context);
  s = encrypted;
  for (i = 0; i < sizeof(digest); ++i) {
    *s = hextable[digest[i]/16]; ++s;
    *s = hextable[digest[i]%16]; ++s;
  }
  *s = '\0';
 
  if (strcmp(password,encrypted)) die(1);
 
  if (prot_gid((int) pw->pw_gid) == -1) die(1);
  if (prot_uid((int) pw->pw_uid) == -1) die(1);

  if (!pathexec_env("USER",pw->pw_name)) die(111);
  if (!pathexec_env("EXT",ext)) die(111);
  if (!pathexec_env("HOME",pw->pw_dir)) die(111);
  if (!pathexec_env("SHELL",pw->pw_shell)) die(111);
  cleanup();
  pathexec(argv + 1);
  _exit(111);
}