示例#1
0
static bool stream_until_newline(w_jbuffer_t *reader, w_stm_t stm)
{
  int x;
  char *buf, *nl;
  bool is_done = false;

  while (true) {
    buf = reader->buf + reader->rpos;
    nl = memchr(buf, '\n', reader->wpos - reader->rpos);
    if (nl) {
      x = 1 + (int)(nl - buf);
      is_done = true;
    } else {
      x = reader->wpos - reader->rpos;
    }

    if (!output_bytes(buf, x)) {
      return false;
    }
    buf += x;
    reader->rpos += x;

    if (is_done) {
      break;
    }

    if (!fill_buffer(reader, stm)) {
      break;
    }
  }
  return true;
}
示例#2
0
static int perf_event__repipe_synth(struct perf_tool *tool,
				    union perf_event *event)
{
	struct perf_inject *inject = container_of(tool, struct perf_inject,
						  tool);

	return output_bytes(inject, event, event->header.size);
}
示例#3
0
static bool stream_n_bytes(w_jbuffer_t *jr, w_stm_t stm, json_int_t len,
    json_error_t *jerr)
{
  uint32_t total = 0;

  if (!output_bytes(jr->buf, jr->rpos)) {
    snprintf(jerr->text, sizeof(jerr->text),
        "failed output headers bytes %d: %s\n",
        jr->rpos, strerror(errno));
    return false;
  }
  while (len > 0) {
    uint32_t avail = jr->wpos - jr->rpos;
    int r;

    if (avail) {
      if (!output_bytes(jr->buf + jr->rpos, avail)) {
        snprintf(jerr->text, sizeof(jerr->text),
            "output_bytes: avail=%d, failed %s\n",
            avail, strerror(errno));
        return false;
      }
      jr->rpos += avail;
      len -= avail;

      if (len == 0) {
        return true;
      }
    }

    avail = MIN((uint32_t)len, shunt_down(jr));
    r = w_stm_read(stm, jr->buf + jr->wpos, avail);

    if (r <= 0) {
      snprintf(jerr->text, sizeof(jerr->text),
        "read: len=%"PRIi64" wanted %"PRIu32" got %d %s\n",
        (int64_t)len, avail,
        r, strerror(errno));
      return false;
    }
    jr->wpos += r;
    total += r;
  }
  return true;
}
示例#4
0
static void
output_line (struct line_reader *r, struct string *s, size_t n)
{
  int unit = r->encoding_info.unit;

  output_bytes (r, s, n);

  r->head += unit;
  r->length -= unit;

  ds_chomp (s, ss_buffer (r->encoding_info.cr, unit));
}
示例#5
0
/* ARGSUSED */
static void
interpret_trans2(int flags, uchar_t *data, int len, char *xtra, int xsz)
{
	struct smb *smb;
	uchar_t *vwv; /* word parameters */
	int wordcount;
	uchar_t *byteparms;
	int bytecount;
	int parambytes;
	int paramoffset;
	int setupcount;
	int subcode;
	uchar_t *setupdata;
	uchar_t *params;
	char *name;

	smb  = (struct smb *)data;
	vwv = (uchar_t *)data + sizeof (struct smb);
	wordcount = *vwv++;

	byteparms = vwv + (2 * wordcount);
	bytecount = get2(byteparms);
	byteparms += 2;

	/*
	 * Print the lengths before we (potentially) bail out
	 * due to lack of data (so the user knows why we did).
	 */
	if (flags & F_DTAIL) {
		show_printf("WordCount = %d", wordcount);
		show_printf("ByteCount = %d", bytecount);
	}

	/* Get length and location of params and setup data. */
	if (!(smb->flags & SERVER_RESPONSE)) {
		/* CALL */
		if (wordcount < 14)
			return;
		parambytes  = get2(vwv + (2 *  9));
		paramoffset = get2(vwv + (2 * 10));
		setupcount = *(vwv + (2 * 13));
		setupdata  =   vwv + (2 * 14);
	} else {
		/* REPLY */
		if (wordcount < 10)
			return;
		parambytes  = get2(vwv + (2 * 3));
		paramoffset = get2(vwv + (2 * 4));
		setupcount = *(vwv + (2 *  9));
		setupdata  =   vwv + (2 * 10);
	}
	if (setupcount > 0)
		subcode = get2(setupdata);
	else
		subcode = -1; /* invalid */

	/* The parameters are offset from the SMB header. */
	params = data + paramoffset;

	if (flags & F_DTAIL && !(smb->flags & SERVER_RESPONSE)) {
		/* This is a CALL. */
		/* print the word parameters */
		show_printf("TotalParamBytes = %d", get2(vwv));
		show_printf("TotalDataBytes = %d", get2(vwv+2));
		show_printf("MaxParamBytes = %d", get2(vwv+4));
		show_printf("MaxDataBytes = %d", get2(vwv+6));
		show_printf("MaxSetupWords = %d", vwv[8]);
		show_printf("TransFlags = 0x%.4x", get2(vwv+10));
		show_printf("Timeout = 0x%.8x", get4(vwv+12));
		/* skip Reserved2 */
		show_printf("ParamBytes = 0x%.4x", parambytes);
		show_printf("ParamOffset = 0x%.4x", paramoffset);
		show_printf("DataBytes = 0x%.4x", get2(vwv+22));
		show_printf("DataOffset = 0x%.4x", get2(vwv+24));
		show_printf("SetupWords = %d", setupcount);

		/* That finishes the VWV, now the misc. stuff. */
		show_printf("FunctionCode = %d", subcode);
	}

	if (!(smb->flags & SERVER_RESPONSE)) {
		/* This is a CALL.  Do sub-function. */
		switch (subcode) {
		case TRANS2_OPEN:
			name = "Open";
			goto name_only;
		case TRANS2_FIND_FIRST:
			output_trans2_findfirst(flags, params, xtra, xsz);
			break;
		case TRANS2_FIND_NEXT2:
			output_trans2_findnext(flags, params, xtra, xsz);
			break;
		case TRANS2_QUERY_FS_INFORMATION:
			name = "QueryFSInfo";
			goto name_only;
		case TRANS2_QUERY_PATH_INFORMATION:
			output_trans2_querypath(flags, params, xtra, xsz);
			break;
		case TRANS2_SET_PATH_INFORMATION:
			name = "SetPathInfo";
			goto name_only;
		case TRANS2_QUERY_FILE_INFORMATION:
			output_trans2_queryfile(flags, params, xtra, xsz);
			break;
		case TRANS2_SET_FILE_INFORMATION:
			output_trans2_setfile(flags, params, xtra, xsz);
			break;
		case TRANS2_CREATE_DIRECTORY:
			name = "CreateDir";
			goto name_only;

		default:
			name = "Unknown";
			/* fall through */
		name_only:
			if (flags & F_SUM)
				snprintf(xtra, xsz, " %s", name);
			if (flags & F_DTAIL)
				show_printf("FunctionName = %s", name);
			break;
		}
	}

	if (flags & F_DTAIL && smb->flags & SERVER_RESPONSE) {
		/* This is a REPLY. */
		/* print the word parameters */
		show_printf("TotalParamBytes = %d", get2(vwv));
		show_printf("TotalDataBytes = %d",  get2(vwv+2));
		/* skip Reserved */
		show_printf("ParamBytes = 0x%.4x", parambytes);
		show_printf("ParamOffset = 0x%.4x", paramoffset);
		show_printf("ParamDispl. = 0x%.4x", get2(vwv+10));
		show_printf("DataBytes = 0x%.4x", get2(vwv+12));
		show_printf("DataOffset = 0x%.4x", get2(vwv+14));
		show_printf("DataDispl. = 0x%.4x", get2(vwv+16));
		show_printf("SetupWords = %d", setupcount);

		output_bytes(byteparms, bytecount);
	}
}
示例#6
0
/* ARGSUSED */
static void
interpret_sesssetupX(int flags, uchar_t *data, int len, char *xtra, int xsz)
{
	int bytecount;
	int lm_pw_len;
	int ext_security;
	int sec_blob_len;
	int isunicode;
	int nt_pw_len;
	int wordcount;
	int cap;
	char tbuf[256];
	struct smb *smbdata;
	uchar_t *setupdata;
	ushort_t smb_flags2;

	smbdata  = (struct smb *)data;
	smb_flags2 = get2(smbdata->flags2);
	setupdata = (uchar_t *)data + sizeof (struct smb);
	wordcount = *setupdata++;

	isunicode = smb_flags2 & FLAGS2_UNICODE;
	ext_security = smb_flags2 & FLAGS2_EXT_SEC;

	if ((smbdata->flags & SERVER_RESPONSE) == 0) {
		/* request summary */
		if (flags & F_SUM) {
			if (ext_security) {
				/* No decoder for SPNEGO */
				snprintf(xtra, xsz, " (SPNEGO)");
				return;
			}
			if (wordcount != 13)
				return;
			setupdata += 14;
			lm_pw_len = get2(setupdata);
			setupdata += 2;
			nt_pw_len = get2(setupdata);
			setupdata += 6;
			cap = get4(setupdata);
			setupdata += 6 + lm_pw_len + nt_pw_len;

			GET_STRING(tbuf, setupdata, isunicode);
			snprintf(xtra, xsz, " Username=%s", tbuf);
		}

		if ((flags & F_DTAIL) == 0)
			return;

		/* request detail */
		show_printf("WordCount = %d", wordcount);
		if (wordcount < 7)
			return;
		/* words 0 - 6 */
		show_printf("ChainedCommand = 0x%.2x", setupdata[0]);
		setupdata += 2;
		show_printf("NextOffset = 0x%.4x", get2(setupdata));
		setupdata += 2;
		show_printf("MaxBufferSize = %d", get2(setupdata));
		setupdata += 2;
		show_printf("MaxMPXRequests = %d", get2(setupdata));
		setupdata += 2;
		show_printf("VCNumber = %d", get2(setupdata));
		setupdata += 2;
		show_printf("SessionKey = 0x%.8x", get4(setupdata));
		setupdata += 4;

		if (ext_security) {
			if (wordcount != 12)
				return;
			/* word 7 */
			sec_blob_len = get2(setupdata);
			setupdata += 2;
			show_printf("Sec. blob len = %d", sec_blob_len);
			/* words 8, 9 (reserved) */
			setupdata += 4;
		} else {
			if (wordcount != 13)
				return;
			/* word 7 */
			lm_pw_len = get2(setupdata);
			setupdata += 2;
			show_printf("LM_Hash_Len = %d", lm_pw_len);
			/* word 8 */
			nt_pw_len = get2(setupdata);
			setupdata += 2;
			show_printf("NT_Hash_Len = %d", nt_pw_len);
			/* words 9, 10 (reserved) */
			setupdata += 4;
		}

		cap = get4(setupdata);
		show_printf("Capabilities = 0x%.8x", cap);
		setupdata += 4;

		bytecount = get2(setupdata);
		setupdata += 2;
		show_printf("ByteCount = %d", bytecount);

		if (ext_security) {
			/* No decoder for SPNEGO.  Just dump hex. */
			show_printf("Security blob: (SPNEGO)");
			output_bytes(setupdata, sec_blob_len);
			setupdata += sec_blob_len;
		} else {
			/* Dump password hashes */
			if (lm_pw_len > 0) {
				show_printf("LM Hash (%d bytes)", lm_pw_len);
				output_bytes(setupdata, lm_pw_len);
				setupdata += lm_pw_len;
			}
			if (nt_pw_len > 0) {
				show_printf("NT Hash (%d bytes)", nt_pw_len);
				output_bytes(setupdata, nt_pw_len);
				setupdata += nt_pw_len;
			}

			/* User */
			GET_STRING(tbuf, setupdata, isunicode);
			show_printf("AccountName = %s", tbuf);

			/* Domain */
			GET_STRING(tbuf, setupdata, isunicode);
			show_printf("DomainName = %s", tbuf);
		}

		/*
		 * Remainder is the same for etc. sec. or not
		 * Native OS, Native LanMan
		 */
		GET_STRING(tbuf, setupdata, isunicode);
		show_printf("NativeOS = %s", tbuf);

		GET_STRING(tbuf, setupdata, isunicode);
		show_printf("NativeLanman = %s", tbuf);
	} else {
		/* response summary */
		if (flags & F_SUM) {
			if (ext_security) {
				/* No decoder for SPNEGO */
				snprintf(xtra, xsz, " (SPNEGO)");
			}
			return;
		}

		if ((flags & F_DTAIL) == 0)
			return;

		/* response detail */
		show_printf("WordCount = %d", wordcount);
		if (wordcount < 3)
			return;

		show_printf("ChainedCommand = 0x%.2x", setupdata[0]);
		setupdata += 2;
		show_printf("NextOffset = 0x%.4x", get2(setupdata));
		setupdata += 2;
		show_printf("SetupAction = 0x%.4x", get2(setupdata));
		setupdata += 2;

		if (ext_security) {
			if (wordcount != 4)
				return;
			sec_blob_len = get2(setupdata);
			setupdata += 2;
			show_printf("Sec. blob len = %d", sec_blob_len);
		} else {
			if (wordcount != 3)
				return;
		}

		bytecount = get2(setupdata);
		setupdata += 2;
		show_printf("ByteCount = %d", bytecount);

		if (ext_security) {
			/* No decoder for SPNEGO.  Just dump hex. */
			show_line("Security blob: (SPNEGO)");
			output_bytes(setupdata, sec_blob_len);
			setupdata += sec_blob_len;
		}

		/*
		 * Native OS, Native LanMan
		 */
		GET_STRING(tbuf, setupdata, isunicode);
		show_printf("NativeOS = %s", tbuf);

		GET_STRING(tbuf, setupdata, isunicode);
		show_printf("NativeLanman = %s", tbuf);

		if (ext_security == 0) {
			GET_STRING(tbuf, setupdata, isunicode);
			show_printf("DomainName = %s", tbuf);
		}
	}
}
示例#7
0
/* ARGSUSED */
static void
interpret_trans(int flags, uchar_t *data, int len, char *xtra, int xsz)
{
	struct smb *smb;
	uchar_t *vwv; /* word parameters */
	int wordcount;
	uchar_t *byteparms;
	int bytecount;
	int parambytes;
	int paramoffset;
	int setupcount;
	int subcode;
	uchar_t *setupdata;
	uchar_t *params;
	int apinum;
	int isunicode;
	char filename[256];
	const char *apiname;
	const char *subcname;
	ushort_t smb_flags2;

	smb = (struct smb *)data;
	smb_flags2 = get2(smb->flags2);
	vwv = (uchar_t *)data + sizeof (struct smb);
	wordcount = *vwv++;

	/* Is the pathname in unicode? */
	isunicode = smb_flags2 & FLAGS2_UNICODE;

	byteparms = vwv + (2 * wordcount);
	bytecount = get2(byteparms);
	byteparms += 2;

	/*
	 * Print the lengths before we (potentially) bail out
	 * due to lack of data (so the user knows why we did).
	 */
	if (flags & F_DTAIL)
		show_printf("WordCount = %d", wordcount);

	/* Get length and location of params and setup data. */
	if (!(smb->flags & SERVER_RESPONSE)) {
		/* CALL */
		if (wordcount < 14)
			return;
		parambytes  = get2(vwv + (2 *  9));
		paramoffset = get2(vwv + (2 * 10));
		setupcount = *(vwv + (2 * 13));
		setupdata  =   vwv + (2 * 14);
	} else {
		/* REPLY */
		if (wordcount < 10)
			return;
		parambytes  = get2(vwv + (2 * 3));
		paramoffset = get2(vwv + (2 * 4));
		setupcount = *(vwv + (2 *  9));
		setupdata  =   vwv + (2 * 10);
	}

	/* The parameters are offset from the SMB header. */
	params = data + paramoffset;

	if ((smb->flags & SERVER_RESPONSE) == 0) {
		/* This is a CALL. */

		if (setupcount > 0)
			subcode = get2(setupdata);
		else
			subcode = -1; /* invalid */
		subcname = pipeapi_name(subcode);

		if (parambytes > 0)
			apinum = params[0];
		else
			apinum = -1; /* invalid */
		if (0 <= apinum && apinum < apinum_max)
			apiname = apiname_table[apinum];
		else
			apiname = "?";

		if (flags & F_SUM) {
			int tl;
			/* Only get one or the other */
			if (*subcname != '?') {
				tl = snprintf(xtra, xsz,
				    " Func=%s", subcname);
				xtra += tl;
				xsz -= tl;
			}
			if (*apiname != '?')
				snprintf(xtra, xsz,
				    " Func=%s", apiname);
			return;
		}
		if ((flags & F_DTAIL) == 0)
			return;

		/* print the word parameters */
		show_printf("TotalParamBytes = %d", get2(vwv));
		show_printf("TotalDataBytes = %d", get2(vwv+2));
		show_printf("MaxParamBytes = %d", get2(vwv+4));
		show_printf("MaxDataBytes = %d", get2(vwv+6));
		show_printf("MaxSetupWords = %d", vwv[8]);
		show_printf("TransFlags = 0x%.4x", get2(vwv+10));
		show_printf("Timeout = 0x%.8x", get4(vwv+12));
		/* skip Reserved2 */
		show_printf("ParamBytes = %d", parambytes);
		show_printf("ParamOffset = %d", paramoffset);
		show_printf("DataBytes = %d", get2(vwv+22));
		show_printf("DataOffset = %d", get2(vwv+24));
		show_printf("SetupWords = %d", setupcount);
		show_printf("ByteCount = %d", bytecount);

		/* That finishes the VWV, now the misc. stuff. */
		if (setupcount > 0)
			show_printf("NmPipeFunc = 0x%x (%s)",
			    subcode, subcname);
		if (parambytes > 0)
			show_printf("RAP_Func = %d (%s)",
			    apinum, apiname);

		/* Finally, print the byte parameters. */
		GET_STRING(filename, byteparms, isunicode);
		show_printf("FileName = %s", filename);
	} else {
		/* This is a REPLY. */
		if (flags & F_SUM)
			return;
		if ((flags & F_DTAIL) == 0)
			return;
		/* print the word parameters */
		show_printf("TotalParamBytes = %d", get2(vwv));
		show_printf("TotalDataBytes = %d", get2(vwv+2));
		/* skip Reserved */
		show_printf("ParamBytes = 0x%.4x", parambytes);
		show_printf("ParamOffset = 0x%.4x", paramoffset);
		show_printf("ParamDispl. = 0x%.4x", get2(vwv+10));
		show_printf("DataBytes = 0x%.4x", get2(vwv+12));
		show_printf("DataOffset = 0x%.4x", get2(vwv+14));
		show_printf("DataDispl. = 0x%.4x", get2(vwv+16));
		show_printf("SetupWords = %d", setupcount);
		show_printf("ByteCount = %d", bytecount);

		show_printf("ParamVec (%d)", parambytes);
		output_bytes(params, parambytes);
	}
}
示例#8
0
/* ARGSUSED */
static void
interpret_negprot(int flags, uchar_t *data, int len, char *xtra, int xsz)
{
	int i, last, length;
	int bytecount;
	int key_len;
	int wordcount;
	char tbuf[256];
	struct smb *smbdata;
	uchar_t *protodata;
	uchar_t *byte0;
	uint64_t nttime;
	uint32_t caps;
	ushort_t smb_flags2;

	smbdata  = (struct smb *)data;
	smb_flags2 = get2(smbdata->flags2);
	protodata = (uchar_t *)data + sizeof (struct smb);
	wordcount = *protodata++;

	if ((smbdata->flags & SERVER_RESPONSE) == 0) {
		/*
		 * request packet:
		 * short bytecount;
		 * struct { char fmt; char name[]; } dialects
		 */
		bytecount = get2(protodata);
		protodata += 2;
		byte0 = protodata;

		if (flags & F_DTAIL)
			show_printf("ByteCount = %d", bytecount);
		if (bytecount > len)
			bytecount = len;

		/* Walk the list of dialects. */
		i = last = 0;
		tbuf[0] = '\0';
		while (protodata < (byte0 + bytecount - 2)) {
			if (*protodata++ != 2)	/* format code */
				break;
			length = strlcpy(tbuf, (char *)protodata,
			    sizeof (tbuf));
			protodata += (length + 1);
			if (flags & F_DTAIL) {
				show_printf("Dialect[%d] = %s",
				    i, tbuf);
			}
			last = i++;
		}
		if (flags & F_SUM) {
			/*
			 * Just print the last dialect, which is
			 * normally the interesting one.
			 */
			snprintf(xtra, xsz, " Dialect[%d]=%s", last, tbuf);
		}
	} else {
		/* Parse reply */
		if (flags & F_SUM) {
			snprintf(xtra, xsz, " Dialect#=%d", protodata[0]);
		}
		if ((flags & F_DTAIL) == 0)
			return;
		if (wordcount < 13)
			return;
		show_printf("WordCount = %d", wordcount);
		show_printf("Dialect Index = %d", protodata[0]);
		protodata += 2;
		show_printf("Security Mode = 0x%x", protodata[0]);
		protodata++;
		show_printf("MaxMPXRequests = %d", get2(protodata));
		protodata += 2;
		show_printf("MaxVCs = %d", get2(protodata));
		protodata += 2;
		show_printf("MaxBufferSize = %d", get4(protodata));
		protodata += 4;
		show_printf("MaxRawBuffer = %d", get4(protodata));
		protodata += 4;
		show_printf("SessionKey = 0x%.8x", get4(protodata));
		protodata += 4;

		caps = get4(protodata);
		protodata += 4;
		show_printf("Capabilities = 0x%.8x", caps);

		/* Server Time */
		nttime = get8(protodata);
		protodata += 8;
		show_printf("Server Time = %s", format_nttime(nttime));

		show_printf("Server TZ = %d", get2(protodata));
		protodata += 2;

		key_len = *protodata++;
		show_printf("KeyLength = %d", key_len);
		bytecount = get2(protodata);
		protodata += 2;
		show_printf("ByteCount = %d", bytecount);

		if (smb_flags2 & FLAGS2_EXT_SEC) {
			show_printf("Server GUID (16)");
			output_bytes(protodata, 16);
			protodata += 16;
			show_printf("Security Blob (SPNEGO)");
			output_bytes(protodata, bytecount - 16);
		} else {
			show_printf("NTLM Challenge: (%d)", key_len);
			output_bytes(protodata, key_len);
			protodata += key_len;
			/*
			 * Get Unicode from capabilities here,
			 * as flags2 typically doesn't have it.
			 * Also, this one is NOT aligned!
			 */
			tbuf[0] = '\0';
			if (caps & 4) {
				(void) unicode2ascii(tbuf, sizeof (tbuf),
				    protodata, 2 * sizeof (tbuf));
			} else {
				(void) strlcpy(tbuf, (char *)protodata,
				    sizeof (tbuf));
			}
			show_printf("Server Domain = %s", tbuf);
		}
	}
}
示例#9
0
/* Reads a line of text, but no more than MAX_LENGTH bytes, from R and appends
   it to S, omitting the final new-line and the carriage return that
   immediately precedes it, if one is present.  The line is left in its
   original encoding.

   Returns true if anything was successfully read from the file.  (If an empty
   line was read, then nothing is appended to S.)  Returns false if end of file
   was reached or a read error occurred before any text could be read. */
bool
line_reader_read (struct line_reader *r, struct string *s, size_t max_length)
{
  size_t original_length = ds_length (s);
  int unit = r->encoding_info.unit;

  do
    {
      size_t max_out = max_length - (ds_length (s) - original_length);
      size_t max_in = r->length;
      size_t max = MIN (max_in, max_out);
      size_t n;
      char *p;

      if (max_out < unit)
        break;

      switch (r->state)
        {
        case S_UNIBYTE:
          p = memchr (r->head, r->encoding_info.lf[0], max);
          if (p != NULL)
            {
              output_line (r, s, p - r->head);
              return true;
            }
          n = max;
          break;

        case S_MULTIBYTE:
          for (n = 0; n + unit <= max; n += unit)
            if (!memcmp (r->head + n, r->encoding_info.lf, unit))
              {
                output_line (r, s, n);
                return true;
              }
          break;

        case S_AUTO:
          for (n = 0; n < max; n++)
            if (!encoding_guess_is_ascii_text (r->head[n]))
              {
                char *encoding;

                output_bytes (r, s, n);
                fill_buffer (r);
                r->state = S_UNIBYTE;

                encoding = xstrdup (encoding_guess_tail_encoding (
                                      r->auto_encoding, r->head, r->length));
                free (r->encoding);
                r->encoding = encoding;

                free (r->auto_encoding);
                r->auto_encoding = NULL;

                n = 0;
                break;
              }
            else if (r->head[n] == '\n')
              {
                output_line (r, s, n);
                return true;
              }
          break;

        default:
          NOT_REACHED ();
        }

      output_bytes (r, s, n);
    }
  while (r->length >= unit || fill_buffer (r) > 0);

  return ds_length (s) > original_length;
}