]>
diplodocus.org Git - nmh/blob - zotnet/mts/client.c
6d86057831c43e05901db9b8434b8c9639215333
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>
41 int a_addrtype
; /* assumes AF_INET for inet_netof () */
48 #define a_net un.un_net
49 #define a_addr un.un_addr
51 static struct addrent
*n1
, *n2
;
52 static struct addrent nets
[MAXNETS
];
53 static struct addrent
*h1
, *h2
;
54 static struct addrent hosts
[MAXHOSTS
];
57 static CREDENTIALS cred
;
58 static MSG_DAT msg_data
;
59 static KTEXT ticket
= (KTEXT
) NULL
;
60 static Key_schedule schedule
;
61 static char *kservice
; /* "pop" if using kpop */
62 char krb_realm
[REALM_SZ
];
63 char *PrincipalHostname();
66 #if defined(BIND) && !defined(h_addr)
67 # define h_addr h_addr_list[0]
70 #define inaddr_copy(hp,sin) \
71 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
76 static int rcaux (struct servent
*, struct hostent
*, int, char *, int);
77 static int getport (int, int, char *, int);
78 static int inet (struct hostent
*, int);
79 struct hostent
*gethostbystring (char *s
);
81 /* client's own static version of several nmh subroutines */
82 static char **client_brkstring (char *, char *, char *);
83 static int client_brkany (char, char *);
84 static char **client_copyip (char **, char **, int);
85 static char *client_getcpy (char *);
89 client (char *args
, char *protocol
, char *service
, int rproto
,
90 char *response
, int len_response
)
94 char *arguments
[MAXARGS
];
95 register struct hostent
*hp
;
96 register struct servent
*sp
;
98 register struct netent
*np
;
105 if (cp
= strchr (service
, '/')) { /* "pop/kpop" */
106 *cp
++ = '\0'; /* kservice = "pop" */
107 service
= cp
; /* service = "kpop" */
109 kservice
= NULL
; /* not using KERBEROS */
114 if ((sp
= getservbyname (service
, protocol
)) == NULL
) {
116 if ((sp
= hes_getservbyname (service
, protocol
)) == NULL
) {
117 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
121 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
127 if (args
!= NULL
&& *args
!= 0) {
128 ap
= client_copyip (client_brkstring (client_getcpy (args
), " ", "\n"),
131 if (servers
!= NULL
&& *servers
!= 0)
132 ap
= client_copyip (client_brkstring (client_getcpy (servers
), " ", "\n"),
135 if (ap
== arguments
) {
136 *ap
++ = client_getcpy ("localhost");
141 n2
= nets
+ sizeof(nets
) / sizeof(nets
[0]);
144 h2
= hosts
+ sizeof(hosts
) / sizeof(hosts
[0]);
146 for (ap
= arguments
; *ap
; ap
++) {
149 if ((np
= getnetbyname (*ap
+ 1))) {
151 while ((hp
= gethostent()))
152 if (np
->n_addrtype
== hp
->h_addrtype
153 && inet (hp
, np
->n_net
)) {
154 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
172 if ((hp
= gethostbystring (*ap
))) {
173 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
187 strncpy (response
, "no servers available", len_response
);
193 rcaux (struct servent
*sp
, struct hostent
*hp
, int rproto
,
194 char *response
, int len_response
)
198 register struct addrent
*ap
;
199 struct sockaddr_in in_socket
;
200 register struct sockaddr_in
*isock
= &in_socket
;
206 for (ap
= nets
; ap
< n1
; ap
++)
207 if (ap
->a_addrtype
== hp
->h_addrtype
&& inet (hp
, ap
->a_net
))
210 for (ap
= hosts
; ap
< h1
; ap
++)
211 if (ap
->a_addrtype
== hp
->h_addrtype
212 && memcmp(ap
->a_addr
, hp
->h_addr
, hp
->h_length
) == 0)
215 if ((sd
= getport (rproto
, hp
->h_addrtype
, response
, len_response
)) == NOTOK
)
218 memset (isock
, 0, sizeof(*isock
));
219 isock
->sin_family
= hp
->h_addrtype
;
220 inaddr_copy (hp
, isock
);
221 isock
->sin_port
= sp
->s_port
;
223 if (connect (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) == NOTOK
)
229 n1
->a_addrtype
= hp
->h_addrtype
;
230 memcpy(&in
, hp
->h_addr
, sizeof(in
));
231 n1
->a_net
= inet_netof (in
);
241 h1
->a_addrtype
= hp
->h_addrtype
;
242 memcpy(h1
->a_addr
, hp
->h_addr
, hp
->h_length
);
249 if (kservice
) { /* "pop" */
252 if ((instance
= strdup (hp
->h_name
)) == NULL
) {
254 strncpy (response
, "Out of memory.", len_response
);
257 ticket
= (KTEXT
) malloc (sizeof(KTEXT_ST
));
258 rem
= krb_sendauth (0L, sd
, ticket
, kservice
, instance
,
259 (char *) krb_realmofhost (instance
),
260 (unsigned long) 0, &msg_data
, &cred
, schedule
,
261 (struct sockaddr_in
*) NULL
,
262 (struct sockaddr_in
*) NULL
,
265 if (rem
!= KSUCCESS
) {
267 strncpy (response
, "Post office refused connection: ", len_response
);
268 strncat (response
, krb_err_txt
[rem
], len_response
- strlen(response
));
279 getport (int rproto
, int addrtype
, char *response
, int len_response
)
282 struct sockaddr_in in_socket
, *isock
;
285 if (rproto
&& addrtype
!= AF_INET
) {
286 snprintf (response
, len_response
, "reserved ports not supported for af=%d", addrtype
);
291 if ((sd
= socket (AF_INET
, SOCK_STREAM
, 0)) == NOTOK
) {
294 if ((s
= strerror (errno
)))
295 snprintf (response
, len_response
, "unable to create socket: %s", s
);
297 snprintf (response
, len_response
, "unable to create socket: unknown error");
301 if (kservice
) /* "pop" */
307 memset(isock
, 0, sizeof(*isock
));
308 isock
->sin_family
= addrtype
;
309 for (port
= IPPORT_RESERVED
- 1;;) {
310 isock
->sin_port
= htons ((unsigned short) port
);
311 if (bind (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) != NOTOK
)
319 if (--port
<= IPPORT_RESERVED
/ 2) {
320 strncpy (response
, "ports available", len_response
);
326 if ((s
= strerror (errno
)))
327 snprintf (response
, len_response
, "unable to bind socket: %s", s
);
329 snprintf (response
, len_response
, "unable to bind socket: unknown error");
337 inet (struct hostent
*hp
, int net
)
341 memcpy(&in
, hp
->h_addr
, sizeof(in
));
342 return (inet_netof (in
) == net
);
347 * taken from ISODE's compat/internet.c
350 static char *empty
= NULL
;
353 static char *addrs
[2] = { NULL
};
357 gethostbystring (char *s
)
359 register struct hostent
*h
;
360 static struct hostent hs
;
362 static struct in_addr iaddr
;
364 static unsigned long iaddr
;
367 iaddr
= inet_addr (s
);
369 if (iaddr
.s_addr
== NOTOK
&& strcmp (s
, "255.255.255.255"))
371 if (((int) iaddr
== NOTOK
) && strcmp (s
, "255.255.255.255"))
373 return gethostbyname (s
);
377 h
->h_aliases
= &empty
;
378 h
->h_addrtype
= AF_INET
;
379 h
->h_length
= sizeof(iaddr
);
381 h
->h_addr_list
= addrs
;
382 memset(addrs
, 0, sizeof(addrs
));
384 h
->h_addr
= (char *) &iaddr
;
391 * static copies of three nmh subroutines
394 static char *broken
[MAXARGS
+ 1];
397 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
400 register char c
, *sp
;
404 for (bi
= 0; bi
< MAXARGS
; bi
++) {
405 while (client_brkany (c
= *sp
, brksep
))
407 if (!c
|| client_brkany (c
, brkterm
)) {
414 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
424 * returns 1 if chr in strg, 0 otherwise
427 client_brkany (char chr
, char *strg
)
432 for (sp
= strg
; *sp
; sp
++)
440 * copy a string array and return pointer to end
443 client_copyip (char **p
, char **q
, int len_q
)
445 while (*p
&& --len_q
> 0)
455 client_getcpy (char *str
)
460 len
= strlen(str
) + 1;
461 if (!(cp
= malloc(len
)))
464 memcpy (cp
, str
, len
);