Hi,
I'm trying to write test programs for auth_user/server_authenticate (and
later for proc_user/server_authenticate) and have problems to get them
right. The attached programs generate the following output:
./auth_recv&
./auth_send
auth_recv.c: Socket file descriptor = 3
auth_recv.c: Receiving via datagram socket
auth_send.c: Socket file descriptor = 3
auth_send.c: Sending via datagram socket
auth_send.c: rendezvous = 128
auth_send.c: mach_port_insert_right() err = 0
auth_send.c: Sent rendezvous port = 128
auth_send.c: send() returned 4 bytes
auth_recv.c: recv() returned 4 bytes
auth_recv.c: Received data = 128
auth_recv.c: rendezvous valid
auth_recv.c: mach_port_mod_refs() err = 15
auth_recv.c: err = 15
EKERN_INVALID_NAME = 15
trying with mach_port_insert_right() gives error 18:
EKERN_INVALID_VALUE = 18
What's missing/wrong here?
Do I have to reauthenticate the received rendezvous port in auth_recv?
/* test of auth_user/server authenticate: send part */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <unistd.h>
#include <hurd/hurd_types.h>
#include <hurd/fd.h>
#include <mach.h>
#include <hurd.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCK_PATH "auth_socket"
int main (void)
{
mach_port_t rendezvous = NULL;
mach_port_t newport;
error_t err;
int sfd;
struct sockaddr_un addr = { 0 };
ssize_t ns;
char wbuf[30];
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1);
sfd = socket (AF_UNIX, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror ("socket");
exit(EXIT_FAILURE);
}
printf("auth_send.c: Socket file descriptor = %d\n", sfd);
if (connect(sfd, (struct sockaddr *) &addr,
sizeof(struct sockaddr_un)) == -1)
{
close(sfd);
perror ("connect");
exit(EXIT_FAILURE);
}
printf("auth_send.c: Sending via datagram socket\n");
rendezvous = __mach_reply_port ();
printf ("auth_send.c: rendezvous = %d\n", (int)rendezvous);
err = mach_port_insert_right (mach_task_self (), rendezvous,
rendezvous, MACH_MSG_TYPE_MAKE_SEND);
printf ("auth_send.c: mach_port_insert_right() err = %d\n", err);
if (err)
goto out;
sprintf(wbuf, "%d", (int)rendezvous);
printf("auth_send.c: Sent rendezvous port = %d\n", (int)rendezvous);
#if 0
ns = send(sfd, wbuf, 4, 0);
#else
err = HURD_DPORT_USE (sfd, __socket_send (port, MACH_PORT_NULL,
0, wbuf, 4,
NULL, MACH_MSG_TYPE_COPY_SEND, 0,
NULL, 0, &ns));
#endif
if (ns == -1)
{
perror ("send");
exit(EXIT_FAILURE);
}
printf("auth_send.c: send() returned %ld bytes\n", (long) ns);
// err = __USEPORT (AUTH, __auth_user_authenticate (port,
// rendezvous, MACH_MSG_TYPE_COPY_SEND,
err = __USEPORT (AUTH, auth_user_authenticate (port,
rendezvous, MACH_MSG_TYPE_MAKE_SEND,
&newport));
out:
printf ("auth_send.c: newport=%d\n", (int)newport);
__mach_port_deallocate (__mach_task_self (), rendezvous);
__mach_port_deallocate (__mach_task_self (), newport);
printf ("auth_send.c: err = %d\n", err);
return err;
}
/* test of auth_user/server authenticate: recv part */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <hurd/hurd_types.h>
#include <hurd.h>
#include <hurd/id.h>
#include <mach.h>
#include <sys/socket.h>
#define SOCK_PATH "auth_socket"
int main (void)
{
__uid_t euids_buf[CMGROUP_MAX], auids_buf[CMGROUP_MAX];
__uid_t *euids = euids_buf, *auids = auids_buf;
__gid_t egids_buf[CMGROUP_MAX], agids_buf[CMGROUP_MAX];
__gid_t *egids = egids_buf, *agids = agids_buf;
size_t neuids = CMGROUP_MAX, nauids = CMGROUP_MAX;
size_t negids = CMGROUP_MAX, nagids = CMGROUP_MAX;
mach_port_t rendezvous;
mach_port_t newport = MACH_PORT_NULL;
error_t err;
int sfd;
struct sockaddr_un addr = { 0 };
ssize_t nr;
char rbuf[30];
/* Create socket bound to well-known address */
err = remove (SOCK_PATH);
if (err == -1 && errno != ENOENT)
{
perror ("remove");
exit(EXIT_FAILURE);
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1);
sfd = socket (AF_UNIX, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror ("socket");
exit(EXIT_FAILURE);
}
printf("auth_recv.c: Socket file descriptor = %d\n", sfd);
err = bind (sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (err == -1) {
close(sfd);
perror ("bind");
exit(EXIT_FAILURE);
}
printf ("auth_recv.c: Receiving via datagram socket\n");
/* Receive data */
nr = recv (sfd, rbuf, 4, 0);
if (nr == -1)
{
perror ("recv");
exit(EXIT_FAILURE);
}
printf("auth_recv.c: recv() returned %ld bytes\n", (long) nr);
rendezvous = atoi (rbuf);
if (nr > 0)
printf("auth_recv.c: Received data = %d\n", (int)rendezvous);
if (MACH_PORT_VALID (rendezvous))
printf ("auth_recv.c: rendezvous valid\n");
else
printf ("auth_recv.c: rendezvous not valid\n");
err = mach_port_mod_refs (mach_task_self (), rendezvous,
MACH_PORT_RIGHT_SEND, +1);
if (err)
{
printf ("auth_recv.c: mach_port_mod_refs() err = %d\n", err);
mach_port_deallocate (mach_task_self (), rendezvous);
goto out;
}
do
err = __USEPORT
(AUTH, auth_server_authenticate (port,
rendezvous, MACH_MSG_TYPE_COPY_SEND,
newport, MACH_MSG_TYPE_COPY_SEND,
&euids, &neuids, &auids, &nauids,
&egids, &negids, &agids, &nagids));
while (err == EINTR);
mach_port_deallocate (mach_task_self (), rendezvous);
mach_port_deallocate (mach_task_self (), newport);
if (err)
goto out;
/* Print *id */
printf("auth_recv.c: Received ids: auid=%ld, euid=%ld, agid=%ld, egid=%ld\n",
(long) euids[0], (long) auids[0],
(long) egids[0], (long) agids[0]);
out:
printf ("auth_recv.c: err = %d\n", err);
return err;
}