]>
diplodocus.org Git - nmh/blob - zotnet/mts/client.c
3 * client.c -- connect to a server
11 #include <sys/socket.h>
12 #include <netinet/in.h>
15 #ifdef HAVE_ARPA_INET_H
16 # include <arpa/inet.h>
39 int a_addrtype
; /* assumes AF_INET for inet_netof () */
46 #define a_net un.un_net
47 #define a_addr un.un_addr
49 static struct addrent
*n1
, *n2
;
50 static struct addrent nets
[MAXNETS
];
51 static struct addrent
*h1
, *h2
;
52 static struct addrent hosts
[MAXHOSTS
];
55 static CREDENTIALS cred
;
56 static MSG_DAT msg_data
;
57 static KTEXT ticket
= (KTEXT
) NULL
;
58 static Key_schedule schedule
;
59 static char *kservice
; /* "pop" if using kpop */
60 char krb_realm
[REALM_SZ
];
61 char *PrincipalHostname();
64 #if defined(BIND) && !defined(h_addr)
65 # define h_addr h_addr_list[0]
68 #define inaddr_copy(hp,sin) \
69 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
74 static int rcaux (struct servent
*, struct hostent
*, int, char *, int);
75 static int getport (int, int, char *, int);
76 static int inet (struct hostent
*, int);
77 struct hostent
*gethostbystring (char *s
);
79 /* client's own static version of several nmh subroutines */
80 static char **client_brkstring (char *, char *, char *);
81 static int client_brkany (char, char *);
82 static char **client_copyip (char **, char **, int);
83 static char *client_getcpy (char *);
87 client (char *args
, char *protocol
, char *service
, int rproto
,
88 char *response
, int len_response
)
92 char *arguments
[MAXARGS
];
93 register struct hostent
*hp
;
94 register struct servent
*sp
;
96 register struct netent
*np
;
103 if (cp
= strchr (service
, '/')) { /* "pop/kpop" */
104 *cp
++ = '\0'; /* kservice = "pop" */
105 service
= cp
; /* service = "kpop" */
107 kservice
= NULL
; /* not using KERBEROS */
112 if ((sp
= getservbyname (service
, protocol
)) == NULL
) {
114 if ((sp
= hes_getservbyname (service
, protocol
)) == NULL
) {
115 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
119 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
125 if (args
!= NULL
&& *args
!= 0) {
126 ap
= client_copyip (client_brkstring (client_getcpy (args
), " ", "\n"),
129 if (servers
!= NULL
&& *servers
!= 0)
130 ap
= client_copyip (client_brkstring (client_getcpy (servers
), " ", "\n"),
133 if (ap
== arguments
) {
134 *ap
++ = client_getcpy ("localhost");
139 n2
= nets
+ sizeof(nets
) / sizeof(nets
[0]);
142 h2
= hosts
+ sizeof(hosts
) / sizeof(hosts
[0]);
144 for (ap
= arguments
; *ap
; ap
++) {
147 if ((np
= getnetbyname (*ap
+ 1))) {
149 while ((hp
= gethostent()))
150 if (np
->n_addrtype
== hp
->h_addrtype
151 && inet (hp
, np
->n_net
)) {
152 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
170 if ((hp
= gethostbystring (*ap
))) {
171 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
185 strncpy (response
, "no servers available", len_response
);
191 rcaux (struct servent
*sp
, struct hostent
*hp
, int rproto
,
192 char *response
, int len_response
)
196 register struct addrent
*ap
;
197 struct sockaddr_in in_socket
;
198 register struct sockaddr_in
*isock
= &in_socket
;
204 for (ap
= nets
; ap
< n1
; ap
++)
205 if (ap
->a_addrtype
== hp
->h_addrtype
&& inet (hp
, ap
->a_net
))
208 for (ap
= hosts
; ap
< h1
; ap
++)
209 if (ap
->a_addrtype
== hp
->h_addrtype
210 && memcmp(ap
->a_addr
, hp
->h_addr
, hp
->h_length
) == 0)
213 if ((sd
= getport (rproto
, hp
->h_addrtype
, response
, len_response
)) == NOTOK
)
216 memset (isock
, 0, sizeof(*isock
));
217 isock
->sin_family
= hp
->h_addrtype
;
218 inaddr_copy (hp
, isock
);
219 isock
->sin_port
= sp
->s_port
;
221 if (connect (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) == NOTOK
)
227 n1
->a_addrtype
= hp
->h_addrtype
;
228 memcpy(&in
, hp
->h_addr
, sizeof(in
));
229 n1
->a_net
= inet_netof (in
);
239 h1
->a_addrtype
= hp
->h_addrtype
;
240 memcpy(h1
->a_addr
, hp
->h_addr
, hp
->h_length
);
247 if (kservice
) { /* "pop" */
250 if ((instance
= strdup (hp
->h_name
)) == NULL
) {
252 strncpy (response
, "Out of memory.", len_response
);
255 ticket
= (KTEXT
) malloc (sizeof(KTEXT_ST
));
256 rem
= krb_sendauth (0L, sd
, ticket
, kservice
, instance
,
257 (char *) krb_realmofhost (instance
),
258 (unsigned long) 0, &msg_data
, &cred
, schedule
,
259 (struct sockaddr_in
*) NULL
,
260 (struct sockaddr_in
*) NULL
,
263 if (rem
!= KSUCCESS
) {
265 strncpy (response
, "Post office refused connection: ", len_response
);
266 strncat (response
, krb_err_txt
[rem
], len_response
- strlen(response
));
277 getport (int rproto
, int addrtype
, char *response
, int len_response
)
280 struct sockaddr_in in_socket
, *isock
;
283 if (rproto
&& addrtype
!= AF_INET
) {
284 snprintf (response
, len_response
, "reserved ports not supported for af=%d", addrtype
);
289 if ((sd
= socket (AF_INET
, SOCK_STREAM
, 0)) == NOTOK
) {
292 if ((s
= strerror (errno
)))
293 snprintf (response
, len_response
, "unable to create socket: %s", s
);
295 snprintf (response
, len_response
, "unable to create socket: unknown error");
299 if (kservice
) /* "pop" */
305 memset(isock
, 0, sizeof(*isock
));
306 isock
->sin_family
= addrtype
;
307 for (port
= IPPORT_RESERVED
- 1;;) {
308 isock
->sin_port
= htons ((unsigned short) port
);
309 if (bind (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) != NOTOK
)
317 if (--port
<= IPPORT_RESERVED
/ 2) {
318 strncpy (response
, "ports available", len_response
);
324 if ((s
= strerror (errno
)))
325 snprintf (response
, len_response
, "unable to bind socket: %s", s
);
327 snprintf (response
, len_response
, "unable to bind socket: unknown error");
335 inet (struct hostent
*hp
, int net
)
339 memcpy(&in
, hp
->h_addr
, sizeof(in
));
340 return (inet_netof (in
) == net
);
345 * taken from ISODE's compat/internet.c
348 static char *empty
= NULL
;
351 static char *addrs
[2] = { NULL
};
355 gethostbystring (char *s
)
357 register struct hostent
*h
;
358 static struct hostent hs
;
360 static struct in_addr iaddr
;
362 static unsigned long iaddr
;
365 iaddr
= inet_addr (s
);
367 if (iaddr
.s_addr
== NOTOK
&& strcmp (s
, "255.255.255.255"))
369 if (((int) iaddr
== NOTOK
) && strcmp (s
, "255.255.255.255"))
371 return gethostbyname (s
);
375 h
->h_aliases
= &empty
;
376 h
->h_addrtype
= AF_INET
;
377 h
->h_length
= sizeof(iaddr
);
379 h
->h_addr_list
= addrs
;
380 memset(addrs
, 0, sizeof(addrs
));
382 h
->h_addr
= (char *) &iaddr
;
389 * static copies of three nmh subroutines
392 static char *broken
[MAXARGS
+ 1];
395 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
398 register char c
, *sp
;
402 for (bi
= 0; bi
< MAXARGS
; bi
++) {
403 while (client_brkany (c
= *sp
, brksep
))
405 if (!c
|| client_brkany (c
, brkterm
)) {
412 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
422 * returns 1 if chr in strg, 0 otherwise
425 client_brkany (char chr
, char *strg
)
430 for (sp
= strg
; *sp
; sp
++)
438 * copy a string array and return pointer to end
441 client_copyip (char **p
, char **q
, int len_q
)
443 while (*p
&& --len_q
> 0)
453 client_getcpy (char *str
)
458 len
= strlen(str
) + 1;
459 if (!(cp
= malloc(len
)))
462 memcpy (cp
, str
, len
);