]> git.saurik.com Git - apple/network_cmds.git/blame - uucpd.tproj/uucpd.c
network_cmds-85.tar.gz
[apple/network_cmds.git] / uucpd.tproj / uucpd.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1985, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Rick Adams.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60#ifndef lint
61static char copyright[] =
62"@(#) Copyright (c) 1985, 1993\n\
63 The Regents of the University of California. All rights reserved.\n";
64#endif /* not lint */
65
66#ifndef lint
67static char sccsid[] = "@(#)uucpd.c 8.1 (Berkeley) 6/4/93";
68#endif /* not lint */
69
70/*
71 * 4.2BSD TCP/IP server for uucico
72 * uucico's TCP channel causes this server to be run at the remote end.
73 */
74
75#include <sys/types.h>
76#include <sys/wait.h>
77#include <sys/ioctl.h>
78#include <sys/socket.h>
79#include <netinet/in.h>
80#include <arpa/inet.h>
81#include <netdb.h>
82#include <signal.h>
83#include <fcntl.h>
84#include <time.h>
85#include <pwd.h>
86#include <unistd.h>
87#include <errno.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
91#include "pathnames.h"
92
93struct sockaddr_in hisctladdr;
94int hisaddrlen = sizeof hisctladdr;
95struct sockaddr_in myctladdr;
96int mypid;
97
98char Username[64];
99char *nenv[] = {
100 Username,
101 NULL,
102};
103extern char **environ;
104
105main(argc, argv)
106int argc;
107char **argv;
108{
109#ifndef BSDINETD
110 register int s, tcp_socket;
111 struct servent *sp;
112#endif !BSDINETD
113 extern int errno;
114 int dologout();
115
116 environ = nenv;
117#ifdef BSDINETD
118 close(1); close(2);
119 dup(0); dup(0);
120 hisaddrlen = sizeof (hisctladdr);
121 if (getpeername(0, &hisctladdr, &hisaddrlen) < 0) {
122 fprintf(stderr, "%s: ", argv[0]);
123 perror("getpeername");
124 _exit(1);
125 }
126 if (fork() == 0)
127 doit(&hisctladdr);
128 dologout();
129 exit(1);
130#else !BSDINETD
131 sp = getservbyname("uucp", "tcp");
132 if (sp == NULL){
133 perror("uucpd: getservbyname");
134 exit(1);
135 }
136 if (fork())
137 exit(0);
138 if ((s=open(_PATH_TTY, 2)) >= 0){
139 ioctl(s, TIOCNOTTY, (char *)0);
140 close(s);
141 }
142
143 bzero((char *)&myctladdr, sizeof (myctladdr));
144 myctladdr.sin_family = AF_INET;
145 myctladdr.sin_port = sp->s_port;
146#ifdef BSD4_2
147 tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
148 if (tcp_socket < 0) {
149 perror("uucpd: socket");
150 exit(1);
151 }
152 if (bind(tcp_socket, (char *)&myctladdr, sizeof (myctladdr)) < 0) {
153 perror("uucpd: bind");
154 exit(1);
155 }
156 listen(tcp_socket, 3); /* at most 3 simultaneuos uucp connections */
157 signal(SIGCHLD, dologout);
158
159 for(;;) {
160 s = accept(tcp_socket, &hisctladdr, &hisaddrlen);
161 if (s < 0){
162 if (errno == EINTR)
163 continue;
164 perror("uucpd: accept");
165 exit(1);
166 }
167 if (fork() == 0) {
168 close(0); close(1); close(2);
169 dup(s); dup(s); dup(s);
170 close(tcp_socket); close(s);
171 doit(&hisctladdr);
172 exit(1);
173 }
174 close(s);
175 }
176#endif BSD4_2
177
178#endif !BSDINETD
179}
180
181doit(sinp)
182struct sockaddr_in *sinp;
183{
184 char user[64], passwd[64];
185 char *xpasswd, *crypt();
186 struct passwd *pw, *getpwnam();
187
188 alarm(60);
189 printf("login: "); fflush(stdout);
190 if (readline(user, sizeof user) < 0) {
191 fprintf(stderr, "user read\n");
192 return;
193 }
194 /* truncate username to 8 characters */
195 user[8] = '\0';
196 pw = getpwnam(user);
197 if (pw == NULL) {
198 fprintf(stderr, "user unknown\n");
199 return;
200 }
201 if (strcmp(pw->pw_shell, _PATH_UUCICO)) {
202 fprintf(stderr, "Login incorrect.");
203 return;
204 }
205 if (pw->pw_passwd && *pw->pw_passwd != '\0') {
206 printf("Password: "); fflush(stdout);
207 if (readline(passwd, sizeof passwd) < 0) {
208 fprintf(stderr, "passwd read\n");
209 return;
210 }
211 xpasswd = crypt(passwd, pw->pw_passwd);
212 if (strcmp(xpasswd, pw->pw_passwd)) {
213 fprintf(stderr, "Login incorrect.");
214 return;
215 }
216 }
217 alarm(0);
218 sprintf(Username, "USER=%s", user);
219 dologin(pw, sinp);
220 setgid(pw->pw_gid);
221#ifdef BSD4_2
222 initgroups(pw->pw_name, pw->pw_gid);
223#endif BSD4_2
224 chdir(pw->pw_dir);
225 setuid(pw->pw_uid);
226#ifdef BSD4_2
227 execl(UUCICO, "uucico", (char *)0);
228#endif BSD4_2
229 perror("uucico server: execl");
230}
231
232readline(p, n)
233register char *p;
234register int n;
235{
236 char c;
237
238 while (n-- > 0) {
239 if (read(0, &c, 1) <= 0)
240 return(-1);
241 c &= 0177;
242 if (c == '\n' || c == '\r') {
243 *p = '\0';
244 return(0);
245 }
246 *p++ = c;
247 }
248 return(-1);
249}
250
251#include <utmp.h>
252#ifdef BSD4_2
253#include <fcntl.h>
254#endif BSD4_2
255
256#define SCPYN(a, b) strncpy(a, b, sizeof (a))
257
258struct utmp utmp;
259
260dologout()
261{
262 union wait status;
263 int pid, wtmp;
264
265#ifdef BSDINETD
266 while ((pid=wait((int *)&status)) > 0) {
267#else !BSDINETD
268 while ((pid=wait3((int *)&status,WNOHANG,0)) > 0) {
269#endif !BSDINETD
270 wtmp = open(_PATH_WTMP, O_WRONLY|O_APPEND);
271 if (wtmp >= 0) {
272 sprintf(utmp.ut_line, "uucp%.4d", pid);
273 SCPYN(utmp.ut_name, "");
274 SCPYN(utmp.ut_host, "");
275 (void) time(&utmp.ut_time);
276 (void) write(wtmp, (char *)&utmp, sizeof (utmp));
277 (void) close(wtmp);
278 }
279 }
280}
281
282/*
283 * Record login in wtmp file.
284 */
285dologin(pw, sin)
286struct passwd *pw;
287struct sockaddr_in *sin;
288{
289 char line[32];
290 char remotehost[32];
291 int wtmp, f;
292 struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
293 sizeof (struct in_addr), AF_INET);
294
295 if (hp) {
296 strncpy(remotehost, hp->h_name, sizeof (remotehost));
297 endhostent();
298 } else
299 strncpy(remotehost, inet_ntoa(sin->sin_addr),
300 sizeof (remotehost));
301 wtmp = open(_PATH_WTMP, O_WRONLY|O_APPEND);
302 if (wtmp >= 0) {
303 /* hack, but must be unique and no tty line */
304 sprintf(line, "uucp%.4d", getpid());
305 SCPYN(utmp.ut_line, line);
306 SCPYN(utmp.ut_name, pw->pw_name);
307 SCPYN(utmp.ut_host, remotehost);
308 time(&utmp.ut_time);
309 (void) write(wtmp, (char *)&utmp, sizeof (utmp));
310 (void) close(wtmp);
311 }
312 if ((f = open(_PATH_LASTLOG, O_RDWR)) >= 0) {
313 struct lastlog ll;
314
315 time(&ll.ll_time);
316 lseek(f, (long)pw->pw_uid * sizeof(struct lastlog), 0);
317 strcpy(line, remotehost);
318 SCPYN(ll.ll_line, line);
319 SCPYN(ll.ll_host, remotehost);
320 (void) write(f, (char *) &ll, sizeof ll);
321 (void) close(f);
322 }
323}