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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/*
 * Sai server definitions src/server/private.h
 *
 * Copyright (C) 2019 - 2025 Andy Green <andy@warmcat.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation:
 *  version 2.1 of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */

#include "../common/include/private.h"
#include <sqlite3.h>
#include <sys/stat.h>

#define SAIW_API_VERSION 4

/*
 * How many builder shells one browser connection may have open and so have
 * tracked for ptydata delivery.  Further openshell forwards still work (it
 * is the server that opens the shell), but ptydata for shells past this
 * many is not delivered back to this connection.
 */
#define SAIW_MAX_SHELLS 8

struct sai_plat;

typedef struct sai_platm {
	struct lws_dll2_owner builder_owner;
	struct lws_dll2_owner subs_owner;

	sqlite3 *pdb;
	sqlite3 *pdb_auth;
} sais_t;

typedef struct sai_platform {
	struct lws_dll2		list;

	const char		*name;
	const char		*build;

	uint8_t			nondefault;

	/* build and name over-allocated here */
} sai_platform_t;

typedef struct sai_builder {
	sais_t c;
} saib_t;

struct vhd;

enum {
	SAIM_NOT_SPECIFIC,
	SAIM_SPECIFIC_H,
	SAIM_SPECIFIC_ID,
	SAIM_SPECIFIC_TASK,
};

/* where a feed http transaction is up to, see w-rss.c */
enum {
	SAIW_RSS_IDLE,
	SAIW_RSS_PARKED,	/* held for a long poll, sending keepalives */
	SAIW_RSS_FINISHING,	/* the whole response is in rss_tx */
	SAIW_RSS_FAILED,	/* drop the connection */
};

typedef enum {
	SAI_AUTH_STATE_NOT_LOGGED_IN,
	SAI_AUTH_STATE_LOGGED_IN_NO_GRANT,
	SAI_AUTH_STATE_LOGGED_IN_GRANT_USER,   /* < :2 */
	SAI_AUTH_STATE_LOGGED_IN_GRANT_ADMIN   /* >= :2 */
} sai_auth_state_t;


struct pss {	struct vhd		*vhd;
	struct lws		*wsi;
	uint8_t			is_gitohashi:1;

	struct lws_spa		*spa;
	struct lejp_ctx		ctx;
	/*
	 * Outgoing ws tx backlog for this browser connection.  Uses the
	 * lws_buflist2 API so we can raise the per-owner sanity limit above
	 * the 2MiB lws_buflist default (a scoped sidebar overview can carry
	 * many events' worth of task data).
	 */
	struct lws_buflist2_owner	raw_tx;
	/*
	 * Reassembly of a fragmented browser -> sai-web message (eg, a
	 * taskclone carrying an edited build script), see LWS_CALLBACK_RECEIVE
	 */
	struct lws_buflist		*rx_reasm;
	struct lws_dll2			same; /* owner: vhd.browsers */

	struct lws_dll2		subs_list;

	uint64_t		sub_timestamp;
	/* highest logs.uid shipped to this browser for sub_task_uuid + sub_run */
	uint64_t		sub_uid;
	char			sub_task_uuid[65];
	int			sub_run;
	char			specific_ref[65];
	char			specific_task[65];
	char			specific_project[96];
	char			selected_event_uuid[33];
	/*
	 * One-shot hint set by com.warmcat.sai.eventinfo: when set, the next
	 * saiw_browser_queue_overview() scopes to just this event and emits its
	 * full task list (instead of the summary-only multi-event payload used
	 * for the sidebar list).  Cleared after being consumed.
	 */
	char			event_tasks_uuid[33];

	/*
	 * Runtime project + branch selection coming from the browser's
	 * sidebar (com.warmcat.sai.taskinfo overview request).  Unlike the
	 * specific_* fields above (which are pinned from the connect URL for
	 * the gitohashi /git/<project> mode), these are updated by the browser
	 * at any time and scope the overview / live pushes to its current
	 * selection.
	 */
	char			selected_project[65];
	char			selected_ref[65];

	/*
	 * task_uuids of the builder shells this browser itself opened with
	 * com.warmcat.sai.openshell; shell ptydata from the server is only
	 * queued to connections that own the shell.
	 */
	char			shell_task_uuid[SAIW_MAX_SHELLS][65];
	unsigned int		shell_count;

	sqlite3			*pdb_artifact;
	sqlite3_blob		*blob_artifact;

	/*
	 * Feed (rss.xml / rss.json) http transaction, see w-rss.c: the
	 * rendered response waiting to go out, and for a held long poll
	 * request, its place in vhd->rss_waiters, its keepalive / deadline
	 * timer, and the scope and index it is waiting on
	 */
	struct lws_buflist	*rss_tx;
	lws_dll2_t		rss_list;
	lws_sorted_usec_list_t	sul_rss;
	lws_usec_t		rss_deadline;
	char			rss_project[65];
	char			rss_branch[65];
	char			rss_fetchurl[96];
	char			rss_index[33];
	uint8_t			rss_state; /* SAIW_RSS_* */
	uint8_t			rss_json:1;
	uint8_t			rss_ka:1;

	lws_dll2_owner_t	logs_owner;
	lws_sorted_usec_list_t	sul_logcache;
	lws_sorted_usec_list_t	sul_overview;
	lws_struct_args_t	a;

	union {
		sai_plat_t	*b;
		sai_plat_owner_t *o;
	} u;
	const char		*server_name;

	lws_dll2_owner_t	sched;	/* scheduled messages */

	struct lwsac		*logs_ac;

	int			log_cache_index;
	int			log_cache_size;
	int			specificity;
	int			segment_flags;
	unsigned int		js_api_version;
	unsigned int		overview_offset;

	/* notification hmac information */
	char			notification_sig[128];
	char			alang[128];
	enum lws_genhmac_types	hmac_type;
	char			our_form;

	uint64_t		first_log_timestamp;
	uint64_t		initial_log_timestamp;
	uint64_t		initial_log_uid;
	uint64_t		artifact_offset;
	uint64_t		artifact_length;

	char			*last_bps[4];
	size_t			last_bps_len[4];

	unsigned int		spa_failed:1;
	unsigned int		dry:1;
	unsigned int		frag:1;
	unsigned int		mark_started:1;
	unsigned int		wants_event_updates:1;
	unsigned int		announced:1;
	unsigned int		bulk_binary_data:1;
	unsigned int		toggle_favour_sch:1;
	unsigned int		resolved_task_offset:1;
	unsigned int		tx_shed:1;
	uint8_t			wants_builder_info;
	sai_auth_state_t	auth_state;
};

struct vhd {
	struct lws_context		*context;
	struct lws_vhost		*vhost;

	/* pss lists */
	struct lws_dll2_owner		browsers;

	struct lws_dll2_owner		builders_owner;
	struct lwsac			*builders;

	struct lws_dll2_owner		pcons_owner;
	struct lwsac			*pcons;

	lws_dll2_owner_t		subs_owner;
	sqlite3				*pdb;
	
	lws_dll2_owner_t		watcher_services;
	
	lws_dll2_owner_t		pcon_watts_owner;
	unsigned int			power_history[150];
	int				power_history_count;
	unsigned int			max_total_power_w;

	struct lws_ss_handle		*h_ss_websrv; /* client */

	const char			*sqlite3_path_lhs;
	const char			*sockpath; /* sai-server control link uds */

	lws_dll2_owner_t		sqlite3_cache; /* sais_sqlite_cache_t */
	lws_dll2_owner_t		rss_waiters; /* pss held on long poll */
	lws_dll2_owner_t		tasklog_cache;

	/*
	 * The projects this vhost shows, from the "projects" pvo; if empty,
	 * it shows all of them.  See w-visible.c
	 */
	lws_dll2_owner_t		visible_projects;
	struct lwsac			*ac_visible;
};

/* nonzero if a "projects" pvo limits the projects this vhost shows */
#define saiw_restricted(_vhd) (!!(_vhd)->visible_projects.count)

typedef struct saiw_websrv {
	struct lws_ss_handle	*ss;
	void			*opaque_data;

	lws_struct_args_t	a;
	struct lejp_ctx		ctx;
	struct lws_buflist	*wbltx;

	/*
	 * ptydata rx reassembly (see w-ws-server.c): fragments are buffered
	 * until the message completes, because only the parsed members say
	 * which browser owns the shell, and shell output must not be sent to
	 * anyone else meanwhile.  pty_dropped marks a message whose
	 * reassembly was abandoned (oversize / oom): nothing is forwarded
	 * for it.
	 */
	uint8_t			*pty_accum; /* content at + LWS_PRE */
	size_t			pty_accum_len;
	unsigned int		pty_dropped:1;
} saiw_websrv_t;


extern const struct lws_protocols protocol_ws;
extern const lws_ss_info_t ssi_saiw_websrv;

int
sai_notification_file_upload_cb(void *data, const char *name,
				const char *filename, char *buf, int len,
				enum lws_spa_fileupload_states state);

int
sai_sq3_event_lookup(sqlite3 *pdb, uint64_t start, lws_struct_args_cb cb, void *ca);

int
sai_sql3_get_uint64_cb(void *user, int cols, char **values, char **name);

int
saiw_ws_json_tx_browser(struct vhd *vhd, struct pss *pss, uint8_t *buf, size_t bl);

int
lws_struct_map_set(const lws_struct_map_t *map, char *u);

int
saiw_ws_json_rx_browser(struct vhd *vhd, struct pss *pss,
			     uint8_t *buf, size_t bl, unsigned int ss_flags);

void
sai_task_uuid_to_event_uuid(char *event_uuid33, const char *task_uuid65);

int
sais_ws_json_tx_builder(struct vhd *vhd, struct pss *pss, uint8_t *buf, size_t bl);

int
saiw_subs_request_writeable(struct vhd *vhd, const char *task_uuid);

int
saiw_event_state_change(struct vhd *vhd, const char *event_uuid);

int
saiw_subs_task_state_change(struct vhd *vhd, const char *task_uuid);

void
saiw_central_cb(lws_sorted_usec_list_t *sul);

int
saiw_get_blob(struct vhd *vhd, const char *url, sqlite3 **pdb,
	      sqlite3_blob **blob, uint64_t *length);

int
saiw_browsers_task_state_change(struct vhd *vhd, const char *task_uuid);


void
saiw_ws_broadcast_browsers_REQUIRES_LWS_PRE(struct vhd *vhd, const void *buf, size_t len,
		      enum lws_write_protocol flags);

int
saiw_ws_browser_queue_REQUIRES_LWS_PRE(struct pss *pss, const void *buf,
				       size_t len, enum lws_write_protocol flags);

void
saiw_browser_state_changed(struct pss *pss, int established);

void
saiw_pss_shell_open(struct pss *pss, const char *task_uuid);

void
saiw_pss_shell_close(struct pss *pss, const char *task_uuid);

int
saiw_pss_owns_shell(struct pss *pss, const char *task_uuid);

void
saiw_update_viewer_count(struct vhd *vhd);

int
saiw_broadcast_logs_batch(struct vhd *vhd, struct pss *pss);

int
saiw_browser_queue_overview(struct vhd *vhd, struct pss *pss);

void
saiw_event_summary_string(sqlite3 *pdb_event, const char *event_uuid,
			  char *out, size_t out_len,
			  unsigned int *p_good, unsigned int *p_bad,
			  unsigned int *p_ongoing, unsigned int *p_pending,
			  unsigned int *p_total);

int
saiw_rss_http(struct vhd *vhd, struct pss *pss, struct lws *wsi, int json);

void
saiw_rss_event_change(struct vhd *vhd);

int
saiw_rss_writeable(struct pss *pss, struct lws *wsi);

void
saiw_rss_close(struct pss *pss);

int
saiw_browser_broadcast_queue_builders(struct vhd *vhd, struct pss *pss);
int
saiw_browser_broadcast_queue_pcons(struct vhd *vhd, struct pss *pss);
int
saiw_browser_broadcast_queue_pcon_energy(struct vhd *vhd, struct pss *pss, sai_pcon_energy_report_t *energy);
void
saiw_update_global_power_history(struct vhd *vhd, sai_pcon_energy_report_t *energy);
int
saiw_browser_broadcast_queue_power_history(struct vhd *vhd, struct pss *pss);

extern const lws_struct_map_t lsm_schema_pcon_energy[];

/* w-visible.c */

int
saiw_visible_init(struct vhd *vhd, void *pvo);

void
saiw_visible_destroy(struct vhd *vhd);

int
saiw_project_visible(struct vhd *vhd, const char *project);

/* uuid may be an event's, or one of its tasks' */
int
saiw_event_visible(struct vhd *vhd, const char *uuid);

/*
 * " and ..." restricting a query on vhd->pdb's events table to the visible
 * projects, or "" if they all are
 */
const char *
saiw_visible_sql(struct vhd *vhd);

/* w-findings.c, for admins only */

int
saiw_browser_send_findings(struct vhd *vhd, struct pss *pss);

int
saiw_browser_send_finding(struct vhd *vhd, struct pss *pss,
			  const sai_findingset_t *fs);