ssize_t SMBC_write_ctx(SMBCCTX *context, SMBCFILE *file, const void *buf, size_t count) { off_t offset; TALLOC_CTX *frame = talloc_stackframe(); NTSTATUS status; /* First check all pointers before dereferencing them */ if (!context || !context->internal->initialized) { errno = EINVAL; TALLOC_FREE(frame); return -1; } if (!file || !SMBC_dlist_contains(context->internal->files, file)) { errno = EBADF; TALLOC_FREE(frame); return -1; } /* Check that the buffer exists ... */ if (buf == NULL) { errno = EINVAL; TALLOC_FREE(frame); return -1; } offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */ status = cli_writeall(file->targetcli, file->cli_fd, 0, (const uint8_t *)buf, offset, count, NULL); if (!NT_STATUS_IS_OK(status)) { errno = map_errno_from_nt_status(status); TALLOC_FREE(frame); return -1; } file->offset += count; TALLOC_FREE(frame); return count; /* Success, 0 bytes of data ... */ }
void nb_writex(int handle, int offset, int size, int ret_size) { int i; NTSTATUS status; if (buf[0] == 0) memset(buf, 1, sizeof(buf)); i = find_handle(handle); status = cli_writeall(c, ftable[i].fd, 0, (uint8_t *)buf, offset, size, NULL); if (!NT_STATUS_IS_OK(status)) { printf("(%d) ERROR: write failed on handle %d, fd %d " "error %s\n", line_count, handle, ftable[i].fd, nt_errstr(status)); exit(1); } children[nbio_id].bytes_out += ret_size; }
static int /* O - 0 = success, non-0 = failure */ smb_print(struct cli_state * cli, /* I - SMB connection */ char *title, /* I - Title/job name */ FILE * fp) { /* I - File to print */ uint16_t fnum; /* File number */ int nbytes, /* Number of bytes read */ tbytes; /* Total bytes read */ char buffer[8192], /* Buffer for copy */ *ptr; /* Pointer into title */ NTSTATUS nt_status; /* * Sanitize the title... */ for (ptr = title; *ptr; ptr++) { if (!isalnum((int) *ptr) && !isspace((int) *ptr)) { *ptr = '_'; } } /* * Open the printer device... */ nt_status = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum); if (!NT_STATUS_IS_OK(nt_status)) { fprintf(stderr, "ERROR: %s opening remote spool %s\n", nt_errstr(nt_status), title); return get_exit_code(cli, nt_status); } /* * Copy the file to the printer... */ if (fp != stdin) rewind(fp); tbytes = 0; while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) { NTSTATUS status; status = cli_writeall(cli, fnum, 0, (uint8_t *)buffer, tbytes, nbytes, NULL); if (!NT_STATUS_IS_OK(status)) { int ret = get_exit_code(cli, status); fprintf(stderr, "ERROR: Error writing spool: %s\n", nt_errstr(status)); fprintf(stderr, "DEBUG: Returning status %d...\n", ret); cli_close(cli, fnum); return (ret); } tbytes += nbytes; } nt_status = cli_close(cli, fnum); if (!NT_STATUS_IS_OK(nt_status)) { fprintf(stderr, "ERROR: %s closing remote spool %s\n", nt_errstr(nt_status), title); return get_exit_code(cli, nt_status); } else { return (0); } }
ssize_t SMBC_write_ctx(SMBCCTX *context, SMBCFILE *file, const void *buf, size_t count) { off_t offset; char *server = NULL, *share = NULL, *user = NULL, *password = NULL; char *path = NULL; char *targetpath = NULL; struct cli_state *targetcli = NULL; TALLOC_CTX *frame = talloc_stackframe(); NTSTATUS status; /* First check all pointers before dereferencing them */ if (!context || !context->internal->initialized) { errno = EINVAL; TALLOC_FREE(frame); return -1; } if (!file || !SMBC_dlist_contains(context->internal->files, file)) { errno = EBADF; TALLOC_FREE(frame); return -1; } /* Check that the buffer exists ... */ if (buf == NULL) { errno = EINVAL; TALLOC_FREE(frame); return -1; } offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */ /*d_printf(">>>write: parsing %s\n", file->fname);*/ if (SMBC_parse_path(frame, context, file->fname, NULL, &server, &share, &path, &user, &password, NULL)) { errno = EINVAL; TALLOC_FREE(frame); return -1; } /*d_printf(">>>write: resolving %s\n", path);*/ status = cli_resolve_path(frame, "", context->internal->auth_info, file->srv->cli, path, &targetcli, &targetpath); if (!NT_STATUS_IS_OK(status)) { d_printf("Could not resolve %s\n", path); errno = ENOENT; TALLOC_FREE(frame); return -1; } /*d_printf(">>>write: resolved path as %s\n", targetpath);*/ status = cli_writeall(targetcli, file->cli_fd, 0, (const uint8_t *)buf, offset, count, NULL); if (!NT_STATUS_IS_OK(status)) { errno = map_errno_from_nt_status(status); TALLOC_FREE(frame); return -1; } file->offset += count; TALLOC_FREE(frame); return count; /* Success, 0 bytes of data ... */ }
bool run_posix_append(int dummy) { struct cli_state *cli; const char *fname = "append"; NTSTATUS status; uint16_t fnum; SMB_OFF_T size; uint8_t c = '\0'; bool ret = false; printf("Starting POSIX_APPEND\n"); if (!torture_open_connection(&cli, 0)) { return false; } status = torture_setup_unix_extensions(cli); if (!NT_STATUS_IS_OK(status)) { printf("torture_setup_unix_extensions failed: %s\n", nt_errstr(status)); goto fail; } status = cli_ntcreate( cli, fname, 0, GENERIC_WRITE_ACCESS|GENERIC_READ_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_POSIX_SEMANTICS, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OVERWRITE_IF, FILE_NON_DIRECTORY_FILE|FILE_DELETE_ON_CLOSE, 0, &fnum); if (!NT_STATUS_IS_OK(status)) { printf("cli_ntcreate failed: %s\n", nt_errstr(status)); goto fail; } /* * Write two bytes at offset 0. With bug 6898 we would end up * with a file of 2 byte length. */ status = cli_writeall(cli, fnum, 0, &c, 0, sizeof(c), NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_write failed: %s\n", nt_errstr(status)); goto fail; } status = cli_writeall(cli, fnum, 0, &c, 0, sizeof(c), NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_write failed: %s\n", nt_errstr(status)); goto fail; } status = cli_getattrE(cli, fnum, NULL, &size, NULL, NULL, NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_getatrE failed: %s\n", nt_errstr(status)); goto fail; } if (size != sizeof(c)) { printf("BUG: Writing with O_APPEND!!\n"); goto fail; } ret = true; fail: torture_close_connection(cli); return ret; }
bool torture_casetable(int dummy) { static struct cli_state *cli; char *fname; uint16_t fnum; int c, i; #define MAX_EQUIVALENCE 8 smb_ucs2_t equiv[0x10000][MAX_EQUIVALENCE]; printf("starting casetable\n"); if (!torture_open_connection(&cli, 0)) { return False; } memset(equiv, 0, sizeof(equiv)); cli_unlink(cli, "\\utable\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN); cli_rmdir(cli, "\\utable"); if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\utable"))) { printf("Failed to create utable directory!\n"); return False; } for (c=1; c < 0x10000; c++) { SMB_OFF_T size; if (c == '.' || c == '\\') continue; printf("%04x (%c)\n", c, isprint(c)?c:'.'); fname = form_name(c); if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum))) { printf("Failed to create file with char %04x\n", c); continue; } size = 0; if (!NT_STATUS_IS_OK(cli_qfileinfo_basic( cli, fnum, NULL, &size, NULL, NULL, NULL, NULL, NULL))) { continue; } if (size > 0) { /* found a character equivalence! */ int c2[MAX_EQUIVALENCE]; if (size/sizeof(int) >= MAX_EQUIVALENCE) { printf("too many chars match?? size=%ld c=0x%04x\n", (unsigned long)size, c); cli_close(cli, fnum); return False; } cli_read(cli, fnum, (char *)c2, 0, size); printf("%04x: ", c); equiv[c][0] = c; for (i=0; i<size/sizeof(int); i++) { printf("%04x ", c2[i]); equiv[c][i+1] = c2[i]; } printf("\n"); fflush(stdout); } cli_writeall(cli, fnum, 0, (uint8_t *)&c, size, sizeof(c), NULL); cli_close(cli, fnum); } cli_unlink(cli, "\\utable\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN); cli_rmdir(cli, "\\utable"); return True; }