334 lines
11 KiB
C
334 lines
11 KiB
C
/*
|
|
* Linux smoke test for NetWare AFP Scan File Information.
|
|
*/
|
|
|
|
#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_SCAN_FILE_INFORMATION 0x0a
|
|
#define AFP20_SCAN_FILE_INFORMATION 0x11
|
|
#define NWE_INVALID_NAMESPACE 0xbf
|
|
#define NWE_INVALID_PATH 0x9c
|
|
#define NWE_NO_FILES_FOUND 0xff
|
|
#define AFP_SCAN_REPLY_LEN 122
|
|
#define AFP_SCAN_INFO_LEN 114
|
|
#define AFP20_SCAN_INFO_LEN 120
|
|
#define AFP_GET_ALL 0xffff
|
|
#define AFP_SEARCH_ALL 0xffff
|
|
|
|
static void usage(const char *prog)
|
|
{
|
|
fprintf(stderr,
|
|
"Usage: %s [--afp10|--afp20] [--allow-invalid-namespace] "
|
|
"[--allow-invalid-path] [--allow-empty] [--volume N] "
|
|
"[--entry-id ID] [--last-seen ID] [--desired-count N] "
|
|
"[--search-mask MASK] [--request-mask MASK] [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 --afp10 -S MARS -U SUPERVISOR -P secret SYS:PUBLIC\n"
|
|
" %s --allow-empty -S MARS -U SUPERVISOR -P secret SYS:EMPTYDIR\n",
|
|
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 uint16_t be16_to_cpu(const uint8_t p[2])
|
|
{
|
|
return ((uint16_t)p[0] << 8) | p[1];
|
|
}
|
|
|
|
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) |
|
|
p[3];
|
|
}
|
|
|
|
static void cpu_to_be16(uint16_t v, uint8_t p[2])
|
|
{
|
|
p[0] = (uint8_t)(v >> 8);
|
|
p[1] = (uint8_t)v;
|
|
}
|
|
|
|
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 void copy_fixed_string(char *dst, size_t dstlen,
|
|
const uint8_t *src, size_t srclen)
|
|
{
|
|
size_t i;
|
|
|
|
if (!dstlen)
|
|
return;
|
|
for (i = 0; i + 1 < dstlen && i < srclen && src[i]; i++)
|
|
dst[i] = (char)src[i];
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
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 allow_empty = 0;
|
|
uint32_t volume_number = 0;
|
|
uint32_t entry_id = 0;
|
|
uint32_t last_seen_id = 0;
|
|
uint32_t desired_count = 1;
|
|
uint32_t search_mask = AFP_SEARCH_ALL;
|
|
uint32_t request_mask = AFP_GET_ALL;
|
|
uint32_t afp_subfunction = AFP20_SCAN_FILE_INFORMATION;
|
|
int i;
|
|
size_t path_len;
|
|
uint8_t request[1 + 4 + 4 + 2 + 2 + 2 + 1 + 255];
|
|
uint8_t reply_buf[AFP_SCAN_REPLY_LEN];
|
|
char long_name[33];
|
|
char short_name[13];
|
|
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], "--afp10")) {
|
|
afp_subfunction = AFP_SCAN_FILE_INFORMATION;
|
|
} else if (!strcmp(argv[i], "--afp20")) {
|
|
afp_subfunction = AFP20_SCAN_FILE_INFORMATION;
|
|
} else if (!strcmp(argv[i], "--allow-invalid-namespace")) {
|
|
allow_invalid_namespace = 1;
|
|
} else if (!strcmp(argv[i], "--allow-invalid-path")) {
|
|
allow_invalid_path = 1;
|
|
} else if (!strcmp(argv[i], "--allow-empty")) {
|
|
allow_empty = 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], &entry_id)) {
|
|
fprintf(stderr, "invalid --entry-id value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--last-seen")) {
|
|
if (++i >= argc || parse_u32(argv[i], &last_seen_id)) {
|
|
fprintf(stderr, "invalid --last-seen value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--desired-count")) {
|
|
if (++i >= argc || parse_u32(argv[i], &desired_count) || desired_count > 4) {
|
|
fprintf(stderr, "invalid --desired-count value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--search-mask")) {
|
|
if (++i >= argc || parse_u32(argv[i], &search_mask) || search_mask > 0xffff) {
|
|
fprintf(stderr, "invalid --search-mask value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--request-mask")) {
|
|
if (++i >= argc || parse_u32(argv[i], &request_mask) || request_mask > 0xffff) {
|
|
fprintf(stderr, "invalid --request-mask 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;
|
|
}
|
|
|
|
path_len = strlen(path);
|
|
if (path_len > 255) {
|
|
fprintf(stderr, "PATH is too long for AFP Scan File Information: %zu\n", path_len);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
memset(request, 0, sizeof(request));
|
|
request[0] = (uint8_t)volume_number;
|
|
cpu_to_be32(entry_id, request + 1);
|
|
cpu_to_be32(last_seen_id, request + 5);
|
|
cpu_to_be16((uint16_t)desired_count, request + 9);
|
|
cpu_to_be16((uint16_t)search_mask, request + 11);
|
|
cpu_to_be16((uint16_t)request_mask, request + 13);
|
|
request[15] = (uint8_t)path_len;
|
|
memcpy(request + 16, 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, (int)afp_subfunction),
|
|
request,
|
|
16 + path_len,
|
|
&reply);
|
|
if (err == NWE_INVALID_NAMESPACE && allow_invalid_namespace) {
|
|
printf("AFP Scan File Info subfunction=0x%02x path=%s last_seen=0x%08x desired=%u "
|
|
"actual_count=%u next_last_seen=0x%08x entry_id=0x%08x parent_id=0x%08x "
|
|
"attrs=0x%04x data_len=%u resource_len=%u offspring=%u long_name=%s "
|
|
"short_name=%s rights=0x%04x reply_len=%zu\n",
|
|
(unsigned int)afp_subfunction,
|
|
path,
|
|
last_seen_id,
|
|
(unsigned int)desired_count,
|
|
be16_to_cpu(reply_buf + 0),
|
|
be32_to_cpu(reply_buf + 2),
|
|
be32_to_cpu(reply_buf + 2),
|
|
be32_to_cpu(reply_buf + 2 + 4),
|
|
be16_to_cpu(reply_buf + 2 + 8),
|
|
be32_to_cpu(reply_buf + 2 + 10),
|
|
be32_to_cpu(reply_buf + 2 + 14),
|
|
be16_to_cpu(reply_buf + 2 + 18),
|
|
long_name,
|
|
short_name,
|
|
be16_to_cpu(reply_buf + 2 + 112),
|
|
reply.fragSize);
|
|
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
if (err == NWE_INVALID_PATH && allow_invalid_path) {
|
|
printf("AFP Scan File Information subfunction=0x%02x returned invalid path as expected for path=%s\n",
|
|
(unsigned int)afp_subfunction, path);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
if (err == NWE_NO_FILES_FOUND && allow_empty) {
|
|
printf("AFP Scan File Information subfunction=0x%02x returned no files as expected for path=%s last_seen=0x%08x\n",
|
|
(unsigned int)afp_subfunction, path, last_seen_id);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
if (err) {
|
|
fprintf(stderr,
|
|
"AFP Scan File Information subfunction=0x%02x failed: completion=0x%02x (%u) path=%s last_seen=0x%08x\n",
|
|
(unsigned int)afp_subfunction, (unsigned int)err & 0xff,
|
|
(unsigned int)err, path, last_seen_id);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
{
|
|
size_t expected_record_len =
|
|
(afp_subfunction == AFP20_SCAN_FILE_INFORMATION)
|
|
? AFP20_SCAN_INFO_LEN
|
|
: AFP_SCAN_INFO_LEN;
|
|
size_t expected_reply_len = 2 + expected_record_len;
|
|
|
|
if (reply.fragSize < expected_reply_len) {
|
|
fprintf(stderr, "short AFP scan reply: %zu bytes expected_at_least=%zu\n",
|
|
reply.fragSize, expected_reply_len);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
if (be16_to_cpu(reply_buf + 0) != 1) {
|
|
fprintf(stderr, "AFP scan reply count is not WebSDK compliant: count=%u\n",
|
|
be16_to_cpu(reply_buf + 0));
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
if (afp_subfunction == AFP_SCAN_FILE_INFORMATION &&
|
|
reply.fragSize > 2 + AFP_SCAN_INFO_LEN) {
|
|
fprintf(stderr, "AFP Scan File Information returned non-WebSDK legacy size: %zu bytes\n",
|
|
reply.fragSize);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
copy_fixed_string(long_name, sizeof(long_name), reply_buf + 2 + 64, 32);
|
|
copy_fixed_string(short_name, sizeof(short_name), reply_buf + 2 + 100, 12);
|
|
|
|
printf("AFP Scan File Info subfunction=0x%02x path=%s last_seen=0x%08x desired=%u "
|
|
"actual_count=%u next_last_seen=0x%08x entry_id=0x%08x parent_id=0x%08x "
|
|
"attrs=0x%04x data_len=%u resource_len=%u offspring=%u long_name=%s "
|
|
"short_name=%s rights=0x%04x reply_len=%zu\n",
|
|
(unsigned int)afp_subfunction,
|
|
path,
|
|
last_seen_id,
|
|
(unsigned int)desired_count,
|
|
be16_to_cpu(reply_buf + 0),
|
|
be32_to_cpu(reply_buf + 2),
|
|
be32_to_cpu(reply_buf + 2),
|
|
be32_to_cpu(reply_buf + 2 + 4),
|
|
be16_to_cpu(reply_buf + 2 + 8),
|
|
be32_to_cpu(reply_buf + 2 + 10),
|
|
be32_to_cpu(reply_buf + 2 + 14),
|
|
be16_to_cpu(reply_buf + 2 + 18),
|
|
long_name,
|
|
short_name,
|
|
be16_to_cpu(reply_buf + 2 + 112),
|
|
reply.fragSize);
|
|
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|