File: decode_postgresql.c

package info (click to toggle)
dsniff 2.4b1+debian-29
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,020 kB
  • sloc: ansic: 10,803; sh: 152; makefile: 126
file content (62 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * decode_postgresql.c
 *
 * PostgreSQL.
 *
 * Thanks to Eric Jackson <shinobi@monkey.org> for packet traces.
 *
 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
 *
 * $Id: decode_postgresql.c,v 1.6 2001/03/15 08:33:02 dugsong Exp $
 */

#include "config.h"

#include <sys/types.h>

#include <stdio.h>
#include <string.h>

#include "decode.h"

#define STARTUP_PKTLEN	296

int
decode_postgresql(u_char *buf, int len, u_char *obuf, int olen)
{
	u_int32_t plen;
	u_char *p;
	char *db, *user;
	
	if (len < STARTUP_PKTLEN)
		return (0);
	
	obuf[0] = '\0';
	db = user = NULL;
	
	for (;;) {
		if (len < 4) break;
		plen = pntohl(buf);
		
		if (plen > len)	break;
		p = buf + 4;
		
		if (plen == STARTUP_PKTLEN) {
			if (pntohl(p) >> 16 == 2) {
				db = p + 4; db[63] = '\0';
				user = db + 64; user[31] = '\0';
			}
		}
		else if (db != NULL && user != NULL) {
			buf[plen - 1] = '\0';
			snprintf(obuf + strlen(obuf),
				 olen - strlen(obuf),
				 "%s\n%s\n%s\n", db, user, p);
			db = user = NULL;
		}
		buf += plen;
		len -= plen;
	}
	return (strlen(obuf));
}