Proposed RPC design for Windows CCAPI clients and server

The proposal is for a single user; the solution is replicated for each user logged onto the PC.

Conventions & clarifications

"Client" and "server" refer to the CCAPI client and server.

The CCAPI client acts as both an RPC client and RPC server and the CCAPI server acts as both an RPC client and RPC server.

The Windows username is referred to below as "<USER>."

The Windows Logon Security Identifier is referred to as "<LSID>."

<UUID> means a thread-specific UUID.

<SST> means server start time, a time_t.

A description of client and server authentication has not been added yet.

Design Requirements

Design

The server and each client create an RPC endpoint. The server's endpoint is CCS_<LSID> and the client's endpoint is CCAPI_<UUID>, where each client geta a UUID.

On Windows, the server's ccs_pipe_t type is a char* and is set to the client UUID.

How is the request handled in the server and the reply sent to the client?

One straightforward way is for the reply to be the returned data in the request RPC call (an [out] parameter). That is, data passed from the RPC server to the RPC client. The request handler calls ccs_server_handle_request. Eventually, the server code calls ccs_os_server_send_reply, which saves the reply somewhere. When the server eventually returns to the request handler, the handler returns the saved reply to the client.

But this doesn't work. If two clients A and B ask for the same lock, A will acquire the lock and B will have to wait. But if the single threaded server waits for B's lock, it will never handle A's unlock message. Therefore the server must return to B's request handler and not send a reply to B. So this method will not work.

Instead, there are listener and worker threads in Windows-specific code.

The client's cci_os_ipc function waits for ccs_reply. The client sends the request, including it's UUID, from which the server can construct the endpoint on which to call ccs_reply.

The server's listener thread listens for RPC requests. The request handler puts each request/reply endpoint in a queue and returns to the client.

The server's worker thread removes items from the queue, calls ccs_server_handle_request. ccs_server_handle_request takes both the request data and the client UUID . Eventually ccs_os_server_send_reply is called, with the reply data and client UUID in the reply_pipe. ccs_os_server_send_reply calls ccs_reply on the client's endpoint, which sends the reply to the client.

Is there any security issue with the client listening for RPC calls from the server?

Connections

If the client wants state to be maintained on the server, the client creates a connection. When the connection is closed, the server cleans up any state associated with the connection.

Any given thread in an application process could want to create a connection. When cci_ipc_thread_init is called, the connection thread-local variables are initialized. New connections are created when cci_os_ipc() (via _cci_ipc_send) is called and no connection was previously established. Basically we lazily establish connections so the client doesn't talk to the server until it has to.

Detecting client exit

The server must be able to detect when clients disappear, so the server can free any resources that had been held for the client.

The Windows RPC API does not appear to provide a notification for an endpoint disappearing. It does provide a way to ask if an endpoint is listening. This is useful for polling, but we want a better performing solution than that.

The client has an isAlive function on its endpoint.

To detect the client disappearing without using polling, the server makes an asynchronous call to the isAlive function on the client's endpoint. The isAlive function never returns. When the client exits for any reason, it's endpoint will be closed and the server's function call will return an error. The asynchronous call on the server means no additional threads are used.

Windows provides a number of notification methods to signal I/O completion. Among them are I/O completion ports and callback functions. I chose callback functions because they appear to consume fewer resources.

RPC Endpoint / Function summary

Windows-specific implementation details

Client CCAPI library initialization:

This code runs when the CCAPI DLL is loaded.

Client initialization:

This code runs when cci_os_ipc_thread_init is called:

Server initialization:

[old]

[new]

Establishing a connection:

Client request:

The server's reply to the client's request is not synchronous.

Detecting client exit

Detecting server exit

 

------
Stop:
Start:

04'>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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
/* 
   Unix SMB/CIFS implementation.
   struct samu access routines
   Copyright (C) Jeremy Allison 		1996-2001
   Copyright (C) Luke Kenneth Casson Leighton 	1996-1998
   Copyright (C) Gerald (Jerry) Carter		2000-2006
   Copyright (C) Andrew Bartlett		2001-2002
   Copyright (C) Stefan (metze) Metzmacher	2002

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "../libcli/auth/libcli_auth.h"
#include "../libcli/security/security.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_PASSDB

/**
 * @todo Redefine this to NULL, but this changes the API because
 *       much of samba assumes that the pdb_get...() funtions 
 *       return strings.  (ie not null-pointers).
 *       See also pdb_fill_default_sam().
 */

#define PDB_NOT_QUITE_NULL ""

/*********************************************************************
 Collection of get...() functions for struct samu.
 ********************************************************************/

uint32_t pdb_get_acct_ctrl(const struct samu *sampass)
{
	return sampass->acct_ctrl;
}

time_t pdb_get_logon_time(const struct samu *sampass)
{
	return sampass->logon_time;
}

time_t pdb_get_logoff_time(const struct samu *sampass)
{
	return sampass->logoff_time;
}

time_t pdb_get_kickoff_time(const struct samu *sampass)
{
	return sampass->kickoff_time;
}

time_t pdb_get_bad_password_time(const struct samu *sampass)
{
	return sampass->bad_password_time;
}

time_t pdb_get_pass_last_set_time(const struct samu *sampass)
{
	return sampass->pass_last_set_time;
}

time_t pdb_get_pass_can_change_time(const struct samu *sampass)
{
	uint32_t allow;

	/* if the last set time is zero, it means the user cannot 
	   change their password, and this time must be zero.   jmcd 
	*/
	if (sampass->pass_last_set_time == 0)
		return (time_t) 0;

	/* if the time is max, and the field has been changed,
	   we're trying to update this real value from the sampass
	   to indicate that the user cannot change their password.  jmcd
	*/
	if (sampass->pass_can_change_time == get_time_t_max() &&
	    IS_SAM_CHANGED(sampass, PDB_CANCHANGETIME))
		return sampass->pass_can_change_time;

	if (!pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &allow))
		allow = 0;

	/* in normal cases, just calculate it from policy */
	return sampass->pass_last_set_time + allow;
}

/* we need this for loading from the backend, so that we don't overwrite
   non-changed max times, otherwise the pass_can_change checking won't work */
time_t pdb_get_pass_can_change_time_noncalc(const struct samu *sampass)
{
	return sampass->pass_can_change_time;
}

time_t pdb_get_pass_must_change_time(const struct samu *sampass)
{
	uint32_t expire;

	if (sampass->pass_last_set_time == 0)
		return (time_t) 0;

	if (sampass->acct_ctrl & ACB_PWNOEXP)
		return get_time_t_max();

	if (!pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire)
	    || expire == (uint32_t)-1 || expire == 0)
		return get_time_t_max();

	return sampass->pass_last_set_time + expire;
}

bool pdb_get_pass_can_change(const struct samu *sampass)
{
	if (sampass->pass_can_change_time == get_time_t_max() &&
	    sampass->pass_last_set_time != 0)
		return False;
	return True;
}

uint16_t pdb_get_logon_divs(const struct samu *sampass)
{
	return sampass->logon_divs;
}

uint32_t pdb_get_hours_len(const struct samu *sampass)
{
	return sampass->hours_len;
}

const uint8 *pdb_get_hours(const struct samu *sampass)
{
	return (sampass->hours);
}

const uint8 *pdb_get_nt_passwd(const struct samu *sampass)
{
	SMB_ASSERT((!sampass->nt_pw.data) 
		   || sampass->nt_pw.length == NT_HASH_LEN);
	return (uint8 *)sampass->nt_pw.data;
}

const uint8 *pdb_get_lanman_passwd(const struct samu *sampass)
{
	SMB_ASSERT((!sampass->lm_pw.data) 
		   || sampass->lm_pw.length == LM_HASH_LEN);
	return (uint8 *)sampass->lm_pw.data;
}

const uint8 *pdb_get_pw_history(const struct samu *sampass, uint32_t *current_hist_len)
{
	SMB_ASSERT((!sampass->nt_pw_his.data) 
	   || ((sampass->nt_pw_his.length % PW_HISTORY_ENTRY_LEN) == 0));
	*current_hist_len = sampass->nt_pw_his.length / PW_HISTORY_ENTRY_LEN;
	return (uint8 *)sampass->nt_pw_his.data;
}

/* Return the plaintext password if known.  Most of the time
   it isn't, so don't assume anything magic about this function.

   Used to pass the plaintext to passdb backends that might 
   want to store more than just the NTLM hashes.
*/
const char *pdb_get_plaintext_passwd(const struct samu *sampass)
{
	return sampass->plaintext_pw;
}

const struct dom_sid *pdb_get_user_sid(const struct samu *sampass)
{
	return &sampass->user_sid;
}

const struct dom_sid *pdb_get_group_sid(struct samu *sampass)
{
	NTSTATUS status;

	/* Return the cached group SID if we have that */
	if (sampass->group_sid) {
		return sampass->group_sid;
	}

	/* No algorithmic mapping, meaning that we have to figure out the
	   primary group SID according to group mapping and the user SID must
	   be a newly allocated one.  We rely on the user's Unix primary gid.
	   We have no choice but to fail if we can't find it. */
	status = get_primary_group_sid(sampass,
					pdb_get_username(sampass),
					&sampass->unix_pw,
					&sampass->group_sid);
	if (!NT_STATUS_IS_OK(status)) {
		return NULL;
	}

	return sampass->group_sid;
}

/**
 * Get flags showing what is initalised in the struct samu
 * @param sampass the struct samu in question
 * @return the flags indicating the members initialised in the struct.
 **/

enum pdb_value_state pdb_get_init_flags(const struct samu *sampass, enum pdb_elements element)
{
	enum pdb_value_state ret = PDB_DEFAULT;

        if (!sampass->change_flags || !sampass->set_flags)
        	return ret;

        if (bitmap_query(sampass->set_flags, element)) {
		DEBUG(11, ("element %d: SET\n", element)); 
        	ret = PDB_SET;
	}

        if (bitmap_query(sampass->change_flags, element)) {
		DEBUG(11, ("element %d: CHANGED\n", element)); 
        	ret = PDB_CHANGED;
	}

	if (ret == PDB_DEFAULT) {
		DEBUG(11, ("element %d: DEFAULT\n", element)); 
	}

        return ret;
}

const char *pdb_get_username(const struct samu *sampass)
{
	return sampass->username;
}

const char *pdb_get_domain(const struct samu *sampass)
{
	return sampass->domain;
}

const char *pdb_get_nt_username(const struct samu *sampass)
{
	return sampass->nt_username;
}

const char *pdb_get_fullname(const struct samu *sampass)
{
	return sampass->full_name;
}

const char *pdb_get_homedir(const struct samu *sampass)
{
	return sampass->home_dir;
}

const char *pdb_get_dir_drive(const struct samu *sampass)
{
	return sampass->dir_drive;
}

const char *pdb_get_logon_script(const struct samu *sampass)
{
	return sampass->logon_script;
}

const char *pdb_get_profile_path(const struct samu *sampass)
{
	return sampass->profile_path;
}

const char *pdb_get_acct_desc(const struct samu *sampass)
{
	return sampass->acct_desc;
}

const char *pdb_get_workstations(const struct samu *sampass)
{
	return sampass->workstations;
}

const char *pdb_get_comment(const struct samu *sampass)
{
	return sampass->comment;
}

const char *pdb_get_munged_dial(const struct samu *sampass)
{
	return sampass->munged_dial;
}

uint16_t pdb_get_bad_password_count(const struct samu *sampass)
{
	return sampass->bad_password_count;
}

uint16_t pdb_get_logon_count(const struct samu *sampass)
{
	return sampass->logon_count;
}

uint32_t pdb_get_unknown_6(const struct samu *sampass)
{
	return sampass->unknown_6;
}

void *pdb_get_backend_private_data(const struct samu *sampass, const struct pdb_methods *my_methods)
{
	if (my_methods == sampass->backend_private_methods) {
		return sampass->backend_private_data;
	} else {
		return NULL;
	}
}

/*********************************************************************
 Collection of set...() functions for struct samu.
 ********************************************************************/

bool pdb_set_acct_ctrl(struct samu *sampass, uint32_t acct_ctrl, enum pdb_value_state flag)
{
	sampass->acct_ctrl = acct_ctrl;
	return pdb_set_init_flags(sampass, PDB_ACCTCTRL, flag);
}

bool pdb_set_logon_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->logon_time = mytime;
	return pdb_set_init_flags(sampass, PDB_LOGONTIME, flag);
}

bool pdb_set_logoff_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->logoff_time = mytime;
	return pdb_set_init_flags(sampass, PDB_LOGOFFTIME, flag);
}

bool pdb_set_kickoff_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->kickoff_time = mytime;
	return pdb_set_init_flags(sampass, PDB_KICKOFFTIME, flag);
}

bool pdb_set_bad_password_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->bad_password_time = mytime;
	return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_TIME, flag);
}

bool pdb_set_pass_can_change_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->pass_can_change_time = mytime;
	return pdb_set_init_flags(sampass, PDB_CANCHANGETIME, flag);
}

bool pdb_set_pass_must_change_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->pass_must_change_time = mytime;
	return pdb_set_init_flags(sampass, PDB_MUSTCHANGETIME, flag);
}

bool pdb_set_pass_last_set_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	sampass->pass_last_set_time = mytime;
	return pdb_set_init_flags(sampass, PDB_PASSLASTSET, flag);
}

bool pdb_set_hours_len(struct samu *sampass, uint32_t len, enum pdb_value_state flag)
{
	sampass->hours_len = len;
	return pdb_set_init_flags(sampass, PDB_HOURSLEN, flag);
}

bool pdb_set_logon_divs(struct samu *sampass, uint16_t hours, enum pdb_value_state flag)
{
	sampass->logon_divs = hours;
	return pdb_set_init_flags(sampass, PDB_LOGONDIVS, flag);
}

/**
 * Set flags showing what is initalised in the struct samu
 * @param sampass the struct samu in question
 * @param flag The *new* flag to be set.  Old flags preserved
 *             this flag is only added.  
 **/

bool pdb_set_init_flags(struct samu *sampass, enum pdb_elements element, enum pdb_value_state value_flag)
{
        if (!sampass->set_flags) {
        	if ((sampass->set_flags = 
        		bitmap_talloc(sampass, 
        				PDB_COUNT))==NULL) {
        		DEBUG(0,("bitmap_talloc failed\n"));
        		return False;
        	}
        }
        if (!sampass->change_flags) {
        	if ((sampass->change_flags = 
        		bitmap_talloc(sampass, 
        				PDB_COUNT))==NULL) {
        		DEBUG(0,("bitmap_talloc failed\n"));
        		return False;
        	}
        }

        switch(value_flag) {
        	case PDB_CHANGED:
        		if (!bitmap_set(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_set(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now CHANGED\n", element)); 
        		break;
        	case PDB_SET:
        		if (!bitmap_clear(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_set(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now SET\n", element)); 
        		break;
        	case PDB_DEFAULT:
        	default:
        		if (!bitmap_clear(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_clear(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now DEFAULT\n", element)); 
        		break;
	}

        return True;
}

bool pdb_set_user_sid(struct samu *sampass, const struct dom_sid *u_sid, enum pdb_value_state flag)
{
	if (!u_sid)
		return False;

	sid_copy(&sampass->user_sid, u_sid);

	DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
		    sid_string_dbg(&sampass->user_sid)));

	return pdb_set_init_flags(sampass, PDB_USERSID, flag);
}

bool pdb_set_user_sid_from_string(struct samu *sampass, fstring u_sid, enum pdb_value_state flag)
{
	struct dom_sid new_sid;

	if (!u_sid)
		return False;

	DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
		   u_sid));

	if (!string_to_sid(&new_sid, u_sid)) { 
		DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
		return False;
	}

	if (!pdb_set_user_sid(sampass, &new_sid, flag)) {
		DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on struct samu!\n", u_sid));
		return False;
	}

	return True;
}

/********************************************************************
 We never fill this in from a passdb backend but rather set is