port of netbsd's tr
[minix.git] / commands / ftpd200 / access.c
blob0e686bbb012ea4556b10d936924c83e62f55dc12
1 /* access.c Copyright 1992-2000 by Michael Temari All Rights Reserved
3 * This file is part of ftpd.
5 * This file handles:
7 * USER PASS QUIT
10 * 01/25/96 Initial Release Michael Temari, <Michael@TemWare.Com>
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <pwd.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <net/gen/in.h>
20 #include <net/gen/tcp.h>
22 #include "ftpd.h"
23 #include "access.h"
25 _PROTOTYPE(static int AreWeIn, (char *name, struct passwd *pwd));
27 static char *msg530 = "530 Not logged in.\r\n";
29 /* Returns -1 = not logged in, 0 = loggedin */
30 int ChkLoggedIn()
32 if(!loggedin) {
33 printf(msg530);
34 return(-1);
35 } else
36 return(0);
39 /* what a USER! */
40 int doUSER(buff)
41 char *buff;
43 loggedin = 0;
44 gotuser = 0;
45 strncpy(username, buff, sizeof(username));
46 username[sizeof(username)-1] = '\0';
48 if(*username == '\0') {
49 printf("501 Bad user name.\r\n");
50 return(GOOD);
53 gotuser = 1;
55 printf("331 Password required for %s.\r\n", username);
57 return(GOOD);
60 /* secret, secret, secret */
61 int doPASS(buff)
62 char *buff;
64 char *name;
65 struct passwd *pwd;
66 int bad=0;
68 name = username;
70 if(!strcmp(name, "anonymous"))
71 name = "ftp";
73 if(!gotuser || ((pwd = getpwnam(name)) == (struct passwd *)0))
74 bad = 1;
75 else
76 if(strcmp(name, "ftp")) {
77 if(!strcmp(pwd->pw_passwd, crypt("", pwd->pw_passwd)))
78 bad = 1;
79 if(strcmp(pwd->pw_passwd, crypt(buff, pwd->pw_passwd)))
80 bad = 1;
81 } else {
82 strncpy(anonpass, buff, sizeof(anonpass));
83 anonpass[sizeof(anonpass)-1] = '\0';
86 if(bad) {
87 logit("LOGIN", "FAIL");
88 printf(msg530);
89 return(GOOD);
92 return(AreWeIn(name, pwd));
95 /* bye, bye don't let the door hit you in the butt on the way out */
96 int doQUIT(buff)
97 char *buff;
99 printf("221 Service closing, don't be a stranger.\r\n");
101 return(BAD);
104 /* see if this user is okay */
105 static int AreWeIn(name, pwd)
106 char *name;
107 struct passwd *pwd;
109 if(!strcmp(name, "ftp")) {
110 if(chroot(pwd->pw_dir)) {
111 logit("LOGIN", "FAIL");
112 printf("530 Not logged in, could not chroot.\r\n");
113 return(GOOD);
115 strncpy(newroot, pwd->pw_dir, sizeof(newroot));
116 newroot[sizeof(newroot)-1] = '\0';
117 anonymous = 1;
118 strcpy(pwd->pw_dir, "/");
121 if(setgid(pwd->pw_gid) || setuid(pwd->pw_uid) || chdir(pwd->pw_dir)) {
122 logit("LOGIN", "FAIL");
123 printf(msg530);
124 anonymous = 0;
125 } else {
126 logit("LOGIN", "PASS");
127 showmsg("230", (char *)NULL);
128 printf("230 User %s logged in, directory %s.\r\n",
129 username, pwd->pw_dir);
130 loggedin = 1;
133 return(GOOD);