273 lines
8.1 KiB
C
273 lines
8.1 KiB
C
/*
|
|
* Linux smoke test for NetWare AFP Alloc Temporary Directory Handle.
|
|
*
|
|
* This uses ncpfs/libncp so the request travels through the same NCP path as a
|
|
* normal Linux requester. The server-side AFP endpoint returns a temporary
|
|
* NetWare directory handle and an effective-rights mask for either a
|
|
* path-backed AFP request or an entry-id-only request.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <ncp/nwcalls.h>
|
|
#include <ncp/ncplib.h>
|
|
#include <ncp/kernel/ncp.h>
|
|
|
|
#ifndef NCPC_SUBFUNCTION
|
|
#define NCPC_SUBFUNCTION 0x10000
|
|
#endif
|
|
#ifndef NCPC_SFN
|
|
#define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION)
|
|
#endif
|
|
|
|
#define AFP_ALLOC_TEMPORARY_DIR_HANDLE 0x0b
|
|
#define AFP_GET_ENTRY_ID_FROM_PATH_NAME 0x0c
|
|
#define NWE_INVALID_NAMESPACE 0xbf
|
|
#define NWE_INVALID_PATH 0x9c
|
|
#define AFP_TEMP_DH_NONE 0xff
|
|
|
|
static void usage(const char *prog)
|
|
{
|
|
fprintf(stderr,
|
|
"Usage: %s [--entry-id-only] [--allow-invalid-namespace] "
|
|
"[--allow-invalid-path] [--volume N] [--entry-id ID] "
|
|
"[ncpfs options] PATH\n"
|
|
"\n"
|
|
"ncpfs options are parsed by ncp_initialize(), for example:\n"
|
|
" -S SERVER -U USER -P PASSWORD -n\n"
|
|
"\n"
|
|
"Examples:\n"
|
|
" %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC\n"
|
|
" %s --entry-id-only -S MARS -U SUPERVISOR -P secret SYS:PUBLIC\n"
|
|
" %s --allow-invalid-namespace -S MARS -U SUPERVISOR -P secret SYS:PUBLIC\n"
|
|
" %s --allow-invalid-path -S MARS -U SUPERVISOR -P secret SYS:NO_SUCH_PATH\n",
|
|
prog, prog, prog, prog, prog);
|
|
}
|
|
|
|
static int parse_u32(const char *text, uint32_t *value)
|
|
{
|
|
char *end = NULL;
|
|
unsigned long v;
|
|
|
|
errno = 0;
|
|
v = strtoul(text, &end, 0);
|
|
if (errno || !end || *end || v > 0xffffffffUL)
|
|
return -1;
|
|
*value = (uint32_t)v;
|
|
return 0;
|
|
}
|
|
|
|
static void cpu_to_be32(uint32_t v, uint8_t p[4])
|
|
{
|
|
p[0] = (uint8_t)(v >> 24);
|
|
p[1] = (uint8_t)(v >> 16);
|
|
p[2] = (uint8_t)(v >> 8);
|
|
p[3] = (uint8_t)v;
|
|
}
|
|
|
|
static uint32_t be32_to_cpu(const uint8_t p[4])
|
|
{
|
|
return ((uint32_t)p[0] << 24) |
|
|
((uint32_t)p[1] << 16) |
|
|
((uint32_t)p[2] << 8) |
|
|
(uint32_t)p[3];
|
|
}
|
|
|
|
static NWCCODE afp_get_entry_id_from_path(NWCONN_HANDLE conn, const char *path,
|
|
uint32_t *entry_id)
|
|
{
|
|
NW_FRAGMENT reply;
|
|
uint8_t request[1 + 1 + 255];
|
|
uint8_t reply_buf[4];
|
|
size_t path_len = strlen(path);
|
|
NWCCODE err;
|
|
|
|
if (path_len > 255)
|
|
return NWE_INVALID_PATH;
|
|
|
|
request[0] = 0; /* directory handle 0, raw VOL:path */
|
|
request[1] = (uint8_t)path_len;
|
|
memcpy(request + 2, path, path_len);
|
|
|
|
memset(reply_buf, 0, sizeof(reply_buf));
|
|
reply.fragAddr.rw = reply_buf;
|
|
reply.fragSize = sizeof(reply_buf);
|
|
|
|
err = NWRequestSimple(conn,
|
|
NCPC_SFN(0x23, AFP_GET_ENTRY_ID_FROM_PATH_NAME),
|
|
request, 2 + path_len, &reply);
|
|
if (err)
|
|
return err;
|
|
if (reply.fragSize < 4)
|
|
return NWE_INVALID_PATH;
|
|
|
|
*entry_id = be32_to_cpu(reply_buf);
|
|
return 0;
|
|
}
|
|
|
|
static void deallocate_dir_handle(NWCONN_HANDLE conn, unsigned int dir_handle)
|
|
{
|
|
uint8_t rq[1];
|
|
|
|
if (dir_handle == AFP_TEMP_DH_NONE)
|
|
return;
|
|
|
|
rq[0] = (uint8_t)dir_handle;
|
|
(void)NWRequestSimple(conn, NCPC_SFN(0x16, 0x14), rq, sizeof(rq), NULL);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
NWCONN_HANDLE conn;
|
|
NW_FRAGMENT reply;
|
|
long init_err = 0;
|
|
const char *path = NULL;
|
|
int allow_invalid_namespace = 0;
|
|
int allow_invalid_path = 0;
|
|
int entry_id_only = 0;
|
|
uint32_t volume_number = 0;
|
|
uint32_t base_entry_id = 0;
|
|
unsigned int allocated_dir_handle = AFP_TEMP_DH_NONE;
|
|
int i;
|
|
size_t path_len;
|
|
uint8_t request[1 + 4 + 1 + 255];
|
|
uint8_t reply_buf[2];
|
|
NWCCODE err;
|
|
|
|
if (NWCallsInit(NULL, NULL)) {
|
|
fprintf(stderr, "NWCallsInit failed\n");
|
|
return 2;
|
|
}
|
|
|
|
conn = ncp_initialize(&argc, argv, 1, &init_err);
|
|
if (!conn) {
|
|
fprintf(stderr, "ncp_initialize/login failed: %ld\n", init_err);
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
if (!strcmp(argv[i], "--allow-invalid-namespace")) {
|
|
allow_invalid_namespace = 1;
|
|
} else if (!strcmp(argv[i], "--entry-id-only")) {
|
|
entry_id_only = 1;
|
|
} else if (!strcmp(argv[i], "--allow-invalid-path")) {
|
|
allow_invalid_path = 1;
|
|
} else if (!strcmp(argv[i], "--volume")) {
|
|
if (++i >= argc || parse_u32(argv[i], &volume_number) ||
|
|
volume_number > 255) {
|
|
fprintf(stderr, "invalid --volume value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--entry-id")) {
|
|
if (++i >= argc || parse_u32(argv[i], &base_entry_id)) {
|
|
fprintf(stderr, "invalid --entry-id value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 0;
|
|
} else if (!path) {
|
|
path = argv[i];
|
|
} else {
|
|
fprintf(stderr, "unexpected argument: %s\n", argv[i]);
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (!path) {
|
|
fprintf(stderr, "missing PATH\n");
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
if (entry_id_only && !base_entry_id) {
|
|
err = afp_get_entry_id_from_path(conn, path, &base_entry_id);
|
|
if (err) {
|
|
fprintf(stderr,
|
|
"AFP Get Entry ID From Path Name failed before entry-id-only temporary handle allocation: completion=0x%02x (%u) path=%s\n",
|
|
(unsigned int)err & 0xff, (unsigned int)err, path);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
if (!base_entry_id) {
|
|
fprintf(stderr, "AFP Get Entry ID From Path Name returned zero for %s\n", path);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
path_len = entry_id_only ? 0 : strlen(path);
|
|
if (path_len > 255) {
|
|
fprintf(stderr,
|
|
"PATH is too long for AFP Alloc Temporary Directory Handle: %zu\n",
|
|
path_len);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
request[0] = (uint8_t)volume_number;
|
|
cpu_to_be32(base_entry_id, request + 1);
|
|
request[5] = (uint8_t)path_len;
|
|
if (path_len)
|
|
memcpy(request + 6, path, path_len);
|
|
|
|
memset(reply_buf, 0, sizeof(reply_buf));
|
|
reply.fragAddr.rw = reply_buf;
|
|
reply.fragSize = sizeof(reply_buf);
|
|
|
|
err = NWRequestSimple(conn,
|
|
NCPC_SFN(0x23, AFP_ALLOC_TEMPORARY_DIR_HANDLE),
|
|
request,
|
|
6 + path_len,
|
|
&reply);
|
|
|
|
if (((unsigned int)err & 0xff) == NWE_INVALID_NAMESPACE &&
|
|
allow_invalid_namespace) {
|
|
printf("AFP Alloc Temporary Dir Handle returned invalid namespace "
|
|
"as expected: path=%s\n", path);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
|
|
if (((unsigned int)err & 0xff) == NWE_INVALID_PATH && allow_invalid_path) {
|
|
printf("AFP Alloc Temporary Dir Handle returned invalid path "
|
|
"as expected: path=%s\n", path);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
|
|
if (err) {
|
|
fprintf(stderr,
|
|
"AFP Alloc Temporary Dir Handle failed: completion=0x%02x (%u) path=%s\n",
|
|
(unsigned int)err & 0xff, (unsigned int)err, path);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
if (reply.fragSize < 2) {
|
|
fprintf(stderr, "short AFP reply: %zu bytes\n", reply.fragSize);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
allocated_dir_handle = reply_buf[0];
|
|
printf("AFP Alloc Temporary Dir Handle path=%s entry_id=0x%08x dir_handle=%u rights=0x%02x%s\n",
|
|
path, (unsigned int)base_entry_id, allocated_dir_handle,
|
|
(unsigned int)reply_buf[1], entry_id_only ? " entry-id-only" : "");
|
|
|
|
deallocate_dir_handle(conn, allocated_dir_handle);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|