• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/LPRng/src/AUTHENTICATE/
1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17/***************************************************************************
18 * LPRng - An Extended Print Spooler System
19 *
20 * Copyright 1988-2003, Patrick Powell, San Diego, CA
21 *     papowell@lprng.com
22 * See LICENSE for conditions of use.
23 *
24 ***************************************************************************/
25
26 static char *const _id =
27"$Id: sserver.c,v 1.1.1.1 2008/10/15 03:28:26 james26_jang Exp $";
28
29/*
30 *
31 * Simple authentictor test for kerberos.
32 *  Based on the SSERVER code in the Kerberos5 distibution.
33 *  See Copyrights, etc.
34 */
35
36#include "lp.h"
37#include "krb5_auth.h"
38#include "child.h"
39#include "fileopen.h"
40
41char *msg[] = {
42	"[-D options] [-p port] [-s service] [-S keytab] file",
43	"  -D turns debugging on",
44	0
45};
46
47char *progname;
48
49void
50usage()
51{
52	int i;
53	FPRINTF(STDOUT, "usage: %s %s\n", progname, msg[0]);
54	for( i = 1; msg[i]; ++i ){
55		FPRINTF(STDOUT, "%s\n", msg[i]);
56	}
57}
58
59
60
61int
62main(int argc, char *argv[])
63{
64	struct sockaddr_in peername;
65	int namelen = sizeof(peername);
66	int sock = -1;          /* incoming connection fd */
67	short port = 1234;     /* If user specifies port */
68	extern int opterr, optind, getopt(), atoi();
69	extern char * optarg;
70	int ch, fd = -1, len;
71	int on = 1;
72	int acc;
73	struct sockaddr_in sin;
74	char auth[128];
75	char *client = 0;
76	char err[128];
77	char *file;
78	char buffer[SMALLBUFFER];
79	struct stat statb;
80
81	progname = argv[0];
82	setlinebuf(STDOUT);
83
84	/*
85	 * Parse command line arguments
86	 *
87	 */
88	opterr = 0;
89	while ((ch = getopt(argc, argv, "D:p:S:s:")) != EOF)
90	switch (ch) {
91	case 'D': Parse_debug(optarg,1); break;
92	case 'p': port = atoi(optarg); break;
93	case 's': Set_DYN(&Kerberos_service_DYN,optarg); break;
94	case 'S': Set_DYN(&Kerberos_keytab_DYN,optarg); break;
95	default: usage(); exit(1); break;
96	}
97	Spool_file_perms_DYN = 0600;
98
99	if( argc - optind != 1 ){
100		usage();
101		exit(1);
102	}
103	file = argv[optind++];
104
105	if( Kerberos_keytab_DYN == 0 ) Set_DYN(&Kerberos_keytab_DYN, "/etc/lpd.keytab");
106	if( Kerberos_service_DYN == 0 ) Set_DYN(&Kerberos_service_DYN,"lpr");
107	if( port == 0 ){
108		FPRINTF( STDOUT, "bad port specified\n" );
109		exit(1);
110	}
111	/*
112	 * If user specified a port, then listen on that port; otherwise,
113	 * assume we've been started out of inetd.
114	 */
115
116	remote_principal_krb5( Kerberos_service_DYN, 0, auth, sizeof(auth));
117	FPRINTF(STDOUT, "server principal '%s'\n", auth );
118
119	if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
120		FPRINTF(STDOUT, "socket: %s\n", Errormsg(errno));
121		exit(3);
122	}
123#ifdef WINDOW_1
124int windowsize=1024;
125setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));
126aaaaaa=fopen("/tmp/qqqqq", "a");
127fprintf(aaaaaa, "sserver: main\n");
128fclose(aaaaaa);
129#endif
130	Max_open(sock);
131	/* Let the socket be reused right away */
132	(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
133			  sizeof(on));
134
135	memset(&sin, 0, sizeof(sin));
136	sin.sin_family = AF_INET;
137	sin.sin_addr.s_addr = 0;
138	sin.sin_port = htons(port);
139	if (bind(sock, (struct sockaddr *) &sin, sizeof(sin))) {
140		FPRINTF(STDOUT, "bind: %s\n", Errormsg(errno));
141		exit(3);
142	}
143	if (listen(sock, 1) == -1) {
144		FPRINTF(STDOUT, "listen: %s", Errormsg(errno));
145		exit(3);
146	}
147	while(1){
148		if ((acc = accept(sock, (struct sockaddr *)&peername,
149				&namelen)) == -1){
150			FPRINTF(STDOUT, "accept: %s\n", Errormsg(errno));
151			exit(3);
152		}
153
154		err[0] = 0;
155		auth[0] = 0;
156		client = 0;
157		if( server_krb5_auth( Kerberos_keytab_DYN, Kerberos_service_DYN, acc,
158			&client, err, sizeof(err), file ) ){
159			FPRINTF( STDOUT, "server_krb5_auth error '%s'\n", err );
160			goto done;
161		}
162		SNPRINTF(buffer,sizeof(buffer))"client '%s'", client );
163		FPRINTF(STDOUT,"%s\n",buffer);
164		fd = Checkread( file, &statb );
165		DEBUG1( "main: opened for write '%s', fd %d, size %ld",
166			file, fd, (long)(statb.st_size) );
167		if( fd < 0 ){
168			SNPRINTF( err, sizeof(err))
169				"file open failed: %s", Errormsg(errno));
170			goto done;
171		}
172		FPRINTF(STDOUT,"RECEVIED:\n");
173		while( (len = read(fd, buffer,sizeof(buffer)-1)) > 0 ){
174			write(1,buffer,len);
175		}
176		close(fd);
177		fd = Checkwrite( file, &statb, O_WRONLY|O_TRUNC, 1, 0 );
178		if( fd < 0 ){
179			SNPRINTF( err, sizeof(err))
180				"main: could not open for writing '%s' - '%s'", file,
181					Errormsg(errno) );
182			goto done;
183		}
184		SNPRINTF(buffer,sizeof(buffer))"credentials '%s'\n", client );
185		Write_fd_str(fd,buffer);
186		close(fd);
187		if( server_krb5_status( acc, err, sizeof(err), file ) ){
188			FPRINTF( STDOUT, "server_krb5_status error '%s'\n", err );
189			goto done;
190		}
191 done:
192		close(acc);
193	}
194	exit(0);
195}
196
197/* VARARGS2 */
198#ifdef HAVE_STDARGS
199void setstatus (struct job *job,char *fmt,...)
200#else
201void setstatus (va_alist) va_dcl
202#endif
203{
204#ifndef HAVE_STDARGS
205    struct job *job;
206    char *fmt;
207#endif
208	char msg[LARGEBUFFER];
209    VA_LOCAL_DECL
210
211    VA_START (fmt);
212    VA_SHIFT (job, struct job * );
213    VA_SHIFT (fmt, char *);
214
215	msg[0] = 0;
216	if( Verbose ){
217		(void) VSNPRINTF( msg, sizeof(msg)-2) fmt, ap);
218		strcat( msg,"\n" );
219		if( Write_fd_str( 2, msg ) < 0 ) cleanup(0);
220	}
221	VA_END;
222	return;
223}
224
225void send_to_logger( int sfd, int mfd, struct job *job, const char *header, char *msg ){;}
226/* VARARGS2 */
227#ifdef HAVE_STDARGS
228void setmessage (struct job *job,const char *header, char *fmt,...)
229#else
230void setmessage (va_alist) va_dcl
231#endif
232{
233#ifndef HAVE_STDARGS
234    struct job *job;
235    char *fmt, *header;
236#endif
237	char msg[LARGEBUFFER];
238    VA_LOCAL_DECL
239
240    VA_START (fmt);
241    VA_SHIFT (job, struct job * );
242    VA_SHIFT (header, char * );
243    VA_SHIFT (fmt, char *);
244
245	msg[0] = 0;
246	if( Verbose ){
247		(void) VSNPRINTF( msg, sizeof(msg)-2) fmt, ap);
248		strcat( msg,"\n" );
249		if( Write_fd_str( 2, msg ) < 0 ) cleanup(0);
250	}
251	VA_END;
252	return;
253}
254
255