/* * Linux smoke test for NetWare AFP Open File Fork. * * This uses ncpfs/libncp so the request travels through the same NCP path as a * normal Linux requester. The first mars_nwe implementation deliberately * exercises the conservative path-backed data-fork subset and returns a normal * NetWare file handle, which this helper closes before exiting. */ #include #include #include #include #include #include #include #include #ifndef NCPC_SUBFUNCTION #define NCPC_SUBFUNCTION 0x10000 #endif #ifndef NCPC_SFN #define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION) #endif #define AFP_OPEN_FILE_FORK 0x08 #define NWE_INVALID_NAMESPACE 0xbf #define NWE_INVALID_PATH 0x9c #define AFP_DATA_FORK 0x00 #define AFP_ACCESS_READ 0x01 static void usage(const char *prog) { fprintf(stderr, "Usage: %s [--allow-invalid-namespace] [--allow-invalid-path] " "[--expect-completion CODE] [--volume N] [--entry-id ID] " "[--fork N] [--access N] " "[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/pmdflts.ini\n" " %s --allow-invalid-namespace -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n" " %s --allow-invalid-path -S MARS -U SUPERVISOR -P secret SYS:NO_SUCH_FILE\n" " %s --expect-completion 0x84 --access 0x02 -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n" " %s --expect-completion 0x9c --fork 1 -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n", prog, 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 uint32_t le32_to_cpu(const uint8_t p[4]) { return ((uint32_t)p[0]) | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } static void cpu_to_le32(uint32_t v, uint8_t p[4]) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } static void close_file_handle(NWCONN_HANDLE conn, uint32_t fhandle) { uint8_t rq[7]; memset(rq, 0, sizeof(rq)); cpu_to_le32(fhandle, rq + 3); (void)NWRequestSimple(conn, 0x42, 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 expected_completion = -1; uint32_t volume_number = 0; uint32_t base_entry_id = 0; uint32_t fork_indicator = AFP_DATA_FORK; uint32_t access_mode = AFP_ACCESS_READ; int i; size_t path_len; uint8_t request[1 + 4 + 1 + 1 + 1 + 255]; uint8_t reply_buf[10]; uint32_t fhandle; uint32_t fork_len; 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], "--allow-invalid-path")) { allow_invalid_path = 1; } else if (!strcmp(argv[i], "--expect-completion")) { uint32_t expected; if (++i >= argc || parse_u32(argv[i], &expected) || expected > 255) { fprintf(stderr, "invalid --expect-completion value\n"); ncp_close(conn); return 2; } expected_completion = (int)expected; } 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], "--fork")) { if (++i >= argc || parse_u32(argv[i], &fork_indicator) || fork_indicator > 255) { fprintf(stderr, "invalid --fork value\n"); ncp_close(conn); return 2; } } else if (!strcmp(argv[i], "--access")) { if (++i >= argc || parse_u32(argv[i], &access_mode) || access_mode > 255) { fprintf(stderr, "invalid --access 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 Open File Fork: %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)fork_indicator; request[6] = (uint8_t)access_mode; request[7] = (uint8_t)path_len; memcpy(request + 8, 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_OPEN_FILE_FORK), request, 8 + path_len, &reply); if (expected_completion >= 0) { if (((unsigned int)err & 0xff) == (unsigned int)expected_completion) { printf("AFP Open File Fork returned expected completion 0x%02x: path=%s fork=%u access=0x%02x\n", expected_completion, path, (unsigned int)fork_indicator, (unsigned int)access_mode); ncp_close(conn); return 0; } if (!err) { fprintf(stderr, "AFP Open File Fork unexpectedly succeeded: expected=0x%02x path=%s fork=%u access=0x%02x\n", expected_completion, path, (unsigned int)fork_indicator, (unsigned int)access_mode); if (reply.fragSize >= 6) close_file_handle(conn, le32_to_cpu(reply_buf + 2)); ncp_close(conn); return 1; } fprintf(stderr, "AFP Open File Fork returned completion 0x%02x, expected 0x%02x: path=%s fork=%u access=0x%02x\n", (unsigned int)err & 0xff, expected_completion, path, (unsigned int)fork_indicator, (unsigned int)access_mode); ncp_close(conn); return 1; } if (((unsigned int)err & 0xff) == NWE_INVALID_NAMESPACE && allow_invalid_namespace) { printf("AFP Open File Fork 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 Open File Fork returned invalid path as expected: path=%s\n", path); ncp_close(conn); return 0; } if (err) { fprintf(stderr, "AFP Open File Fork failed: completion=0x%02x (%u) path=%s\n", (unsigned int)err & 0xff, (unsigned int)err, path); ncp_close(conn); return 1; } if (reply.fragSize < 10) { fprintf(stderr, "short AFP reply: %zu bytes\n", reply.fragSize); ncp_close(conn); return 1; } fhandle = le32_to_cpu(reply_buf + 2); fork_len = be32_to_cpu(reply_buf + 6); printf("AFP Open File Fork path=%s handle=%u fork=%u access=0x%02x fork_len=%u\n", path, fhandle, (unsigned int)fork_indicator, (unsigned int)access_mode, fork_len); close_file_handle(conn, fhandle); ncp_close(conn); return 0; }