Debian Codegraph @

hfaxd/Login.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "HylaFAXServer.h"
27 #include "Sys.h"
28
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <grp.h>
33 #include <pwd.h>
34 #if HAS_CRYPT_H
35 #include <crypt.h>
36 #endif
37
38 void
39 HylaFAXServer::loginRefused(const char* why)
40 {
41 if (++loginAttempts >= maxLoginAttempts) {
42 reply(530, "Login incorrect (closing connection).");
43 logNotice("Repeated login failures for user %s from %s [%s]"
44 , (const char*) the_user
45 , (const char*) remotehost
46 , (const char*) remoteaddr
47 );
48 dologout(0);
49 } else {
50 reply(530, "User %s access denied.", (const char*) the_user);
51 logNotice("HylaFAX LOGIN REFUSED (%s) FROM %s [%s], %s"
52 , why
53 , (const char*) remotehost
54 , (const char*) remoteaddr
55 , (const char*) the_user
56 );
57 }
58 }
59
60 /*
61 * USER command. Sets global passwd state if named
62 * account exists and is acceptable; sets askpasswd if a
63 * PASS command is expected. If logged in previously,
64 * need to reset state. User account must be accessible
65 * from client host according to the contents of the
66 * userAccessFile.
67 */
68 void
69 HylaFAXServer::userCmd(const char* name)
70 {
71 if (IS(LOGGEDIN)) {
72 if (IS(PRIVILEGED) && the_user == name) {// revert to unprivileged mode
73 state &= ~S_PRIVILEGED;
74 reply(230, "User %s logged in.", name);
75 return;
76 }
77 end_login();
78 }
79 the_user = name;
80 state &= ~S_PRIVILEGED;
81 adminwd = "*"; // make sure no admin privileges
82 passwd = "*"; // just in case...
83
84 if (checkUser(name)) {
85 if (passwd != "") {
86 state |= S_WAITPASS;
87 reply(331, "Password required for %s.", name);
88 /*
89 * Delay before reading passwd after first failed
90 * attempt to slow down password-guessing programs.
91 */
92 if (loginAttempts)
93 sleep(loginAttempts);
94 } else
95 login(230);
96 } else
97 loginRefused("user denied");
98 }
99
100 bool
101 HylaFAXServer::isAdminGroup(const char* user)
102 {
103 bool retval = false;
104 if (admingroup.length() > 0) {
105 int i;
106 static struct group* grinfo = getgrnam(admingroup);
107 const char *curruser = (user == NULL ? the_user.c_str() : user);
108 if (grinfo != NULL) {
109 for (i=0; grinfo->gr_mem[i] != NULL; i++) {
110 if (strcmp(curruser, grinfo->gr_mem[i]) == 0) retval = true;
111 }
112 }
113 }
114 return(retval);
115 }
116
117 void
118 HylaFAXServer::passCmd(const char* pass)
119 {
120 if (IS(LOGGEDIN)) {
121 reply(503, "Already logged in as USER %s.", (const char*) the_user);
122 return;
123 }
124 if (!IS(WAITPASS)) {
125 reply(503, "Login with USER first.");
126 return;
127 }
128 state &= ~S_WAITPASS;
129
130 /*
131 * Disable long reply messages for old (broken) FTP
132 * clients if the first character of the password
133 * is a ``-''.
134 */
135 if (pass[0] == '-') {
136 state &= ~S_LREPLIES;
137 pass++;
138 } else
139 state |= S_LREPLIES;
140
141 if (! checkPasswd(pass)) {
142 if (++loginAttempts >= maxLoginAttempts) {
143 reply(530, "Login incorrect (closing connection).");
144 logNotice("Repeated login failures for user %s from %s [%s]"
145 , (const char*) the_user
146 , (const char*) remotehost
147 , (const char*) remoteaddr
148 );
149 dologout(0);
150 }
151 reply(530, "Login incorrect.");
152 logInfo("Login failed from %s [%s], %s"
153 , (const char*) remotehost
154 , (const char*) remoteaddr
155 , (const char*) the_user
156 );
157 return;
158 }
159 login(230);
160 }
161
162 /*
163 * Login is shared between SNPP and HylaFAX RFC 959 protocol,
164 * but different protocols use different codes for success.
165 * We make the caller tell us what success is.
166 */
167 void
168 HylaFAXServer::login(int code)
169 {
170 loginAttempts = 0; // this time successful
171 state |= S_LOGGEDIN;
172
173 uid_t ouid = geteuid();
174 (void) seteuid(0);
175 bool isSetup = (chroot(".") >= 0 && chdir("/") >= 0);
176 /*
177 * Install the client's fax-uid as the effective gid
178 * so that created files automatically are given the
179 * correct ``ownership'' (we store the owner's fax-uid
180 * in the group-id field of the inode).
181 */
182 if (isSetup)
183 (void) setegid(uid);
184 (void) seteuid(ouid);
185 if (!isSetup) {
186 reply(550, "Cannot set privileges.");
187 end_login();
188 return;
189 }
190
191 (void) isShutdown(false); // display any shutdown messages
192 reply(code, "User %s logged in.", (const char*) the_user);
193 if (TRACE(LOGIN))
194 logInfo("FAX LOGIN FROM %s [%s], %s"
195 , (const char*) remotehost
196 , (const char*) remoteaddr
197 , (const char*) the_user
198 );
199 (void) umask(077);
200 if (tracingLevel & (TRACE_INXFERS|TRACE_OUTXFERS))
201 xferfaxlog = Sys::open(xferfaxLogFile, O_WRONLY|O_APPEND|O_CREAT, 0600);
202
203 initDefaultJob(); // setup connection-related state
204 dirSetup(); // initialize directory handling
205 if (isAdminGroup()) state |= S_PRIVILEGED;
206 }
207
208 void
209 HylaFAXServer::adminCmd(const char* pass)
210 {
211 fxAssert(IS(LOGGEDIN), "ADMIN command permitted when not logged in");
212 // NB: null adminwd is permitted
213 if ((strcmp(crypt(pass, adminwd), adminwd) != 0) && !isAdminGroup()) {
214 if (++adminAttempts >= maxAdminAttempts) {
215 reply(530, "Password incorrect (closing connection).");
216 logNotice("Repeated admin failures from %s [%s]"
217 , (const char*) remotehost
218 , (const char*) remoteaddr
219 );
220 dologout(0);
221 } else {
222 reply(530, "Password incorrect.");
223 logInfo("ADMIN failed from %s [%s], %s"
224 , (const char*) remotehost
225 , (const char*) remoteaddr
226 , (const char*) the_user
227 );
228 }
229 return;
230 }
231 if (TRACE(SERVER))
232 logInfo("FAX ADMIN FROM %s [%s], %s"
233 , (const char*) remotehost
234 , (const char*) remoteaddr
235 , (const char*) the_user
236 );
237 adminAttempts = 0;
238 state |= S_PRIVILEGED;
239 reply(230, "Administrative privileges established.");
240 }
241
242 /*
243 * Terminate login as previous user, if any,
244 * resetting state; used when USER command is
245 * given or login fails.
246 */
247 void
248 HylaFAXServer::end_login(void)
249 {
250 if (IS(LOGGEDIN)) {
251 uid_t ouid = geteuid();
252 seteuid(0);
253 seteuid(ouid);
254 }
255 state &= ~(S_LOGGEDIN|S_PRIVILEGED|S_WAITPASS);
256 passwd = "*";
257 adminwd = "*";
258 }
259
260 /*
261 * Record logout in wtmp file, cleanup state,
262 * and exit with supplied status.
263 */
264 void
265 HylaFAXServer::dologout(int status)
266 {
267 if (IS(LOGGEDIN))
268 end_login();
269 if (trigSpec != "") {
270 fxStr emsg;
271 cancelTrigger(emsg);
272 }
273 for (u_int i = 0, n = tempFiles.length(); i < n; i++)
274 (void) Sys::unlink(tempFiles[i]);
275 if (xferfaxlog != -1)
276 Sys::close(xferfaxlog);
277 if (clientFd != -1)
278 Sys::close(clientFd);
279 if (clientFIFOName != "") {
280 /* we need to check for the FIFO since dologout() might be called
281 * before we are chroot()ed... *sigh*
282 */
283 if (Sys::isFIFOFile( "/" | clientFIFOName)) {
284 Sys::unlink("/" | clientFIFOName);
285 } else if (Sys::isFIFOFile( FAX_SPOOLDIR "/" | clientFIFOName)) {
286 Sys::unlink( FAX_SPOOLDIR "/" | clientFIFOName);
287 }
288 }
289 for (JobDictIter iter(blankJobs); iter.notDone(); iter++) {
290 Job* job = iter.value();
291 fxStr file("/" | job->qfile);
292 Sys::unlink(file);
293 }
294 _exit(status); // beware of flushing buffers after a SIGPIPE
295 }