summaryrefslogtreecommitdiff
path: root/appl/lib/db.b
blob: 804c5a820b466c6eb60396b81c224563c712116b (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
implement DB;

include "sys.m";
	sys: Sys;

include "dial.m";

include "keyring.m";

include "security.m";

include "db.m";

RES_HEADER_SIZE: con 22;

open(addr, username, password, dbname: string): (ref DB_Handle, list of string)
{
	(fd, err) := connect(addr, "none");
	if(nil == fd)
		return (nil, err :: nil);
	return dbopen(fd, username, password, dbname);
}

connect(addr: string, alg: string): (ref Sys->FD, string)
{
	if (sys == nil)
		sys = load Sys Sys->PATH;

	dial := load Dial Dial->PATH;
	if(dial == nil)
		return (nil, sys->sprint("load %s: %r", Dial->PATH));

	addr = dial->netmkaddr(addr, "net", "6669");	# infdb

	conn := dial->dial(addr, nil);
	if(conn == nil)
		return (nil, sys->sprint("can't dial %s: %r", addr));

	(n, addrparts) := sys->tokenize(addr, "!");
	if(n >= 2)
		addr = hd addrparts + "!" + hd tl addrparts;	# ignore service for key search

	kr := load Keyring Keyring->PATH;

	user := user();
	kd := "/usr/" + user + "/keyring/";
	cert := kd + addr;
	if(sys->stat(cert).t0 < 0)
		cert = kd + "default";

	ai := kr->readauthinfo(cert);

	#
	# let auth->client handle nil ai
	# if(ai == nil){
	#	return (nil, sys->sprint("DB init: certificate for %s not found, use getauthinfo first", addr));
	# }
	#

	au := load Auth Auth->PATH;
	if(au == nil)
		return (nil, sys->sprint("DB init: can't load module Auth %r"));

	err := au->init();
	if(err != nil)
		return (nil, sys->sprint("DB init: can't initialize module Auth: %s", err));

	fd: ref Sys->FD;

	(fd, err) = au->client(alg, ai, conn.dfd);
	if(fd == nil)
		return (nil, sys->sprint("DB init: authentication failed: %s", err));

	return (fd, nil);
}

dbopen(fd: ref Sys->FD, username, password, dbname: string): (ref DB_Handle, list of string)
{
	dbh := ref DB_Handle;
	dbh.datafd = fd;
	dbh.lock = makelock();
	dbh.sqlstream = -1;
	logon := array of byte (username +"/"+ password +"/"+ dbname);
	(mtype, strm, rc, data) := sendReq(dbh, 'I', logon);
	if(mtype == 'h')
		return (nil, (sys->sprint("DB: couldn't initialize %s for %s", dbname, username) :: string data :: nil));
	dbh.sqlconn = int string data;

	(mtype, strm, rc, data) = sendReq(dbh, 'O', array of byte string dbh.sqlconn);
	if(mtype == 'h')
		return (nil, (sys->sprint("DB: couldn't open SQL connection") :: string data :: nil));
	dbh.sqlstream = int string data;
	return (dbh, nil);
}

DB_Handle.SQLOpen(oldh: self ref DB_Handle): (int, ref DB_Handle)
{
	dbh := ref *oldh;
	(mtype, nil, nil, data) := sendReq(dbh, 'O', array of byte string dbh.sqlconn);
	if(mtype == 'h')
		return (-1, nil);
	dbh.sqlstream = int string data;
	return (0, dbh);
}

DB_Handle.SQLClose(dbh: self ref DB_Handle): int
{
	(mtype, nil, nil, nil) := sendReq(dbh, 'K', array[0] of byte);
	if(mtype == 'h')
		return -1;
	dbh.sqlstream = -1;
	return 0;    
}

DB_Handle.SQL(dbh: self ref DB_Handle, command: string): (int, list of string)
{
	(mtype, nil, nil, data) := sendReq(dbh, 'W', array of byte command);
	if(mtype == 'h')
		return (-1, "Probable SQL format error" :: string data :: nil);
	return (0, nil);
}

DB_Handle.columns(dbh: self ref DB_Handle): int
{
	(mtype, nil, nil, data) := sendReq(dbh, 'C', array[0] of byte);
	if(mtype == 'h')
		return 0;
	return int string data;
}

DB_Handle.nextRow(dbh: self ref DB_Handle): int
{
	(mtype, nil, nil, data) := sendReq(dbh, 'N', array[0] of byte);
	if(mtype == 'h')
		return 0;
	return int string data;
}

DB_Handle.read(dbh: self ref DB_Handle, columnI: int): (int, array of byte)
{
	(mtype, nil, nil, data) := sendReq(dbh, 'R', array of byte string columnI);
	if(mtype == 'h')
		return (-1, data);
	return (len data, data);
}

DB_Handle.write(dbh: self ref DB_Handle, paramI: int, val: array of byte)
									: int
{
	outbuf := array[len val + 4] of byte;
	param := array of byte sys->sprint("%3d ", paramI);

	for(i := 0; i < 4; i++)
		outbuf[i] = param[i];
	outbuf[4:] = val;
	(mtype, nil, nil, nil) := sendReq(dbh, 'P', outbuf);
	if(mtype == 'h')
		return -1;
	return len val;
}
 
DB_Handle.columnTitle(handle: self ref DB_Handle, columnI: int): string
{
	(mtype, nil, nil, data) := sendReq(handle, 'T', array of byte string columnI);
	if(mtype == 'h')
		return nil;
	return string data;
}

DB_Handle.errmsg(dbh: self ref DB_Handle): string
{
	(nil, nil, nil, data) := sendReq(dbh, 'H', array[0] of byte);
	return string data;
}

sendReq(dbh: ref DB_Handle, mtype: int, data: array of byte) : (int, int, int, array of byte)
{
	lock(dbh);
	header := sys->sprint("%c1%11d %3d ", mtype, len data, dbh.sqlstream);
	if(sys->write(dbh.datafd, array of byte header, 18) != 18) {
		unlock(dbh);
		return ('h', dbh.sqlstream, 0, array of byte "header write failure");
	}
	if(sys->write(dbh.datafd, data, len data) != len data) {
		unlock(dbh);
		return ('h', dbh.sqlstream, 0, array of byte "data write failure");
	}
	if(sys->write(dbh.datafd, array of byte "\n", 1) != 1) {
		unlock(dbh);
		return ('h', dbh.sqlstream, 0, array of byte "header write failure");
	}
	hbuf := array[RES_HEADER_SIZE+3] of byte;
	if((n := sys->readn(dbh.datafd, hbuf, RES_HEADER_SIZE)) != RES_HEADER_SIZE) {
		unlock(dbh);
		if(n < 0)
			why := sys->aprint("read error: %r");
		else if(n == 0)
			why = sys->aprint("lost connection");
		else
			why = sys->aprint("read error: short read");
		return ('h', dbh.sqlstream, 0, why);
	}
	rheader := string hbuf[0:22];
	rtype := rheader[0];
	#	Probably should check version in header[1]
	datalen := int rheader[2:13];
	rstrm := int rheader[14:17];
	retcode := int rheader[18:21];
    
	databuf := array[datalen] of byte;
	# read in loop until get amount of data we want.  If there is a mismatch
	# here, we may hang with a lock on!

	nbytes: int;

	for(length := 0; length < datalen; length += nbytes) {
		nbytes = sys->read(dbh.datafd, databuf[length:], datalen-length);
		if(nbytes <= 0) {
		    break;
		}
	}
	nbytes = sys->read(dbh.datafd, hbuf, 1);	#  The final \n
	unlock(dbh);
	return (rtype, rstrm, retcode, databuf);
}

makelock(): chan of int
{
	return chan[1] of int;
}

lock(h: ref DB_Handle)
{
	h.lock <-= h.sqlstream;
}

unlock(h: ref DB_Handle)
{
	<-h.lock;
}

user(): string
{
	sys = load Sys Sys->PATH;
	fd := sys->open("/dev/user", sys->OREAD);
	if(fd == nil)
		return "";
	buf := array[Sys->NAMEMAX] of byte;
	n := sys->read(fd, buf, len buf);
	if(n < 0)
		return "";
	return string buf[0:n];	
}