1 |
|
|
/* $OpenBSD: login_reject.c,v 1.16 2016/09/03 10:51:26 gsoares Exp $ */ |
2 |
|
|
|
3 |
|
|
/*- |
4 |
|
|
* Copyright (c) 1995 Berkeley Software Design, Inc. All rights reserved. |
5 |
|
|
* |
6 |
|
|
* Redistribution and use in source and binary forms, with or without |
7 |
|
|
* modification, are permitted provided that the following conditions |
8 |
|
|
* are met: |
9 |
|
|
* 1. Redistributions of source code must retain the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer in the |
13 |
|
|
* documentation and/or other materials provided with the distribution. |
14 |
|
|
* 3. All advertising materials mentioning features or use of this software |
15 |
|
|
* must display the following acknowledgement: |
16 |
|
|
* This product includes software developed by Berkeley Software Design, |
17 |
|
|
* Inc. |
18 |
|
|
* 4. The name of Berkeley Software Design, Inc. may not be used to endorse |
19 |
|
|
* or promote products derived from this software without specific prior |
20 |
|
|
* written permission. |
21 |
|
|
* |
22 |
|
|
* THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND |
23 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE |
26 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 |
|
|
* SUCH DAMAGE. |
33 |
|
|
* |
34 |
|
|
* BSDI $From: login_reject.c,v 1.5 1996/08/22 20:43:11 prb Exp $ |
35 |
|
|
*/ |
36 |
|
|
#include <sys/stat.h> |
37 |
|
|
#include <sys/time.h> |
38 |
|
|
#include <sys/resource.h> |
39 |
|
|
#include <sys/file.h> |
40 |
|
|
#include <sys/wait.h> |
41 |
|
|
|
42 |
|
|
#include <err.h> |
43 |
|
|
#include <errno.h> |
44 |
|
|
#include <login_cap.h> |
45 |
|
|
#include <pwd.h> |
46 |
|
|
#include <readpassphrase.h> |
47 |
|
|
#include <signal.h> |
48 |
|
|
#include <stdarg.h> |
49 |
|
|
#include <stdio.h> |
50 |
|
|
#include <stdlib.h> |
51 |
|
|
#include <string.h> |
52 |
|
|
#include <syslog.h> |
53 |
|
|
#include <unistd.h> |
54 |
|
|
|
55 |
|
|
int |
56 |
|
|
main(int argc, char *argv[]) |
57 |
|
|
{ |
58 |
|
|
struct rlimit rl; |
59 |
|
|
FILE *back; |
60 |
|
|
char passbuf[1]; |
61 |
|
|
int mode = 0, c; |
62 |
|
|
|
63 |
|
|
rl.rlim_cur = 0; |
64 |
|
|
rl.rlim_max = 0; |
65 |
|
|
(void)setrlimit(RLIMIT_CORE, &rl); |
66 |
|
|
|
67 |
|
|
(void)setpriority(PRIO_PROCESS, 0, 0); |
68 |
|
|
|
69 |
|
|
if (pledge("stdio rpath tty", NULL) == -1) { |
70 |
|
|
syslog(LOG_AUTH|LOG_ERR, "pledge: %m"); |
71 |
|
|
exit(1); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
openlog("login", LOG_ODELAY, LOG_AUTH); |
75 |
|
|
|
76 |
|
|
while ((c = getopt(argc, argv, "v:s:")) != -1) |
77 |
|
|
switch (c) { |
78 |
|
|
case 'v': |
79 |
|
|
break; |
80 |
|
|
case 's': /* service */ |
81 |
|
|
if (strcmp(optarg, "login") == 0) |
82 |
|
|
mode = 0; |
83 |
|
|
else if (strcmp(optarg, "challenge") == 0) |
84 |
|
|
mode = 1; |
85 |
|
|
else if (strcmp(optarg, "response") == 0) |
86 |
|
|
mode = 2; |
87 |
|
|
else { |
88 |
|
|
syslog(LOG_ERR, "%s: invalid service", optarg); |
89 |
|
|
exit(1); |
90 |
|
|
} |
91 |
|
|
break; |
92 |
|
|
default: |
93 |
|
|
syslog(LOG_ERR, "usage error"); |
94 |
|
|
exit(1); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
switch (argc - optind) { |
98 |
|
|
case 2: |
99 |
|
|
case 1: |
100 |
|
|
break; |
101 |
|
|
default: |
102 |
|
|
syslog(LOG_ERR, "usage error"); |
103 |
|
|
exit(1); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
if (!(back = fdopen(3, "r+"))) { |
107 |
|
|
syslog(LOG_ERR, "reopening back channel: %m"); |
108 |
|
|
exit(1); |
109 |
|
|
} |
110 |
|
|
if (mode == 1) { |
111 |
|
|
fprintf(back, BI_SILENT "\n"); |
112 |
|
|
exit(0); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
if (mode == 2) { |
116 |
|
|
mode = 0; |
117 |
|
|
c = -1; |
118 |
|
|
while (read(3, passbuf, 1) == 1) { |
119 |
|
|
if (passbuf[0] == '\0' && ++mode == 2) |
120 |
|
|
break; |
121 |
|
|
} |
122 |
|
|
if (mode < 2) { |
123 |
|
|
syslog(LOG_ERR, "protocol error on back channel"); |
124 |
|
|
exit(1); |
125 |
|
|
} |
126 |
|
|
} else |
127 |
|
|
readpassphrase("Password:", passbuf, sizeof(passbuf), 0); |
128 |
|
|
|
129 |
|
|
crypt_checkpass("password", NULL); |
130 |
|
|
|
131 |
|
|
fprintf(back, BI_REJECT "\n"); |
132 |
|
|
exit(1); |
133 |
|
|
} |