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: sclient.c,v 1.1.1.1 2008/10/15 03:28:26 james26_jang Exp $";
28
29
30/*
31 * Simple Kerberos client tester.
32 * Derived from the Kerberos 5 SCLIENT code -
33 *   Note carefully that there are NO MIT copyrights here.
34 */
35
36#include "lp.h"
37#include "child.h"
38#include "krb5_auth.h"
39
40char *msg[] = {
41	"[-D options] [-p port] [-s service] [-k keytab] [-P principal] host file",
42	"  -D turns debugging on",
43	0
44};
45
46char *progname;
47
48void send_to_logger( int sfd, int mfd, struct job *job, const char *header, char *msg ){;}
49
50void
51usage()
52{
53	int i;
54	FPRINTF(STDERR, "usage: %s %s\n", progname, msg[0]);
55	for( i = 1; msg[i]; ++i ){
56		FPRINTF(STDERR, "%s\n", msg[i]);
57	}
58}
59
60
61int
62main(argc, argv)
63int argc;
64char *argv[];
65{
66    struct hostent *host_ent;
67    struct sockaddr_in sin;
68    int sock;
69	int port = 1234;
70	char *host;
71	int c;
72	extern int opterr, optind, getopt();
73	extern char * optarg;
74	char buffer[SMALLBUFFER];
75	char msg[SMALLBUFFER];
76	char *file;
77	char *keytab = 0;
78	char *principal = 0;
79
80	progname = argv[0];
81	while( (c = getopt(argc, argv, "D:p:s:k:P:")) != EOF){
82		switch(c){
83		default:
84			FPRINTF(STDERR,"bad option '%c'\n", c );
85			usage(progname); exit(1); break;
86		case 'k': keytab = optarg; break;
87		case 'D': Parse_debug(optarg,1); break;
88		case 'p': port= atoi(optarg); break;
89		case 's': Set_DYN(&Kerberos_service_DYN,optarg); break;
90		case 'P': principal = optarg; break;
91		}
92	}
93	if( argc - optind != 2 ){
94		FPRINTF(STDERR,"missing host or file name\n" );
95		usage(progname);
96		exit(1);
97	}
98	host = argv[optind++];
99	file = argv[optind++];
100
101    /* clear out the structure first */
102    (void) memset((char *)&sin, 0, sizeof(sin));
103	if(Kerberos_service_DYN == 0 ) Set_DYN(&Kerberos_service_DYN,"lpr");
104	if( principal ){
105		FPRINTF(STDERR, "using '%s'\n", principal );
106	} else {
107		remote_principal_krb5( Kerberos_service_DYN, host, buffer, sizeof(buffer) );
108		FPRINTF(STDERR, "default remote principal '%s'\n", buffer );
109		principal = buffer;
110	}
111
112    /* look up the server host */
113    host_ent = gethostbyname(host);
114    if(host_ent == 0){
115		FPRINTF(STDERR, "unknown host %s\n",host);
116		exit(1);
117    }
118
119    /* set up the address of the foreign socket for connect() */
120    sin.sin_family = host_ent->h_addrtype;
121    (void) memcpy((char *)&sin.sin_addr, (char *)host_ent->h_addr,
122		 sizeof(host_ent->h_addr));
123	sin.sin_port = htons(port);
124
125    /* open a TCP socket */
126    sock = socket(PF_INET, SOCK_STREAM, 0);
127#ifdef WINDOW_1
128int windowsize=1024;
129setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));
130aaaaaa=fopen("/tmp/qqqqq", "a");
131fprintf(aaaaaa, "sclient: main\n");
132fclose(aaaaaa);
133#endif
134	Max_open(sock);
135    if( sock < 0 ){
136		perror("socket");
137		exit(1);
138    }
139
140    /* connect to the server */
141    if( connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
142		perror("connect");
143		close(sock);
144		exit(1);
145    }
146
147	buffer[0] = 0;
148	if( client_krb5_auth( keytab, Kerberos_service_DYN, host,
149		0, 0, 0, 0,
150		sock, buffer, sizeof(buffer), file ) ){
151		FPRINTF( STDERR, "client_krb5_auth failed: %s\n", buffer );
152		exit(1);
153	}
154	fflush(STDOUT);
155	fflush(STDERR);
156	SNPRINTF(msg, sizeof(msg))"starting read from %d\n", sock );
157	write(1,msg, safestrlen(msg) );
158	while( (c = read( sock, buffer, sizeof(buffer) ) ) > 0 ){
159		buffer[c] = 0;
160		SNPRINTF(msg, sizeof(msg))
161			"read %d from fd %d '%s'\n", c, sock, buffer );
162		write( 1, msg, safestrlen(msg) );
163	}
164	SNPRINTF(msg, sizeof(msg))
165		"last read status %d from fd %d\n", c, sock );
166	write( 1, msg, safestrlen(msg) );
167    return(0);
168}
169
170/* VARARGS2 */
171#ifdef HAVE_STDARGS
172void setstatus (struct job *job,char *fmt,...)
173#else
174void setstatus (va_alist) va_dcl
175#endif
176{
177#ifndef HAVE_STDARGS
178    struct job *job;
179    char *fmt;
180#endif
181	char msg[LARGEBUFFER];
182    VA_LOCAL_DECL
183
184    VA_START (fmt);
185    VA_SHIFT (job, struct job * );
186    VA_SHIFT (fmt, char *);
187
188	msg[0] = 0;
189	if( Verbose ){
190		(void) VSNPRINTF( msg, sizeof(msg)-2) fmt, ap);
191		strcat( msg,"\n" );
192		if( Write_fd_str( 2, msg ) < 0 ) cleanup(0);
193	}
194	VA_END;
195	return;
196}
197
198
199/* VARARGS2 */
200#ifdef HAVE_STDARGS
201void setmessage (struct job *job,const char *header, char *fmt,...)
202#else
203void setmessage (va_alist) va_dcl
204#endif
205{
206#ifndef HAVE_STDARGS
207    struct job *job;
208    char *fmt, *header;
209#endif
210	char msg[LARGEBUFFER];
211    VA_LOCAL_DECL
212
213    VA_START (fmt);
214    VA_SHIFT (job, struct job * );
215    VA_SHIFT (header, char * );
216    VA_SHIFT (fmt, char *);
217
218	msg[0] = 0;
219	if( Verbose ){
220		(void) VSNPRINTF( msg, sizeof(msg)-2) fmt, ap);
221		strcat( msg,"\n" );
222		if( Write_fd_str( 2, msg ) < 0 ) cleanup(0);
223	}
224	VA_END;
225	return;
226}
227
228