File: client.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,368 kB
  • sloc: ansic: 15,820; perl: 674; sh: 64; makefile: 26
file content (121 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download | duplicates (8)
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
/* pwcmp/client.c - Password comparison client library
 * Copyright (C) 2001  Bruce Guenter <bruce@untroubled.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "sysdeps.h"

#include "client.h"

typedef int (*pwcmpfn)(const char*, const char*);

static long pid;
static int fd0;
static int fd1;
static pwcmpfn cmpfn;


static int pwcmp_plain(const char* plaintext, const char* encoded)
{
  return strcmp(plaintext, encoded) == 0 ? 0 : 1;
}

static int pwcmp_crypt(const char* plaintext, const char* encoded)
{
  extern char* crypt(const char* key, const char* salt);
  plaintext = crypt(plaintext, encoded);
  return pwcmp_plain(plaintext, encoded);
}

static int rwrite(int fd, const char* buf, unsigned long len)
{
  while (len > 0) {
    unsigned long wr = write(fd, buf, len);
    if (wr == (unsigned long)-1) return 0;
    len -= wr;
    buf += wr;
  }
  return 1;
}

static int pwcmp_pipe(const char* plaintext, const char* encoded)
{
  char buf[1];
  if (!rwrite(fd0, plaintext, strlen(plaintext)+1)) return -1;
  if (!rwrite(fd0, encoded, strlen(encoded)+1)) return -1;
  if (read(fd1, buf, 1) != 1) return -1;
  return buf[0] == 0 ? 0 : 1;
}

static int pwcmp_start_pipe(const char* module)
{
  int pipe0[2];
  int pipe1[2];
  if (pipe(pipe0) == -1) return 0;
  if (pipe(pipe1) == -1) { close(pipe0[0]); close(pipe0[1]); return 0; }
  if ((pid = fork()) == -1) {
    close(pipe0[0]); close(pipe0[1]);
    close(pipe1[0]); close(pipe1[1]);
    return 0;
  }
  if (pid == 0) {
    close(0);
    dup2(pipe0[0], 0);
    close(1);
    dup2(pipe1[1], 1);
    close(pipe0[0]); close(pipe0[1]);
    close(pipe1[0]); close(pipe1[1]);
    execlp(module, module, NULL);
    _exit(1);
  }
  else {
    close(pipe0[0]); fd0 = pipe0[1];
    close(pipe1[1]); fd1 = pipe1[0];
  }
  return 1;
}

int pwcmp_start(const char* module)
{
  if (!module || module[0] == 0 || strcmp(module, "plain") == 0)
    cmpfn = pwcmp_plain;
  else if (strcmp(module, "crypt") == 0)
    cmpfn = pwcmp_crypt;
  else {
    cmpfn = pwcmp_pipe;
    if (!pwcmp_start_pipe(module)) return 0;
  }
  return 1;
}

int pwcmp_check(const char* plaintext, const char* encoded)
{
  if (!cmpfn) return -1;
  return cmpfn(plaintext, encoded);
}

void pwcmp_stop(void)
{
  if (cmpfn == pwcmp_pipe) {
    close(fd0);
    close(fd1);
    waitpid(pid, 0, WUNTRACED);
  }
  cmpfn = 0;
}