split some security related functions in their own files.
[amitay/samba.git] / source3 / rpc_server / srv_lsa.c
index 5e6e101883c35a59700dd651bf0d5c872cc50842..5d6c1551c91b8d5ce685d0384bff9954742eb86d 100644 (file)
@@ -1,12 +1,11 @@
-
 /* 
- *  Unix SMB/Netbios implementation.
- *  Version 1.9.
+ *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
- *  Copyright (C) Paul Ashton                       1997.
- *  Copyright (C) Jeremy Allison                    1998.
+ *  Copyright (C) Paul Ashton                       1997,
+ *  Copyright (C) Jeremy Allison                    2001,
+ *  Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002-2003.
  *
  *  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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+/* This is the interface to the lsa server code. */
 
 #include "includes.h"
-#include "nterr.h"
 
-extern int DEBUGLEVEL;
-extern DOM_SID global_sam_sid;
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
 
 /***************************************************************************
-lsa_reply_open_policy2
+ api_lsa_open_policy2
  ***************************************************************************/
-static void lsa_reply_open_policy2(prs_struct *rdata)
-{
-       int i;
-       LSA_R_OPEN_POL2 r_o;
 
-       ZERO_STRUCT(r_o);
+static BOOL api_lsa_open_policy2(pipes_struct *p)
+{
+       LSA_Q_OPEN_POL2 q_u;
+       LSA_R_OPEN_POL2 r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       /* set up the LSA QUERY INFO response */
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       for (i = 4; i < POL_HND_SIZE; i++)
-       {
-               r_o.pol.data[i] = i;
+       /* grab the server, object attributes and desired access flag...*/
+       if(!lsa_io_q_open_pol2("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_open_policy2: unable to unmarshall LSA_Q_OPEN_POL2.\n"));
+               return False;
        }
-       r_o.status = 0x0;
+
+       r_u.status = _lsa_open_policy2(p, &q_u, &r_u);
 
        /* store the response in the SMB stream */
-       lsa_io_r_open_pol2("", &r_o, rdata, 0);
+       if(!lsa_io_r_open_pol2("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_open_policy2: unable to marshall LSA_R_OPEN_POL2.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-lsa_reply_open_policy
+api_lsa_open_policy
  ***************************************************************************/
-static void lsa_reply_open_policy(prs_struct *rdata)
-{
-       int i;
-       LSA_R_OPEN_POL r_o;
 
-       ZERO_STRUCT(r_o);
+static BOOL api_lsa_open_policy(pipes_struct *p)
+{
+       LSA_Q_OPEN_POL q_u;
+       LSA_R_OPEN_POL r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       /* set up the LSA QUERY INFO response */
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       for (i = 4; i < POL_HND_SIZE; i++)
-       {
-               r_o.pol.data[i] = i;
+       /* grab the server, object attributes and desired access flag...*/
+       if(!lsa_io_q_open_pol("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_open_policy: unable to unmarshall LSA_Q_OPEN_POL.\n"));
+               return False;
        }
-       r_o.status = 0x0;
+
+       r_u.status = _lsa_open_policy(p, &q_u, &r_u);
 
        /* store the response in the SMB stream */
-       lsa_io_r_open_pol("", &r_o, rdata, 0);
+       if(!lsa_io_r_open_pol("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_open_policy: unable to marshall LSA_R_OPEN_POL.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-make_dom_query
+ api_lsa_enum_trust_dom
  ***************************************************************************/
-static void make_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
+
+static BOOL api_lsa_enum_trust_dom(pipes_struct *p)
 {
-       int domlen = strlen(dom_name);
+       LSA_Q_ENUM_TRUST_DOM q_u;
+       LSA_R_ENUM_TRUST_DOM r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       d_q->uni_dom_max_len = domlen * 2;
-       d_q->uni_dom_str_len = domlen * 2;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       d_q->buffer_dom_name = 4; /* domain buffer pointer */
-       d_q->buffer_dom_sid  = 2; /* domain sid pointer */
+       /* grab the enum trust domain context etc. */
+       if(!lsa_io_q_enum_trust_dom("", &q_u, data, 0))
+               return False;
+
+       /* get required trusted domains information */
+       r_u.status = _lsa_enum_trust_dom(p, &q_u, &r_u);
 
-       /* this string is supposed to be character short */
-       make_unistr2(&(d_q->uni_domain_name), dom_name, domlen);
+       /* prepare the response */
+       if(!lsa_io_r_enum_trust_dom("", &r_u, rdata, 0))
+               return False;
 
-       make_dom_sid2(&(d_q->dom_sid), dom_sid);
+       return True;
 }
 
 /***************************************************************************
-lsa_reply_query_info
+ api_lsa_query_info
  ***************************************************************************/
-static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
-                               prs_struct *rdata,
-                               uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
+
+static BOOL api_lsa_query_info(pipes_struct *p)
 {
-       LSA_R_ENUM_TRUST_DOM r_e;
+       LSA_Q_QUERY_INFO q_u;
+       LSA_R_QUERY_INFO r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ZERO_STRUCT(r_e);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
+
+       /* grab the info class and policy handle */
+       if(!lsa_io_q_query("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_query_info: failed to unmarshall LSA_Q_QUERY_INFO.\n"));
+               return False;
+       }
 
-       /* set up the LSA QUERY INFO response */
-       make_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
-             dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
+       r_u.status = _lsa_query_info(p, &q_u, &r_u);
 
        /* store the response in the SMB stream */
-       lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
+       if(!lsa_io_r_query("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_query_info: failed to marshall LSA_R_QUERY_INFO.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-lsa_reply_query_info
+ api_lsa_lookup_sids
  ***************************************************************************/
-static void lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
-                               char *dom_name, DOM_SID *dom_sid)
-{
-       LSA_R_QUERY_INFO r_q;
 
-       ZERO_STRUCT(r_q);
+static BOOL api_lsa_lookup_sids(pipes_struct *p)
+{
+       LSA_Q_LOOKUP_SIDS q_u;
+       LSA_R_LOOKUP_SIDS r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       /* set up the LSA QUERY INFO response */
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       r_q.undoc_buffer = 0x22000000; /* bizarre */
-       r_q.info_class = q_q->info_class;
+       /* grab the info class and policy handle */
+       if(!lsa_io_q_lookup_sids("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_lookup_sids: failed to unmarshall LSA_Q_LOOKUP_SIDS.\n"));
+               return False;
+       }
 
-       make_dom_query(&r_q.dom.id5, dom_name, dom_sid);
+       r_u.status = _lsa_lookup_sids(p, &q_u, &r_u);
 
-       r_q.status = 0x0;
+       if(!lsa_io_r_lookup_sids("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_lookup_sids: Failed to marshall LSA_R_LOOKUP_SIDS.\n"));
+               return False;
+       }
 
-       /* store the response in the SMB stream */
-       lsa_io_r_query("", &r_q, rdata, 0);
+       return True;
 }
 
-
 /***************************************************************************
-make_dom_ref
+ api_lsa_lookup_names
  ***************************************************************************/
-static void make_dom_ref(DOM_R_REF *ref, int num_domains,
-                               char **dom_names, DOM_SID **dom_sids)
-                         
-{
-       int i;
 
-       if (num_domains > MAX_REF_DOMAINS)
-       {
-               num_domains = MAX_REF_DOMAINS;
-       }
+static BOOL api_lsa_lookup_names(pipes_struct *p)
+{
+       LSA_Q_LOOKUP_NAMES q_u;
+       LSA_R_LOOKUP_NAMES r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ref->undoc_buffer = 1;
-       ref->num_ref_doms_1 = num_domains;
-       ref->undoc_buffer2 = 1;
-       ref->max_entries = MAX_REF_DOMAINS;
-       ref->num_ref_doms_2 = num_domains;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       for (i = 0; i < num_domains; i++)
-       {
-               int len = dom_names[i] != NULL ? strlen(dom_names[i]) : 0;
+       /* grab the info class and policy handle */
+       if(!lsa_io_q_lookup_names("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_lookup_names: failed to unmarshall LSA_Q_LOOKUP_NAMES.\n"));
+               return False;
+       }
 
-               make_uni_hdr(&(ref->hdr_ref_dom[i].hdr_dom_name), len, len, len != 0 ? 1 : 0);
-               ref->hdr_ref_dom[i].ptr_dom_sid = dom_sids[i] != NULL ? 1 : 0;
+       r_u.status = _lsa_lookup_names(p, &q_u, &r_u);
 
-               make_unistr2 (&(ref->ref_dom[i].uni_dom_name), dom_names[i], len);
-               make_dom_sid2(&(ref->ref_dom[i].ref_dom     ), dom_sids [i]);
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_lookup_names("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_lookup_names: Failed to marshall LSA_R_LOOKUP_NAMES.\n"));
+               return False;
        }
 
+       return True;
 }
 
 /***************************************************************************
-make_reply_lookup_rids
+ api_lsa_close.
  ***************************************************************************/
-static void make_reply_lookup_rids(LSA_R_LOOKUP_RIDS *r_l,
-                               int num_entries, uint32 dom_rids[MAX_LOOKUP_SIDS],
-                               int num_ref_doms,
-                               char **dom_names, DOM_SID **dom_sids)
+
+static BOOL api_lsa_close(pipes_struct *p)
 {
-       int i;
+       LSA_Q_CLOSE q_u;
+       LSA_R_CLOSE r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       make_dom_ref(&(r_l->dom_ref), num_ref_doms, dom_names, dom_sids);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       r_l->num_entries = num_entries;
-       r_l->undoc_buffer = 1;
-       r_l->num_entries2 = num_entries;
+       if (!lsa_io_q_close("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_close: lsa_io_q_close failed.\n"));
+               return False;
+       }
 
-       SMB_ASSERT_ARRAY(r_l->dom_rid, num_entries);
+       r_u.status = _lsa_close(p, &q_u, &r_u);
 
-       for (i = 0; i < num_entries; i++)
-       {
-               make_dom_rid2(&(r_l->dom_rid[i]), dom_rids[i], 0x01);
+       /* store the response in the SMB stream */
+       if (!lsa_io_r_close("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_close: lsa_io_r_close failed.\n"));
+               return False;
        }
 
-       r_l->num_entries3 = num_entries;
+       return True;
 }
 
 /***************************************************************************
-make_lsa_trans_names
+ api_lsa_open_secret.
  ***************************************************************************/
-static void make_lsa_trans_names(LSA_TRANS_NAME_ENUM *trn,
-                               int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
-                               uint32 *total)
-{
-       uint32 status = 0x0;
-       int i;
-       (*total) = 0;
-
-       SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
-
-       for (i = 0; i < num_entries; i++)
-       {
-               uint32 rid = 0xffffffff;
-               uint8 num_auths = sid[i].sid.num_auths;
-               fstring name;
-               uint32 type;
-               
-               SMB_ASSERT_ARRAY(sid[i].sid.sub_auths, num_auths);
-
-               /* find the rid to look up */
-               if (num_auths != 0)
-               {
-                       rid = sid[i].sid.sub_auths[num_auths-1];
-
-                       status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
-
-                       status = (status != 0x0) ? lookup_user_name (rid, name, &type) : status;
-                       status = (status != 0x0) ? lookup_group_name(rid, name, &type) : status;
-                       status = (status != 0x0) ? lookup_alias_name(rid, name, &type) : status;
-               }
-
-               if (status == 0x0)
-               {
-                       make_lsa_trans_name(&(trn->name    [(*total)]),
-                                           &(trn->uni_name[(*total)]),
-                                           type, name, (*total));
-                       (*total)++;
-               }
-       }
-
-       trn->num_entries = (*total);
-       trn->ptr_trans_names = 1;
-       trn->num_entries2 = (*total);
-}
 
-/***************************************************************************
-make_reply_lookup_sids
- ***************************************************************************/
-static void make_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
-                               DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
-                               uint32 mapped_count, uint32 status)
+static BOOL api_lsa_open_secret(pipes_struct *p)
 {
-       r_l->dom_ref      = ref;
-       r_l->names        = names;
-       r_l->mapped_count = mapped_count;
-       r_l->status       = status;
-}
+       LSA_Q_OPEN_SECRET q_u;
+       LSA_R_OPEN_SECRET r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-/***************************************************************************
-lsa_reply_lookup_sids
- ***************************************************************************/
-static void lsa_reply_lookup_sids(prs_struct *rdata,
-                               int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
-                               int num_ref_doms,
-                               char **dom_names, DOM_SID **dom_sids)
-{
-       LSA_R_LOOKUP_SIDS r_l;
-       DOM_R_REF ref;
-       LSA_TRANS_NAME_ENUM names;
-       uint32 mapped_count = 0;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       ZERO_STRUCT(r_l);
-       ZERO_STRUCT(ref);
-       ZERO_STRUCT(names);
+       if(!lsa_io_q_open_secret("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_open_secret: failed to unmarshall LSA_Q_OPEN_SECRET.\n"));
+               return False;
+       }
 
-       /* set up the LSA Lookup SIDs response */
-       make_dom_ref(&ref, num_ref_doms, dom_names, dom_sids);
-       make_lsa_trans_names(&names, num_entries, sid, &mapped_count);
-       make_reply_lookup_sids(&r_l, &ref, &names, mapped_count, 0x0);
+       r_u.status = _lsa_open_secret(p, &q_u, &r_u);
 
        /* store the response in the SMB stream */
-       lsa_io_r_lookup_sids("", &r_l, rdata, 0);
+       if(!lsa_io_r_open_secret("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_open_secret: Failed to marshall LSA_R_OPEN_SECRET.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-lsa_reply_lookup_rids
+ api_lsa_open_secret.
  ***************************************************************************/
-static void lsa_reply_lookup_rids(prs_struct *rdata,
-                               int num_entries, uint32 dom_rids[MAX_LOOKUP_SIDS],
-                               int num_ref_doms,
-                               char **dom_names, DOM_SID **dom_sids)
+
+static BOOL api_lsa_enum_privs(pipes_struct *p)
 {
-       LSA_R_LOOKUP_RIDS r_l;
+       LSA_Q_ENUM_PRIVS q_u;
+       LSA_R_ENUM_PRIVS r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
+
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       ZERO_STRUCT(r_l);
+       if(!lsa_io_q_enum_privs("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_enum_privs: failed to unmarshall LSA_Q_ENUM_PRIVS.\n"));
+               return False;
+       }
 
-       /* set up the LSA Lookup RIDs response */
-       make_reply_lookup_rids(&r_l, num_entries, dom_rids,
-                               num_ref_doms, dom_names, dom_sids);
-       r_l.status = 0x0;
+       r_u.status = _lsa_enum_privs(p, &q_u, &r_u);
 
        /* store the response in the SMB stream */
-       lsa_io_r_lookup_rids("", &r_l, rdata, 0);
+       if(!lsa_io_r_enum_privs("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_enum_privs: Failed to marshall LSA_R_ENUM_PRIVS.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-api_lsa_open_policy
+ api_lsa_open_secret.
  ***************************************************************************/
-static void api_lsa_open_policy2( uint16 vuid, prs_struct *data,
-                             prs_struct *rdata )
+
+static BOOL api_lsa_priv_get_dispname(pipes_struct *p)
 {
-       LSA_Q_OPEN_POL2 q_o;
+       LSA_Q_PRIV_GET_DISPNAME q_u;
+       LSA_R_PRIV_GET_DISPNAME r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ZERO_STRUCT(q_o);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       /* grab the server, object attributes and desired access flag...*/
-       lsa_io_q_open_pol2("", &q_o, data, 0);
+       if(!lsa_io_q_priv_get_dispname("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_priv_get_dispname: failed to unmarshall LSA_Q_PRIV_GET_DISPNAME.\n"));
+               return False;
+       }
 
-       /* lkclXXXX having decoded it, ignore all fields in the open policy! */
+       r_u.status = _lsa_priv_get_dispname(p, &q_u, &r_u);
 
-       /* return a 20 byte policy handle */
-       lsa_reply_open_policy2(rdata);
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_priv_get_dispname("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_priv_get_dispname: Failed to marshall LSA_R_PRIV_GET_DISPNAME.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-api_lsa_open_policy
+ api_lsa_open_secret.
  ***************************************************************************/
-static void api_lsa_open_policy( uint16 vuid, prs_struct *data,
-                             prs_struct *rdata )
+
+static BOOL api_lsa_enum_accounts(pipes_struct *p)
 {
-       LSA_Q_OPEN_POL q_o;
+       LSA_Q_ENUM_ACCOUNTS q_u;
+       LSA_R_ENUM_ACCOUNTS r_u;
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ZERO_STRUCT(q_o);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       /* grab the server, object attributes and desired access flag...*/
-       lsa_io_q_open_pol("", &q_o, data, 0);
+       if(!lsa_io_q_enum_accounts("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_enum_accounts: failed to unmarshall LSA_Q_ENUM_ACCOUNTS.\n"));
+               return False;
+       }
 
-       /* lkclXXXX having decoded it, ignore all fields in the open policy! */
+       r_u.status = _lsa_enum_accounts(p, &q_u, &r_u);
 
-       /* return a 20 byte policy handle */
-       lsa_reply_open_policy(rdata);
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_enum_accounts("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_enum_accounts: Failed to marshall LSA_R_ENUM_ACCOUNTS.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-api_lsa_enum_trust_dom
+ api_lsa_UNK_GET_CONNUSER
  ***************************************************************************/
-static void api_lsa_enum_trust_dom( uint16 vuid, prs_struct *data,
-                                    prs_struct *rdata )
+
+static BOOL api_lsa_unk_get_connuser(pipes_struct *p)
 {
-       LSA_Q_ENUM_TRUST_DOM q_e;
+       LSA_Q_UNK_GET_CONNUSER q_u;
+       LSA_R_UNK_GET_CONNUSER r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ZERO_STRUCT(q_e);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       /* grab the enum trust domain context etc. */
-       lsa_io_q_enum_trust_dom("", &q_e, data, 0);
+       if(!lsa_io_q_unk_get_connuser("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_unk_get_connuser: failed to unmarshall LSA_Q_UNK_GET_CONNUSER.\n"));
+               return False;
+       }
 
-       /* construct reply.  return status is always 0x0 */
-       lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
+       r_u.status = _lsa_unk_get_connuser(p, &q_u, &r_u);
+
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_unk_get_connuser("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_unk_get_connuser: Failed to marshall LSA_R_UNK_GET_CONNUSER.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
-api_lsa_query_info
+ api_lsa_open_user
  ***************************************************************************/
-static void api_lsa_query_info( uint16 vuid, prs_struct *data,
-                                prs_struct *rdata )
+
+static BOOL api_lsa_open_account(pipes_struct *p)
 {
-       LSA_Q_QUERY_INFO q_i;
-       pstring dom_name;
+       LSA_Q_OPENACCOUNT q_u;
+       LSA_R_OPENACCOUNT r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       ZERO_STRUCT(q_i);
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       /* grab the info class and policy handle */
-       lsa_io_q_query("", &q_i, data, 0);
+       if(!lsa_io_q_open_account("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_open_account: failed to unmarshall LSA_Q_OPENACCOUNT.\n"));
+               return False;
+       }
+
+       r_u.status = _lsa_open_account(p, &q_u, &r_u);
 
-       pstrcpy(dom_name, lp_workgroup());
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_open_account("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_open_account: Failed to marshall LSA_R_OPENACCOUNT.\n"));
+               return False;
+       }
 
-       /* construct reply.  return status is always 0x0 */
-       lsa_reply_query_info(&q_i, rdata, dom_name, &global_sam_sid);
+       return True;
 }
 
 /***************************************************************************
-api_lsa_lookup_sids
+ api_lsa_get_privs
  ***************************************************************************/
-static void api_lsa_lookup_sids( uint16 vuid, prs_struct *data,
-                                 prs_struct *rdata )
+
+static BOOL api_lsa_enum_privsaccount(pipes_struct *p)
 {
-       LSA_Q_LOOKUP_SIDS q_l;
-       pstring dom_name;
-       DOM_SID sid_S_1_1;
-       DOM_SID sid_S_1_3;
-       DOM_SID sid_S_1_5;
+       LSA_Q_ENUMPRIVSACCOUNT q_u;
+       LSA_R_ENUMPRIVSACCOUNT r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       DOM_SID *sid_array[4];
-       char    *dom_names[4];
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       ZERO_STRUCT(q_l);
-       ZERO_STRUCT(sid_S_1_1);
-       ZERO_STRUCT(sid_S_1_3);
-       ZERO_STRUCT(sid_S_1_5);
+       if(!lsa_io_q_enum_privsaccount("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_enum_privsaccount: failed to unmarshall LSA_Q_ENUMPRIVSACCOUNT.\n"));
+               return False;
+       }
 
-       /* grab the info class and policy handle */
-       lsa_io_q_lookup_sids("", &q_l, data, 0);
+       r_u.status = _lsa_enum_privsaccount(p, rdata, &q_u, &r_u);
+
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_enum_privsaccount("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_enum_privsaccount: Failed to marshall LSA_R_ENUMPRIVSACCOUNT.\n"));
+               return False;
+       }
 
-       pstrcpy(dom_name, lp_workgroup());
+       return True;
+}
+
+/***************************************************************************
+ api_lsa_getsystemaccount
+ ***************************************************************************/
 
-       string_to_sid(&sid_S_1_1, "S-1-1");
-        string_to_sid(&sid_S_1_3, "S-1-3");
-        string_to_sid(&sid_S_1_5, "S-1-5");
+static BOOL api_lsa_getsystemaccount(pipes_struct *p)
+{
+       LSA_Q_GETSYSTEMACCOUNT q_u;
+       LSA_R_GETSYSTEMACCOUNT r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       dom_names[0] = dom_name;
-       sid_array[0] = &global_sam_sid;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       dom_names[1] = "Everyone";
-       sid_array[1] = &sid_S_1_1;
+       if(!lsa_io_q_getsystemaccount("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_getsystemaccount: failed to unmarshall LSA_Q_GETSYSTEMACCOUNT.\n"));
+               return False;
+       }
 
-       dom_names[2] = "don't know";
-       sid_array[2] = &sid_S_1_3;
+       r_u.status = _lsa_getsystemaccount(p, &q_u, &r_u);
 
-       dom_names[3] = "NT AUTHORITY";
-       sid_array[3] = &sid_S_1_5;
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_getsystemaccount("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_getsystemaccount: Failed to marshall LSA_R_GETSYSTEMACCOUNT.\n"));
+               return False;
+       }
 
-       /* construct reply.  return status is always 0x0 */
-       lsa_reply_lookup_sids(rdata,
-                              q_l.sids.num_entries, q_l.sids.sid, /* SIDs */
-                              4, dom_names, sid_array);
+       return True;
 }
 
+
 /***************************************************************************
-api_lsa_lookup_names
+ api_lsa_setsystemaccount
  ***************************************************************************/
-static void api_lsa_lookup_names( uint16 vuid, prs_struct *data,
-                                  prs_struct *rdata )
+
+static BOOL api_lsa_setsystemaccount(pipes_struct *p)
 {
-       int i;
-       LSA_Q_LOOKUP_RIDS q_l;
-       pstring dom_name;
-       uint32 dom_rids[MAX_LOOKUP_SIDS];
-       uint32 dummy_g_rid;
+       LSA_Q_SETSYSTEMACCOUNT q_u;
+       LSA_R_SETSYSTEMACCOUNT r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       DOM_SID sid_S_1_1;
-       DOM_SID sid_S_1_3;
-       DOM_SID sid_S_1_5;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       DOM_SID *sid_array[4];
-       char    *dom_names[4];
+       if(!lsa_io_q_setsystemaccount("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_setsystemaccount: failed to unmarshall LSA_Q_SETSYSTEMACCOUNT.\n"));
+               return False;
+       }
 
-       ZERO_STRUCT(q_l);
-       ZERO_STRUCT(sid_S_1_1);
-       ZERO_STRUCT(sid_S_1_3);
-       ZERO_STRUCT(sid_S_1_5);
-       ZERO_ARRAY(dom_rids);   
+       r_u.status = _lsa_setsystemaccount(p, &q_u, &r_u);
 
-       /* grab the info class and policy handle */
-       lsa_io_q_lookup_rids("", &q_l, data, 0);
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_setsystemaccount("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_setsystemaccount: Failed to marshall LSA_R_SETSYSTEMACCOUNT.\n"));
+               return False;
+       }
 
-       pstrcpy(dom_name, lp_workgroup());
+       return True;
+}
 
-       string_to_sid(&sid_S_1_1, "S-1-1");
-        string_to_sid(&sid_S_1_3, "S-1-3");
-        string_to_sid(&sid_S_1_5, "S-1-5");
+/***************************************************************************
+ api_lsa_addprivs
+ ***************************************************************************/
 
-       dom_names[0] = dom_name;
-       sid_array[0] = &global_sam_sid;
+static BOOL api_lsa_addprivs(pipes_struct *p)
+{
+       LSA_Q_ADDPRIVS q_u;
+       LSA_R_ADDPRIVS r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
 
-       dom_names[1] = "Everyone";
-       sid_array[1] = &sid_S_1_1;
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
 
-       dom_names[2] = "don't know";
-       sid_array[2] = &sid_S_1_3;
+       if(!lsa_io_q_addprivs("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_addprivs: failed to unmarshall LSA_Q_ADDPRIVS.\n"));
+               return False;
+       }
 
-       dom_names[3] = "NT AUTHORITY";
-       sid_array[3] = &sid_S_1_5;
+       r_u.status = _lsa_addprivs(p, &q_u, &r_u);
 
-       SMB_ASSERT_ARRAY(q_l.lookup_name, q_l.num_entries);
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_addprivs("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_addprivs: Failed to marshall LSA_R_ADDPRIVS.\n"));
+               return False;
+       }
 
-       /* convert received RIDs to strings, so we can do them. */
-       for (i = 0; i < q_l.num_entries; i++)
-       {
-               fstring user_name;
-               fstrcpy(user_name, unistr2(q_l.lookup_name[i].str.buffer));
+       return True;
+}
 
-               /*
-                * Map to the UNIX username.
-                */
-               map_username(user_name);
+/***************************************************************************
+ api_lsa_removeprivs
+ ***************************************************************************/
 
-               /*
-                * Do any case conversions.
-                */
-               (void)Get_Pwnam(user_name, True);
+static BOOL api_lsa_removeprivs(pipes_struct *p)
+{
+       LSA_Q_REMOVEPRIVS q_u;
+       LSA_R_REMOVEPRIVS r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
+
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
+
+       if(!lsa_io_q_removeprivs("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_removeprivs: failed to unmarshall LSA_Q_REMOVEPRIVS.\n"));
+               return False;
+       }
+
+       r_u.status = _lsa_removeprivs(p, &q_u, &r_u);
 
-               if (!pdb_name_to_rid(user_name, &dom_rids[i], &dummy_g_rid))
-               {
-                       /* WHOOPS!  we should really do something about this... */
-                       dom_rids[i] = 0;
-               }
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_removeprivs("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_removeprivs: Failed to marshall LSA_R_REMOVEPRIVS.\n"));
+               return False;
        }
 
-       /* construct reply.  return status is always 0x0 */
-       lsa_reply_lookup_rids(rdata,
-                              q_l.num_entries, dom_rids, /* text-converted SIDs */
-                              4, dom_names, sid_array);
+       return True;
 }
 
 /***************************************************************************
- api_lsa_close
+ api_lsa_query_secobj
  ***************************************************************************/
-static void api_lsa_close( uint16 vuid, prs_struct *data,
-                                  prs_struct *rdata)
+
+static BOOL api_lsa_query_secobj(pipes_struct *p)
 {
-       /* XXXX this is NOT good */
-       char *q = mem_data(&(rdata->data), rdata->offset);
-
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0); 
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-
-       rdata->offset += 24;
+       LSA_Q_QUERY_SEC_OBJ q_u;
+       LSA_R_QUERY_SEC_OBJ r_u;
+       
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
+
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
+
+       if(!lsa_io_q_query_sec_obj("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_query_secobj: failed to unmarshall LSA_Q_QUERY_SEC_OBJ.\n"));
+               return False;
+       }
+
+       r_u.status = _lsa_query_secobj(p, &q_u, &r_u);
+
+       /* store the response in the SMB stream */
+       if(!lsa_io_r_query_sec_obj("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_query_secobj: Failed to marshall LSA_R_QUERY_SEC_OBJ.\n"));
+               return False;
+       }
+
+       return True;
 }
 
 /***************************************************************************
- api_lsa_open_secret
+ api_lsa_query_dnsdomainfo
  ***************************************************************************/
-static void api_lsa_open_secret( uint16 vuid, prs_struct *data,
-                                  prs_struct *rdata)
+
+static BOOL api_lsa_query_info2(pipes_struct *p)
 {
-       /* XXXX this is NOT good */
-       char *q = mem_data(&(rdata->data), rdata->offset);
-
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0);
-       q += 4;
-       SIVAL(q, 0, 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND);
-       q += 4;
-       
-       rdata->offset += 24;
+       LSA_Q_QUERY_INFO2 q_u;
+       LSA_R_QUERY_INFO2 r_u;
+
+       prs_struct *data = &p->in_data.data;
+       prs_struct *rdata = &p->out_data.rdata;
+
+       ZERO_STRUCT(q_u);
+       ZERO_STRUCT(r_u);
+
+       if(!lsa_io_q_query_info2("", &q_u, data, 0)) {
+               DEBUG(0,("api_lsa_query_info2: failed to unmarshall LSA_Q_QUERY_INFO2.\n"));
+               return False;
+       }
+
+       r_u.status = _lsa_query_info2(p, &q_u, &r_u);
+
+       if (!lsa_io_r_query_info2("", &r_u, rdata, 0)) {
+               DEBUG(0,("api_lsa_query_info2: failed to marshall LSA_R_QUERY_INFO2.\n"));
+               return False;
+       }
+
+       return True;
 }
 
+
 /***************************************************************************
  \PIPE\ntlsa commands
  ***************************************************************************/
 static struct api_struct api_lsa_cmds[] =
 {
-       { "LSA_OPENPOLICY2"     , LSA_OPENPOLICY2     , api_lsa_open_policy2   },
-       { "LSA_OPENPOLICY"      , LSA_OPENPOLICY      , api_lsa_open_policy    },
-       { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info     },
-       { "LSA_ENUMTRUSTDOM"    , LSA_ENUMTRUSTDOM    , api_lsa_enum_trust_dom },
-       { "LSA_CLOSE"           , LSA_CLOSE           , api_lsa_close          },
-       { "LSA_OPENSECRET"      , LSA_OPENSECRET      , api_lsa_open_secret    },
-       { "LSA_LOOKUPSIDS"      , LSA_LOOKUPSIDS      , api_lsa_lookup_sids    },
-       { "LSA_LOOKUPNAMES"     , LSA_LOOKUPNAMES     , api_lsa_lookup_names   },
-       { NULL                  , 0                   , NULL                   }
+       { "LSA_OPENPOLICY2"     , LSA_OPENPOLICY2     , api_lsa_open_policy2     },
+       { "LSA_OPENPOLICY"      , LSA_OPENPOLICY      , api_lsa_open_policy      },
+       { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info       },
+       { "LSA_ENUMTRUSTDOM"    , LSA_ENUMTRUSTDOM    , api_lsa_enum_trust_dom   },
+       { "LSA_CLOSE"           , LSA_CLOSE           , api_lsa_close            },
+       { "LSA_OPENSECRET"      , LSA_OPENSECRET      , api_lsa_open_secret      },
+       { "LSA_LOOKUPSIDS"      , LSA_LOOKUPSIDS      , api_lsa_lookup_sids      },
+       { "LSA_LOOKUPNAMES"     , LSA_LOOKUPNAMES     , api_lsa_lookup_names     },
+       { "LSA_ENUM_PRIVS"      , LSA_ENUM_PRIVS      , api_lsa_enum_privs       },
+       { "LSA_PRIV_GET_DISPNAME",LSA_PRIV_GET_DISPNAME,api_lsa_priv_get_dispname},
+       { "LSA_ENUM_ACCOUNTS"   , LSA_ENUM_ACCOUNTS   , api_lsa_enum_accounts    },
+       { "LSA_UNK_GET_CONNUSER", LSA_UNK_GET_CONNUSER, api_lsa_unk_get_connuser },
+       { "LSA_OPENACCOUNT"     , LSA_OPENACCOUNT     , api_lsa_open_account     },
+       { "LSA_ENUMPRIVSACCOUNT", LSA_ENUMPRIVSACCOUNT, api_lsa_enum_privsaccount},
+       { "LSA_GETSYSTEMACCOUNT", LSA_GETSYSTEMACCOUNT, api_lsa_getsystemaccount },
+       { "LSA_SETSYSTEMACCOUNT", LSA_SETSYSTEMACCOUNT, api_lsa_setsystemaccount },
+       { "LSA_ADDPRIVS"        , LSA_ADDPRIVS        , api_lsa_addprivs         },
+       { "LSA_REMOVEPRIVS"     , LSA_REMOVEPRIVS     , api_lsa_removeprivs      },
+       { "LSA_QUERYSECOBJ"     , LSA_QUERYSECOBJ     , api_lsa_query_secobj     },
+       /* be careful of the adding of new RPC's.  See commentrs below about
+          ADS DC capabilities                                               */
+       { "LSA_QUERYINFO2"      , LSA_QUERYINFO2      , api_lsa_query_info2      }
 };
 
-/***************************************************************************
- api_ntLsarpcTNP
- ***************************************************************************/
-BOOL api_ntlsa_rpc(pipes_struct *p, prs_struct *data)
+static int count_fns(void)
 {
-       return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds, data);
+       int funcs = sizeof(api_lsa_cmds) / sizeof(struct api_struct);
+       
+       /*
+        * NOTE: Certain calls can not be enabled if we aren't an ADS DC.  Make sure
+        * these calls are always last and that you decrement by the amount of calls
+        * to disable.
+        */
+       if (!(SEC_ADS == lp_security() && ROLE_DOMAIN_PDC == lp_server_role())) {
+               funcs -= 1;
+       }
+
+       return funcs;
+}
+void lsa_get_pipe_fns( struct api_struct **fns, int *n_fns )
+{
+       *fns = api_lsa_cmds;
+       *n_fns = count_fns();
+}
+
+
+NTSTATUS rpc_lsa_init(void)
+{
+       int funcs = count_fns();
+
+       return rpc_pipe_register_commands(SMB_RPC_INTERFACE_VERSION, "lsarpc", "lsass", api_lsa_cmds, 
+               funcs);
 }