rpc_parse/parse_misc.c

説明を見る。
00001 /* 
00002  *  Unix SMB/CIFS implementation.
00003  *  RPC Pipe client / server routines
00004  *  Copyright (C) Andrew Tridgell              1992-1997,
00005  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
00006  *  Copyright (C) Paul Ashton                       1997.
00007  *  Copyright (C) Gerald (Jerry) Carter             2005
00008  *  
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *  
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *  
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  */
00023 
00024 #include "includes.h"
00025 
00026 #undef DBGC_CLASS
00027 #define DBGC_CLASS DBGC_RPC_PARSE
00028 
00029 /****************************************************************************
00030  A temporary TALLOC context for things like unistrs, that is valid for
00031  the life of a complete RPC call.
00032 ****************************************************************************/
00033 
00034 static TALLOC_CTX *current_rpc_talloc = NULL;
00035 
00036 static TALLOC_CTX *get_current_rpc_talloc(void)
00037 {
00038     return current_rpc_talloc;
00039 }
00040 
00041 void set_current_rpc_talloc( TALLOC_CTX *ctx)
00042 {
00043         current_rpc_talloc = ctx;
00044 }
00045 
00046 static TALLOC_CTX *main_loop_talloc = NULL;
00047 
00048 /*******************************************************************
00049 free up temporary memory - called from the main loop
00050 ********************************************************************/
00051 
00052 void main_loop_TALLOC_FREE(void)
00053 {
00054     if (!main_loop_talloc)
00055         return;
00056     talloc_destroy(main_loop_talloc);
00057     main_loop_talloc = NULL;
00058 }
00059 
00060 /*******************************************************************
00061  Get a talloc context that is freed in the main loop...
00062 ********************************************************************/
00063 
00064 TALLOC_CTX *main_loop_talloc_get(void)
00065 {
00066     if (!main_loop_talloc) {
00067         main_loop_talloc = talloc_init("main loop talloc (mainly parse_misc)");
00068         if (!main_loop_talloc)
00069             smb_panic("main_loop_talloc: malloc fail\n");
00070     }
00071 
00072     return main_loop_talloc;
00073 }
00074 
00075 /*******************************************************************
00076  Try and get a talloc context. Get the rpc one if possible, else
00077  get the main loop one. The main loop one is more dangerous as it
00078  goes away between packets, the rpc one will stay around for as long
00079  as a current RPC lasts.
00080 ********************************************************************/ 
00081 
00082 TALLOC_CTX *get_talloc_ctx(void)
00083 {
00084         TALLOC_CTX *tc = get_current_rpc_talloc();
00085 
00086         if (tc)
00087                 return tc;
00088         return main_loop_talloc_get();
00089 }
00090 
00091 /*******************************************************************
00092  Reads or writes a UTIME type.
00093 ********************************************************************/
00094 
00095 static BOOL smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
00096 {
00097         if (t == NULL)
00098                 return False;
00099 
00100         prs_debug(ps, depth, desc, "smb_io_utime");
00101         depth++;
00102 
00103         if(!prs_align(ps))
00104                 return False;
00105         
00106         if(!prs_uint32 ("time", ps, depth, &t->time))
00107                 return False;
00108 
00109         return True;
00110 }
00111 
00112 /*******************************************************************
00113  Reads or writes an NTTIME structure.
00114 ********************************************************************/
00115 
00116 BOOL smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
00117 {
00118         uint32 low, high;
00119         if (nttime == NULL)
00120                 return False;
00121 
00122         prs_debug(ps, depth, desc, "smb_io_time");
00123         depth++;
00124 
00125         if(!prs_align(ps))
00126                 return False;
00127         
00128         if (MARSHALLING(ps)) {
00129                 low = *nttime & 0xFFFFFFFF;
00130                 high = *nttime >> 32;
00131         }
00132         
00133         if(!prs_uint32("low ", ps, depth, &low)) /* low part */
00134                 return False;
00135         if(!prs_uint32("high", ps, depth, &high)) /* high part */
00136                 return False;
00137 
00138         if (UNMARSHALLING(ps)) {
00139                 *nttime = (((uint64_t)high << 32) + low);
00140         }
00141 
00142         return True;
00143 }
00144 
00145 /*******************************************************************
00146  Reads or writes an NTTIME structure.
00147 ********************************************************************/
00148 
00149 BOOL smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
00150 {
00151         return smb_io_time( desc, nttime, ps, depth );
00152 }
00153 
00154 /*******************************************************************
00155  Gets an enumeration handle from an ENUM_HND structure.
00156 ********************************************************************/
00157 
00158 uint32 get_enum_hnd(ENUM_HND *enh)
00159 {
00160         return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
00161 }
00162 
00163 /*******************************************************************
00164  Inits an ENUM_HND structure.
00165 ********************************************************************/
00166 
00167 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
00168 {
00169         DEBUG(5,("smb_io_enum_hnd\n"));
00170 
00171         enh->ptr_hnd = (hnd != 0) ? 1 : 0;
00172         enh->handle = hnd;
00173 }
00174 
00175 /*******************************************************************
00176  Reads or writes an ENUM_HND structure.
00177 ********************************************************************/
00178 
00179 BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
00180 {
00181         if (hnd == NULL)
00182                 return False;
00183 
00184         prs_debug(ps, depth, desc, "smb_io_enum_hnd");
00185         depth++;
00186 
00187         if(!prs_align(ps))
00188                 return False;
00189         
00190         if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
00191                 return False;
00192 
00193         if (hnd->ptr_hnd != 0) {
00194                 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
00195                         return False;
00196         }
00197 
00198         return True;
00199 }
00200 
00201 /*******************************************************************
00202  Reads or writes a DOM_SID structure.
00203 ********************************************************************/
00204 
00205 BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
00206 {
00207         int i;
00208 
00209         if (sid == NULL)
00210                 return False;
00211 
00212         prs_debug(ps, depth, desc, "smb_io_dom_sid");
00213         depth++;
00214 
00215         if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
00216                 return False;
00217 
00218         if(!prs_uint8 ("num_auths  ", ps, depth, &sid->num_auths))
00219                 return False;
00220 
00221         for (i = 0; i < 6; i++)
00222         {
00223                 fstring tmp;
00224                 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
00225                 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
00226                         return False;
00227         }
00228 
00229         /* oops! XXXX should really issue a warning here... */
00230         if (sid->num_auths > MAXSUBAUTHS)
00231                 sid->num_auths = MAXSUBAUTHS;
00232 
00233         if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
00234                 return False;
00235 
00236         return True;
00237 }
00238 
00239 /*******************************************************************
00240  Inits a DOM_SID2 structure.
00241 ********************************************************************/
00242 
00243 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
00244 {
00245         sid2->sid = *sid;
00246         sid2->num_auths = sid2->sid.num_auths;
00247 }
00248 
00249 /*******************************************************************
00250  Reads or writes a DOM_SID2 structure.
00251 ********************************************************************/
00252 
00253 BOOL smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
00254 {
00255         uint32 data_p;
00256 
00257         /* caputure the pointer value to stream */
00258 
00259         data_p = *sid2 ? 0xf000baaa : 0;
00260 
00261         if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
00262                 return False;
00263 
00264         /* we're done if there is no data */
00265 
00266         if ( !data_p )
00267                 return True;
00268 
00269         if (UNMARSHALLING(ps)) {
00270                 if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
00271                         return False;
00272         }
00273 
00274         return True;
00275 }
00276 /*******************************************************************
00277  Reads or writes a DOM_SID2 structure.
00278 ********************************************************************/
00279 
00280 BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
00281 {
00282         if (sid == NULL)
00283                 return False;
00284 
00285         prs_debug(ps, depth, desc, "smb_io_dom_sid2");
00286         depth++;
00287 
00288         if(!prs_align(ps))
00289                 return False;
00290         
00291         if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
00292                 return False;
00293 
00294         if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
00295                 return False;
00296 
00297         return True;
00298 }
00299 
00300 /*******************************************************************
00301  Reads or writes a struct GUID
00302 ********************************************************************/
00303 
00304 BOOL smb_io_uuid(const char *desc, struct GUID *uuid, 
00305                  prs_struct *ps, int depth)
00306 {
00307         if (uuid == NULL)
00308                 return False;
00309 
00310         prs_debug(ps, depth, desc, "smb_io_uuid");
00311         depth++;
00312 
00313         if(!prs_uint32 ("data   ", ps, depth, &uuid->time_low))
00314                 return False;
00315         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_mid))
00316                 return False;
00317         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_hi_and_version))
00318                 return False;
00319 
00320         if(!prs_uint8s (False, "data   ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
00321                 return False;
00322         if(!prs_uint8s (False, "data   ", ps, depth, uuid->node, sizeof(uuid->node)))
00323                 return False;
00324 
00325         return True;
00326 }
00327 
00328 /*******************************************************************
00329 creates a STRHDR structure.
00330 ********************************************************************/
00331 
00332 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
00333 {
00334         hdr->str_max_len = max_len;
00335         hdr->str_str_len = len;
00336         hdr->buffer      = buffer;
00337 }
00338 
00339 /*******************************************************************
00340  Reads or writes a STRHDR structure.
00341 ********************************************************************/
00342 
00343 BOOL smb_io_strhdr(const char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
00344 {
00345         if (hdr == NULL)
00346                 return False;
00347 
00348         prs_debug(ps, depth, desc, "smb_io_strhdr");
00349         depth++;
00350 
00351         prs_align(ps);
00352         
00353         if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
00354                 return False;
00355         if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
00356                 return False;
00357         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
00358                 return False;
00359 
00360         return True;
00361 }
00362 
00363 /*******************************************************************
00364  Inits a UNIHDR structure.
00365 ********************************************************************/
00366 
00367 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
00368 {
00369         hdr->uni_str_len = 2 * (str2->uni_str_len);
00370         hdr->uni_max_len = 2 * (str2->uni_max_len);
00371         hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
00372 }
00373 
00374 /*******************************************************************
00375  Reads or writes a UNIHDR structure.
00376 ********************************************************************/
00377 
00378 BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
00379 {
00380         if (hdr == NULL)
00381                 return False;
00382 
00383         prs_debug(ps, depth, desc, "smb_io_unihdr");
00384         depth++;
00385 
00386         if(!prs_align(ps))
00387                 return False;
00388         
00389         if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
00390                 return False;
00391         if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
00392                 return False;
00393         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
00394                 return False;
00395 
00396         return True;
00397 }
00398 
00399 /*******************************************************************
00400  Inits a BUFHDR structure.
00401 ********************************************************************/
00402 
00403 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
00404 {
00405         hdr->buf_max_len = max_len;
00406         hdr->buf_len     = len;
00407 }
00408 
00409 /*******************************************************************
00410  prs_uint16 wrapper. Call this and it sets up a pointer to where the
00411  uint16 should be stored, or gets the size if reading.
00412  ********************************************************************/
00413 
00414 BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
00415 {
00416         (*offset) = prs_offset(ps);
00417         if (ps->io) {
00418 
00419                 /* reading. */
00420 
00421                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
00422                         return False;
00423 
00424         } else {
00425 
00426                 /* writing. */
00427 
00428                 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
00429                         return False;
00430         }
00431 
00432         return True;
00433 }
00434 
00435 /*******************************************************************
00436  smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
00437  Does nothing on reading, as that is already handled by ...._pre()
00438  ********************************************************************/
00439 
00440 BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
00441                                 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
00442 {
00443         if (!ps->io) {
00444                 /* writing: go back and do a retrospective job.  i hate this */
00445 
00446                 uint32 old_offset = prs_offset(ps);
00447 
00448                 init_buf_hdr(hdr, max_len, len);
00449                 if(!prs_set_offset(ps, ptr_hdrbuf))
00450                         return False;
00451                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
00452                         return False;
00453 
00454                 if(!prs_set_offset(ps, old_offset))
00455                         return False;
00456         }
00457 
00458         return True;
00459 }
00460 
00461 /*******************************************************************
00462  Reads or writes a BUFHDR structure.
00463 ********************************************************************/
00464 
00465 BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
00466 {
00467         if (hdr == NULL)
00468                 return False;
00469 
00470         prs_debug(ps, depth, desc, "smb_io_hdrbuf");
00471         depth++;
00472 
00473         if(!prs_align(ps))
00474                 return False;
00475         
00476         if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
00477                 return False;
00478         if(!prs_uint32("buf_len    ", ps, depth, &hdr->buf_len))
00479                 return False;
00480 
00481         return True;
00482 }
00483 
00484 /*******************************************************************
00485  Inits a UNISTR structure.
00486 ********************************************************************/
00487 
00488 void init_unistr(UNISTR *str, const char *buf)
00489 {
00490         size_t len;
00491 
00492         if (buf == NULL) {
00493                 str->buffer = NULL;
00494                 return;
00495         }
00496                 
00497         len = strlen(buf) + 1;
00498 
00499         if (len) {
00500                 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
00501                 if (str->buffer == NULL)
00502                         smb_panic("init_unistr: malloc fail\n");
00503 
00504                 rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
00505         } else {
00506                 str->buffer = NULL;
00507         }
00508 }
00509 
00510 /*******************************************************************
00511 reads or writes a UNISTR structure.
00512 XXXX NOTE: UNISTR structures NEED to be null-terminated.
00513 ********************************************************************/
00514 
00515 BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
00516 {
00517         if (uni == NULL)
00518                 return False;
00519 
00520         prs_debug(ps, depth, desc, "smb_io_unistr");
00521         depth++;
00522 
00523         if(!prs_unistr("unistr", ps, depth, uni))
00524                 return False;
00525 
00526         return True;
00527 }
00528 
00529 /*******************************************************************
00530  Allocate the RPC_DATA_BLOB memory.
00531 ********************************************************************/
00532 
00533 static void create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
00534 {
00535         if (len) {
00536                 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(), len);
00537                 if (str->buffer == NULL)
00538                         smb_panic("create_rpc_blob: talloc fail\n");
00539                 str->buf_len = len;
00540         } else {
00541                 str->buffer = NULL;
00542                 str->buf_len = 0;
00543         }
00544 }
00545 
00546 /*******************************************************************
00547  Inits a RPC_DATA_BLOB structure from a uint32
00548 ********************************************************************/
00549 
00550 void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
00551 {
00552         ZERO_STRUCTP(str);
00553 
00554         /* set up string lengths. */
00555         create_rpc_blob(str, sizeof(uint32));
00556         SIVAL(str->buffer, 0, val);
00557 }
00558 
00559 /*******************************************************************
00560  Inits a RPC_DATA_BLOB structure.
00561 ********************************************************************/
00562 
00563 void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
00564 {
00565         ZERO_STRUCTP(str);
00566 
00567         /* set up string lengths. */
00568         if (len) {
00569                 create_rpc_blob(str, len*2);
00570                 rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
00571         }
00572 }
00573 
00574 /*******************************************************************
00575  Inits a RPC_DATA_BLOB structure from a hex string.
00576 ********************************************************************/
00577 
00578 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
00579 {
00580         ZERO_STRUCTP(str);
00581         if (buf && *buf) {
00582                 create_rpc_blob(str, strlen(buf));
00583                 str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
00584         }
00585 }
00586 
00587 /*******************************************************************
00588  Inits a RPC_DATA_BLOB structure.
00589 ********************************************************************/
00590 
00591 void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
00592 {
00593         ZERO_STRUCTP(str);
00594 
00595         /* max buffer size (allocated size) */
00596         if (buf != NULL && len) {
00597                 create_rpc_blob(str, len);
00598                 memcpy(str->buffer, buf, len);
00599         }
00600         str->buf_len = len;
00601 }
00602 
00603 /*******************************************************************
00604 reads or writes a BUFFER5 structure.
00605 the buf_len member tells you how large the buffer is.
00606 ********************************************************************/
00607 BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
00608 {
00609         prs_debug(ps, depth, desc, "smb_io_buffer5");
00610         depth++;
00611 
00612         if (buf5 == NULL) return False;
00613 
00614         if(!prs_align(ps))
00615                 return False;
00616         if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
00617                 return False;
00618 
00619         if(buf5->buf_len) {
00620                 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
00621                         return False;
00622         }
00623 
00624         return True;
00625 }
00626 
00627 /*******************************************************************
00628  Inits a REGVAL_BUFFER structure.
00629 ********************************************************************/
00630 
00631 void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
00632 {
00633         ZERO_STRUCTP(str);
00634 
00635         /* max buffer size (allocated size) */
00636         str->buf_max_len = len;
00637         str->offset = 0;
00638         str->buf_len = buf != NULL ? len : 0;
00639 
00640         if (buf != NULL) {
00641                 SMB_ASSERT(str->buf_max_len >= str->buf_len);
00642                 str->buffer = (uint16 *)TALLOC_ZERO(get_talloc_ctx(),
00643                                                     str->buf_max_len);
00644                 if (str->buffer == NULL)
00645                         smb_panic("init_regval_buffer: talloc fail\n");
00646                 memcpy(str->buffer, buf, str->buf_len);
00647         }
00648 }
00649 
00650 /*******************************************************************
00651  Reads or writes a REGVAL_BUFFER structure.
00652    the uni_max_len member tells you how large the buffer is.
00653    the uni_str_len member tells you how much of the buffer is really used.
00654 ********************************************************************/
00655 
00656 BOOL smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
00657 {
00658 
00659         prs_debug(ps, depth, desc, "smb_io_regval_buffer");
00660         depth++;
00661 
00662         if(!prs_align(ps))
00663                 return False;
00664                 
00665         if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
00666                 return False;
00667         if(!prs_uint32("offset     ", ps, depth, &buf2->offset))
00668                 return False;
00669         if(!prs_uint32("buf_len    ", ps, depth, &buf2->buf_len))
00670                 return False;
00671 
00672         /* buffer advanced by indicated length of string
00673            NOT by searching for null-termination */
00674 
00675         if(!prs_regval_buffer(True, "buffer     ", ps, depth, buf2))
00676                 return False;
00677 
00678         return True;
00679 }
00680 
00681 /*******************************************************************
00682 creates a UNISTR2 structure: sets up the buffer, too
00683 ********************************************************************/
00684 
00685 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
00686 {
00687         if (buf != NULL) {
00688                 *ptr = 1;
00689                 init_unistr2(str, buf, UNI_STR_TERMINATE);
00690         } else {
00691                 *ptr = 0;
00692                 init_unistr2(str, NULL, UNI_FLAGS_NONE);
00693 
00694         }
00695 }
00696 
00697 /*******************************************************************
00698  Copies a UNISTR2 structure.
00699 ********************************************************************/
00700 
00701 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
00702 {
00703         if (from->buffer == NULL) {
00704                 ZERO_STRUCTP(str);
00705                 return;
00706         }
00707 
00708         SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
00709 
00710         str->uni_max_len = from->uni_max_len;
00711         str->offset      = from->offset;
00712         str->uni_str_len = from->uni_str_len;
00713 
00714         /* the string buffer is allocated to the maximum size
00715            (the the length of the source string) to prevent
00716            reallocation of memory. */
00717         if (str->buffer == NULL) {
00718                 if (str->uni_max_len) {
00719                         str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_max_len);
00720                         if ((str->buffer == NULL)) {
00721                                 smb_panic("copy_unistr2: talloc fail\n");
00722                                 return;
00723                         }
00724                         /* copy the string */
00725                         memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
00726                 } else {
00727                         str->buffer = NULL;
00728                 }
00729         }
00730 }
00731 
00732 /*******************************************************************
00733  Creates a STRING2 structure.
00734 ********************************************************************/
00735 
00736 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
00737 {
00738         /* set up string lengths. */
00739         SMB_ASSERT(max_len >= str_len);
00740 
00741         /* Ensure buf is valid if str_len was set. Coverity check. */
00742         if (str_len && !buf) {
00743                 return;
00744         }
00745 
00746         str->str_max_len = max_len;
00747         str->offset = 0;
00748         str->str_str_len = str_len;
00749 
00750         /* store the string */
00751         if(str_len != 0) {
00752                 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(),
00753                                                    str->str_max_len);
00754                 if (str->buffer == NULL)
00755                         smb_panic("init_string2: malloc fail\n");
00756                 memcpy(str->buffer, buf, str_len);
00757         }
00758 }
00759 
00760 /*******************************************************************
00761  Reads or writes a STRING2 structure.
00762  XXXX NOTE: STRING2 structures need NOT be null-terminated.
00763    the str_str_len member tells you how long the string is;
00764    the str_max_len member tells you how large the buffer is.
00765 ********************************************************************/
00766 
00767 BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
00768 {
00769         if (str2 == NULL)
00770                 return False;
00771 
00772         if (buffer) {
00773 
00774                 prs_debug(ps, depth, desc, "smb_io_string2");
00775                 depth++;
00776 
00777                 if(!prs_align(ps))
00778                         return False;
00779                 
00780                 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
00781                         return False;
00782                 if(!prs_uint32("offset     ", ps, depth, &str2->offset))
00783                         return False;
00784                 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
00785                         return False;
00786 
00787                 /* buffer advanced by indicated length of string
00788                    NOT by searching for null-termination */
00789                 if(!prs_string2(True, "buffer     ", ps, depth, str2))
00790                         return False;
00791 
00792         } else {
00793 
00794                 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
00795                 depth++;
00796                 memset((char *)str2, '\0', sizeof(*str2));
00797 
00798         }
00799 
00800         return True;
00801 }
00802 
00803 /*******************************************************************
00804  Inits a UNISTR2 structure.
00805 ********************************************************************/
00806 
00807 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
00808 {
00809         size_t len = 0;
00810         uint32 num_chars = 0;
00811 
00812         if (buf) {
00813                 /* We always null terminate the copy. */
00814                 len = strlen(buf) + 1;
00815                 if ( flags == UNI_STR_DBLTERMINATE )
00816                         len++;
00817         }
00818 
00819         if (buf == NULL || len == 0) {
00820                 /* no buffer -- nothing to do */
00821                 str->uni_max_len = 0;
00822                 str->offset = 0;
00823                 str->uni_str_len = 0;
00824 
00825                 return;
00826         }
00827         
00828 
00829         str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
00830         if (str->buffer == NULL) {
00831                 smb_panic("init_unistr2: malloc fail\n");
00832                 return;
00833         }
00834 
00835         /* Ensure len is the length in *bytes* */
00836         len *= sizeof(uint16);
00837 
00838         /*
00839          * The UNISTR2 must be initialized !!!
00840          * jfm, 7/7/2001.
00841          */
00842         if (buf) {
00843                 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
00844                 num_chars = strlen_w(str->buffer);
00845                 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
00846                         num_chars++;
00847                 }
00848                 if ( flags == UNI_STR_DBLTERMINATE )
00849                         num_chars += 2;
00850         }
00851 
00852         str->uni_max_len = num_chars;
00853         str->offset = 0;
00854         str->uni_str_len = num_chars;
00855         if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
00856                 str->uni_max_len++;
00857 }
00858 
00859 /*******************************************************************
00860  Inits a UNISTR4 structure.
00861 ********************************************************************/
00862 
00863 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
00864 {
00865         uni4->string = TALLOC_P( get_talloc_ctx(), UNISTR2 );
00866         if (!uni4->string) {
00867                 smb_panic("init_unistr4: talloc fail\n");
00868                 return;
00869         }
00870         init_unistr2( uni4->string, buf, flags );
00871 
00872         uni4->length = 2 * (uni4->string->uni_str_len);
00873         uni4->size   = 2 * (uni4->string->uni_max_len);
00874 }
00875 
00876 void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
00877 {
00878         uni4->string = TALLOC_P( ctx, UNISTR2 );
00879         if (!uni4->string) {
00880                 smb_panic("init_unistr4_w: talloc fail\n");
00881                 return;
00882         }
00883         init_unistr2_w( ctx, uni4->string, buf );
00884 
00885         uni4->length = 2 * (uni4->string->uni_str_len);
00886         uni4->size   = 2 * (uni4->string->uni_max_len);
00887 }
00888 
00889 /** 
00890  *  Inits a UNISTR2 structure.
00891  *  @param  ctx talloc context to allocate string on
00892  *  @param  str pointer to string to create
00893  *  @param  buf UCS2 null-terminated buffer to init from
00894 */
00895 
00896 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
00897 {
00898         uint32 len = buf ? strlen_w(buf) : 0;
00899 
00900         ZERO_STRUCTP(str);
00901 
00902         /* set up string lengths. */
00903         str->uni_max_len = len;
00904         str->offset = 0;
00905         str->uni_str_len = len;
00906 
00907         if (len + 1) {
00908                 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
00909                 if (str->buffer == NULL) {
00910                         smb_panic("init_unistr2_w: talloc fail\n");
00911                         return;
00912                 }
00913         } else {
00914                 str->buffer = NULL;
00915         }
00916         
00917         /*
00918          * don't move this test above ! The UNISTR2 must be initialized !!!
00919          * jfm, 7/7/2001.
00920          */
00921         if (buf==NULL)
00922                 return;
00923         
00924         /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
00925            long as the buffer above is talloc()ed correctly then this
00926            is the correct thing to do */
00927         if (len+1) {
00928                 strncpy_w(str->buffer, buf, len + 1);
00929         }
00930 }
00931 
00932 /*******************************************************************
00933  Inits a UNISTR2 structure from a UNISTR
00934 ********************************************************************/
00935 
00936 void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
00937 {
00938         uint32 i;
00939 
00940         /* the destination UNISTR2 should never be NULL.
00941            if it is it is a programming error */
00942 
00943         /* if the source UNISTR is NULL, then zero out
00944            the destination string and return */
00945         ZERO_STRUCTP (to);
00946         if ((from == NULL) || (from->buffer == NULL))
00947                 return;
00948 
00949         /* get the length; UNISTR must be NULL terminated */
00950         i = 0;
00951         while ((from->buffer)[i]!='\0')
00952                 i++;
00953         i++;    /* one more to catch the terminating NULL */
00954                 /* is this necessary -- jerry?  I need to think */
00955 
00956         /* set up string lengths; uni_max_len is set to i+1
00957            because we need to account for the final NULL termination */
00958         to->uni_max_len = i;
00959         to->offset = 0;
00960         to->uni_str_len = i;
00961 
00962         /* allocate the space and copy the string buffer */
00963         if (i) {
00964                 to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
00965                 if (to->buffer == NULL)
00966                         smb_panic("init_unistr2_from_unistr: malloc fail\n");
00967                 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
00968         } else {
00969                 to->buffer = NULL;
00970         }
00971         return;
00972 }
00973 
00974 /*******************************************************************
00975   Inits a UNISTR2 structure from a DATA_BLOB.
00976   The length of the data_blob must count the bytes of the buffer.
00977   Copies the blob data.
00978 ********************************************************************/
00979 
00980 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob) 
00981 {
00982         /* Allocs the unistring */
00983         init_unistr2(str, NULL, UNI_FLAGS_NONE);
00984         
00985         /* Sets the values */
00986         str->uni_str_len = blob->length / sizeof(uint16);
00987         str->uni_max_len = str->uni_str_len;
00988         str->offset = 0;
00989         if (blob->length) {
00990                 str->buffer = (uint16 *) memdup(blob->data, blob->length);
00991         } else {
00992                 str->buffer = NULL;
00993         }
00994         if ((str->buffer == NULL) && (blob->length > 0)) {
00995                 smb_panic("init_unistr2_from_datablob: malloc fail\n");
00996         }
00997 }
00998 
00999 /*******************************************************************
01000  UNISTR2* are a little different in that the pointer and the UNISTR2
01001  are not necessarily read/written back to back.  So we break it up 
01002  into 2 separate functions.
01003  See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
01004 ********************************************************************/
01005 
01006 BOOL prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
01007 {
01008         uint32 data_p;
01009 
01010         /* caputure the pointer value to stream */
01011 
01012         data_p = *uni2 ? 0xf000baaa : 0;
01013 
01014         if ( !prs_uint32("ptr", ps, depth, &data_p ))
01015                 return False;
01016 
01017         /* we're done if there is no data */
01018 
01019         if ( !data_p )
01020                 return True;
01021 
01022         if (UNMARSHALLING(ps)) {
01023                 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
01024                         return False;
01025         }
01026 
01027         return True;
01028 }
01029 
01030 /*******************************************************************
01031  now read/write the actual UNISTR2.  Memory for the UNISTR2 (but
01032  not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
01033 ********************************************************************/
01034 
01035 BOOL prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
01036 {
01037         /* just return true if there is no pointer to deal with.
01038            the memory must have been previously allocated on unmarshalling
01039            by prs_unistr2_p() */
01040 
01041         if ( !uni2 )
01042                 return True;
01043 
01044         /* just pass off to smb_io_unstr2() passing the uni2 address as 
01045            the pointer (like you would expect) */
01046 
01047         return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
01048 }
01049 
01050 /*******************************************************************
01051  Reads or writes a UNISTR2 structure.
01052  XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
01053    the uni_str_len member tells you how long the string is;
01054    the uni_max_len member tells you how large the buffer is.
01055 ********************************************************************/
01056 
01057 BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
01058 {
01059         if (uni2 == NULL)
01060                 return False;
01061 
01062         if (buffer) {
01063 
01064                 prs_debug(ps, depth, desc, "smb_io_unistr2");
01065                 depth++;
01066 
01067                 if(!prs_align(ps))
01068                         return False;
01069                 
01070                 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
01071                         return False;
01072                 if(!prs_uint32("offset     ", ps, depth, &uni2->offset))
01073                         return False;
01074                 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
01075                         return False;
01076 
01077                 /* buffer advanced by indicated length of string
01078                    NOT by searching for null-termination */
01079                 if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
01080                         return False;
01081 
01082         } else {
01083 
01084                 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
01085                 depth++;
01086                 memset((char *)uni2, '\0', sizeof(*uni2));
01087 
01088         }
01089 
01090         return True;
01091 }
01092 
01093 /*******************************************************************
01094  now read/write UNISTR4
01095 ********************************************************************/
01096 
01097 BOOL prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
01098 {
01099         void *ptr;
01100         prs_debug(ps, depth, desc, "prs_unistr4");
01101         depth++;
01102 
01103         if ( !prs_uint16("length", ps, depth, &uni4->length ))
01104                 return False;
01105         if ( !prs_uint16("size", ps, depth, &uni4->size ))
01106                 return False;
01107                 
01108         ptr = uni4->string;
01109 
01110         if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
01111                 return False;
01112 
01113         uni4->string = (UNISTR2 *)ptr;
01114         
01115         return True;
01116 }
01117 
01118 /*******************************************************************
01119  now read/write UNISTR4 header
01120 ********************************************************************/
01121 
01122 BOOL prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
01123 {
01124         prs_debug(ps, depth, desc, "prs_unistr4_hdr");
01125         depth++;
01126 
01127         if ( !prs_uint16("length", ps, depth, &uni4->length) )
01128                 return False;
01129         if ( !prs_uint16("size", ps, depth, &uni4->size) )
01130                 return False;
01131         if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
01132                 return False;
01133                 
01134         return True;
01135 }
01136 
01137 /*******************************************************************
01138  now read/write UNISTR4 string
01139 ********************************************************************/
01140 
01141 BOOL prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
01142 {
01143         prs_debug(ps, depth, desc, "prs_unistr4_str");
01144         depth++;
01145 
01146         if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
01147                 return False;
01148                 
01149         return True;
01150 }
01151 
01152 /*******************************************************************
01153  Reads or writes a UNISTR4_ARRAY structure.
01154 ********************************************************************/
01155 
01156 BOOL prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
01157 {
01158         unsigned int i;
01159 
01160         prs_debug(ps, depth, desc, "prs_unistr4_array");
01161         depth++;
01162 
01163         if(!prs_uint32("count", ps, depth, &array->count))
01164                 return False;
01165 
01166         if (UNMARSHALLING(ps)) {
01167                 if (array->count) {
01168                         if ( !(array->strings = TALLOC_ZERO_ARRAY( get_talloc_ctx(), UNISTR4, array->count)) )
01169                                 return False;
01170                 } else {
01171                         array->strings = NULL;
01172                 }
01173         }
01174         
01175         /* write the headers and then the actual string buffer */
01176         
01177         for ( i=0; i<array->count; i++ ) {
01178                 if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
01179                         return False;
01180         }
01181 
01182         for (i=0;i<array->count;i++) {
01183                 if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) ) 
01184                         return False;
01185         }
01186         
01187         return True;
01188 }
01189 
01190 /********************************************************************
01191   initialise a UNISTR_ARRAY from a char**
01192 ********************************************************************/
01193 
01194 BOOL init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
01195 {
01196         unsigned int i;
01197 
01198         array->count = count;
01199 
01200         /* allocate memory for the array of UNISTR4 objects */
01201 
01202         if (array->count) {
01203                 if ( !(array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR4, count )) )
01204                         return False;
01205         } else {
01206                 array->strings = NULL;
01207         }
01208 
01209         for ( i=0; i<count; i++ ) 
01210                 init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
01211 
01212         return True;
01213 }
01214 
01215 BOOL smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
01216 {
01217         prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
01218         depth++;
01219 
01220         if(!prs_align(ps))
01221                 return False;
01222 
01223         if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
01224                 return False;
01225         if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
01226                 return False;
01227         if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
01228                 return False;
01229 
01230         return True;
01231 }
01232 
01233 BOOL smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
01234 {
01235         prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
01236         depth++;
01237 
01238         if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
01239                 return False;
01240 
01241         if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
01242                 return False;
01243         if(!prs_uint32("length", ps, depth, &account_lockout->length))
01244                 return False;
01245 
01246         if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
01247                 return False;
01248         if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
01249                 return False;
01250         if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
01251                 return False;
01252         if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
01253                 return False;
01254 #if 0
01255         if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
01256                 return False;
01257 #endif
01258 
01259         return True;
01260 }
01261 
01262 /*******************************************************************
01263  Inits a DOM_RID structure.
01264 ********************************************************************/
01265 
01266 void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
01267 {
01268         prid->type    = type;
01269         prid->rid     = rid;
01270         prid->rid_idx = idx;
01271 }
01272 
01273 /*******************************************************************
01274  Reads or writes a DOM_RID structure.
01275 ********************************************************************/
01276 
01277 BOOL smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
01278 {
01279         if (rid == NULL)
01280                 return False;
01281 
01282         prs_debug(ps, depth, desc, "smb_io_dom_rid");
01283         depth++;
01284 
01285         if(!prs_align(ps))
01286                 return False;
01287    
01288         if(!prs_uint16("type   ", ps, depth, &rid->type))
01289                 return False;
01290         if(!prs_align(ps))
01291                 return False;
01292         if(!prs_uint32("rid    ", ps, depth, &rid->rid))
01293                 return False;
01294         if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
01295                 return False;
01296 
01297         return True;
01298 }
01299 
01300 /*******************************************************************
01301  Reads or writes a DOM_RID2 structure.
01302 ********************************************************************/
01303 
01304 BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
01305 {
01306         if (rid == NULL)
01307                 return False;
01308 
01309         prs_debug(ps, depth, desc, "smb_io_dom_rid2");
01310         depth++;
01311 
01312         if(!prs_align(ps))
01313                 return False;
01314    
01315         if(!prs_uint16("type   ", ps, depth, &rid->type))
01316                 return False;
01317         if(!prs_align(ps))
01318                 return False;
01319         if(!prs_uint32("rid    ", ps, depth, &rid->rid))
01320                 return False;
01321         if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
01322                 return False;
01323         if(!prs_uint32("unknown", ps, depth, &rid->unknown))
01324                 return False;
01325 
01326         return True;
01327 }
01328 
01329 
01330 /*******************************************************************
01331 creates a DOM_RID3 structure.
01332 ********************************************************************/
01333 
01334 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
01335 {
01336     rid3->rid      = rid;
01337     rid3->type1    = type;
01338     rid3->ptr_type = 0x1; /* non-zero, basically. */
01339     rid3->type2    = 0x1;
01340     rid3->unk      = type;
01341 }
01342 
01343 /*******************************************************************
01344 reads or writes a DOM_RID3 structure.
01345 ********************************************************************/
01346 
01347 BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
01348 {
01349         if (rid3 == NULL)
01350                 return False;
01351 
01352         prs_debug(ps, depth, desc, "smb_io_dom_rid3");
01353         depth++;
01354 
01355         if(!prs_align(ps))
01356                 return False;
01357 
01358         if(!prs_uint32("rid     ", ps, depth, &rid3->rid))
01359                 return False;
01360         if(!prs_uint32("type1   ", ps, depth, &rid3->type1))
01361                 return False;
01362         if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
01363                 return False;
01364         if(!prs_uint32("type2   ", ps, depth, &rid3->type2))
01365                 return False;
01366         if(!prs_uint32("unk     ", ps, depth, &rid3->unk))
01367                 return False;
01368 
01369         return True;
01370 }
01371 
01372 /*******************************************************************
01373  Inits a DOM_RID4 structure.
01374 ********************************************************************/
01375 
01376 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
01377 {
01378     rid4->unknown = unknown;
01379     rid4->attr    = attr;
01380     rid4->rid     = rid;
01381 }
01382 
01383 /*******************************************************************
01384  Inits a DOM_CLNT_SRV structure.
01385 ********************************************************************/
01386 
01387 static void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv, const char *comp_name)
01388 {
01389         DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
01390 
01391         if (logon_srv != NULL) {
01392                 logcln->undoc_buffer = 1;
01393                 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
01394         } else {
01395                 logcln->undoc_buffer = 0;
01396         }
01397 
01398         if (comp_name != NULL) {
01399                 logcln->undoc_buffer2 = 1;
01400                 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
01401         } else {
01402                 logcln->undoc_buffer2 = 0;
01403         }
01404 }
01405 
01406 /*******************************************************************
01407  Inits or writes a DOM_CLNT_SRV structure.
01408 ********************************************************************/
01409 
01410 BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
01411 {
01412         if (logcln == NULL)
01413                 return False;
01414 
01415         prs_debug(ps, depth, desc, "smb_io_clnt_srv");
01416         depth++;
01417 
01418         if(!prs_align(ps))
01419                 return False;
01420         
01421         if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
01422                 return False;
01423 
01424         if (logcln->undoc_buffer != 0) {
01425                 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
01426                         return False;
01427         }
01428 
01429         if(!prs_align(ps))
01430                 return False;
01431 
01432         if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
01433                 return False;
01434 
01435         if (logcln->undoc_buffer2 != 0) {
01436                 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
01437                         return False;
01438         }
01439 
01440         return True;
01441 }
01442 
01443 /*******************************************************************
01444  Inits a DOM_LOG_INFO structure.
01445 ********************************************************************/
01446 
01447 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
01448                 uint16 sec_chan, const char *comp_name)
01449 {
01450         DEBUG(5,("make_log_info %d\n", __LINE__));
01451 
01452         loginfo->undoc_buffer = 1;
01453 
01454         init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
01455         init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
01456 
01457         loginfo->sec_chan = sec_chan;
01458 
01459         init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
01460 }
01461 
01462 /*******************************************************************
01463  Reads or writes a DOM_LOG_INFO structure.
01464 ********************************************************************/
01465 
01466 BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
01467 {
01468         if (loginfo == NULL)
01469                 return False;
01470 
01471         prs_debug(ps, depth, desc, "smb_io_log_info");
01472         depth++;
01473 
01474         if(!prs_align(ps))
01475                 return False;
01476         
01477         if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
01478                 return False;
01479 
01480         if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
01481                 return False;
01482         if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
01483                 return False;
01484 
01485         if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
01486                 return False;
01487 
01488         if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
01489                 return False;
01490 
01491         return True;
01492 }
01493 
01494 /*******************************************************************
01495  Reads or writes a DOM_CHAL structure.
01496 ********************************************************************/
01497 
01498 BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
01499 {
01500         if (chal == NULL)
01501                 return False;
01502 
01503         prs_debug(ps, depth, desc, "smb_io_chal");
01504         depth++;
01505         
01506         if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
01507                 return False;
01508 
01509         return True;
01510 }
01511 
01512 /*******************************************************************
01513  Reads or writes a DOM_CRED structure.
01514 ********************************************************************/
01515 
01516 BOOL smb_io_cred(const char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
01517 {
01518         if (cred == NULL)
01519                 return False;
01520 
01521         prs_debug(ps, depth, desc, "smb_io_cred");
01522         depth++;
01523 
01524         if(!prs_align(ps))
01525                 return False;
01526 
01527         if(!smb_io_chal ("", &cred->challenge, ps, depth))
01528                 return False;
01529 
01530         if(!smb_io_utime("", &cred->timestamp, ps, depth))
01531                 return False;
01532 
01533         return True;
01534 }
01535 
01536 /*******************************************************************
01537  Inits a DOM_CLNT_INFO2 structure.
01538 ********************************************************************/
01539 
01540 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
01541                                 const char *logon_srv, const char *comp_name,
01542                                 const DOM_CRED *clnt_cred)
01543 {
01544         DEBUG(5,("make_clnt_info: %d\n", __LINE__));
01545 
01546         init_clnt_srv(&clnt->login, logon_srv, comp_name);
01547 
01548         if (clnt_cred != NULL) {
01549                 clnt->ptr_cred = 1;
01550                 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
01551         } else {
01552                 clnt->ptr_cred = 0;
01553         }
01554 }
01555 
01556 /*******************************************************************
01557  Reads or writes a DOM_CLNT_INFO2 structure.
01558 ********************************************************************/
01559 
01560 BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
01561 {
01562         if (clnt == NULL)
01563                 return False;
01564 
01565         prs_debug(ps, depth, desc, "smb_io_clnt_info2");
01566         depth++;
01567 
01568         if(!prs_align(ps))
01569                 return False;
01570         
01571         if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
01572                 return False;
01573 
01574         if(!prs_align(ps))
01575                 return False;
01576         
01577         if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
01578                 return False;
01579         if(!smb_io_cred("", &clnt->cred, ps, depth))
01580                 return False;
01581 
01582         return True;
01583 }
01584 
01585 /*******************************************************************
01586  Inits a DOM_CLNT_INFO structure.
01587 ********************************************************************/
01588 
01589 void init_clnt_info(DOM_CLNT_INFO *clnt,
01590                 const char *logon_srv, const char *acct_name,
01591                 uint16 sec_chan, const char *comp_name,
01592                 const DOM_CRED *cred)
01593 {
01594         DEBUG(5,("make_clnt_info\n"));
01595 
01596         init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
01597         memcpy(&clnt->cred, cred, sizeof(clnt->cred));
01598 }
01599 
01600 /*******************************************************************
01601  Reads or writes a DOM_CLNT_INFO structure.
01602 ********************************************************************/
01603 
01604 BOOL smb_io_clnt_info(const char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
01605 {
01606         if (clnt == NULL)
01607                 return False;
01608 
01609         prs_debug(ps, depth, desc, "smb_io_clnt_info");
01610         depth++;
01611 
01612         if(!prs_align(ps))
01613                 return False;
01614         
01615         if(!smb_io_log_info("", &clnt->login, ps, depth))
01616                 return False;
01617         if(!smb_io_cred("", &clnt->cred, ps, depth))
01618                 return False;
01619 
01620         return True;
01621 }
01622 
01623 /*******************************************************************
01624  Inits a DOM_LOGON_ID structure.
01625 ********************************************************************/
01626 
01627 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
01628 {
01629         DEBUG(5,("make_logon_id: %d\n", __LINE__));
01630 
01631         logonid->low  = log_id_low;
01632         logonid->high = log_id_high;
01633 }
01634 
01635 /*******************************************************************
01636  Reads or writes a DOM_LOGON_ID structure.
01637 ********************************************************************/
01638 
01639 BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
01640 {
01641         if (logonid == NULL)
01642                 return False;
01643 
01644         prs_debug(ps, depth, desc, "smb_io_logon_id");
01645         depth++;
01646 
01647         if(!prs_align(ps))
01648                 return False;
01649         
01650         if(!prs_uint32("low ", ps, depth, &logonid->low ))
01651                 return False;
01652         if(!prs_uint32("high", ps, depth, &logonid->high))
01653                 return False;
01654 
01655         return True;
01656 }
01657 
01658 /*******************************************************************
01659  Inits an OWF_INFO structure.
01660 ********************************************************************/
01661 
01662 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
01663 {
01664         DEBUG(5,("init_owf_info: %d\n", __LINE__));
01665         
01666         if (data != NULL)
01667                 memcpy(hash->data, data, sizeof(hash->data));
01668         else
01669                 memset((char *)hash->data, '\0', sizeof(hash->data));
01670 }
01671 
01672 /*******************************************************************
01673  Reads or writes an OWF_INFO structure.
01674 ********************************************************************/
01675 
01676 BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
01677 {
01678         if (hash == NULL)
01679                 return False;
01680 
01681         prs_debug(ps, depth, desc, "smb_io_owf_info");
01682         depth++;
01683 
01684         if(!prs_align(ps))
01685                 return False;
01686         
01687         if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
01688                 return False;
01689 
01690         return True;
01691 }
01692 
01693 /*******************************************************************
01694  Reads or writes a DOM_GID structure.
01695 ********************************************************************/
01696 
01697 BOOL smb_io_gid(const char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
01698 {
01699         if (gid == NULL)
01700                 return False;
01701 
01702         prs_debug(ps, depth, desc, "smb_io_gid");
01703         depth++;
01704 
01705         if(!prs_align(ps))
01706                 return False;
01707         
01708         if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
01709                 return False;
01710         if(!prs_uint32("attr ", ps, depth, &gid->attr))
01711                 return False;
01712 
01713         return True;
01714 }
01715 
01716 /*******************************************************************
01717  Reads or writes an POLICY_HND structure.
01718 ********************************************************************/
01719 
01720 BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
01721 {
01722         if (pol == NULL)
01723                 return False;
01724 
01725         prs_debug(ps, depth, desc, "smb_io_pol_hnd");
01726         depth++;
01727 
01728         if(!prs_align(ps))
01729                 return False;
01730 
01731         if(UNMARSHALLING(ps))
01732                 ZERO_STRUCTP(pol);
01733         
01734         if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
01735                 return False;
01736         if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
01737                 return False;
01738 
01739         return True;
01740 }
01741 
01742 /*******************************************************************
01743  Create a UNISTR3.
01744 ********************************************************************/
01745 
01746 void init_unistr3(UNISTR3 *str, const char *buf)
01747 {
01748         if (buf == NULL) {
01749                 str->uni_str_len=0;
01750                 str->str.buffer = NULL;
01751                 return;
01752         }
01753 
01754         str->uni_str_len = strlen(buf) + 1;
01755 
01756         if (str->uni_str_len) {
01757                 str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_str_len);
01758                 if (str->str.buffer == NULL)
01759                         smb_panic("init_unistr3: malloc fail\n");
01760 
01761                 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
01762         } else {
01763                 str->str.buffer = NULL;
01764         }
01765 }
01766 
01767 /*******************************************************************
01768  Reads or writes a UNISTR3 structure.
01769 ********************************************************************/
01770 
01771 BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
01772 {
01773         if (name == NULL)
01774                 return False;
01775 
01776         prs_debug(ps, depth, desc, "smb_io_unistr3");
01777         depth++;
01778 
01779         if(!prs_align(ps))
01780                 return False;
01781         
01782         if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
01783                 return False;
01784                 
01785         /* we're done if there is no string */
01786         
01787         if ( name->uni_str_len == 0 )
01788                 return True;
01789 
01790         /* don't know if len is specified by uni_str_len member... */
01791         /* assume unicode string is unicode-null-terminated, instead */
01792 
01793         if(!prs_unistr3(True, "unistr", name, ps, depth))
01794                 return False;
01795 
01796         return True;
01797 }
01798 
01799 /*******************************************************************
01800  Stream a uint64_struct
01801  ********************************************************************/
01802 BOOL prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
01803 {
01804         if (UNMARSHALLING(ps)) {
01805                 uint32 high, low;
01806 
01807                 if (!prs_uint32(name, ps, depth+1, &low))
01808                         return False;
01809 
01810                 if (!prs_uint32(name, ps, depth+1, &high))
01811                         return False;
01812 
01813                 *data64 = ((uint64_t)high << 32) + low;
01814 
01815                 return True;
01816         } else {
01817                 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
01818                 return prs_uint32(name, ps, depth+1, &low) && 
01819                            prs_uint32(name, ps, depth+1, &high);
01820         }
01821 }
01822 
01823 /*******************************************************************
01824 reads or writes a BUFHDR2 structure.
01825 ********************************************************************/
01826 BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
01827 {
01828         prs_debug(ps, depth, desc, "smb_io_bufhdr2");
01829         depth++;
01830 
01831         prs_align(ps);
01832         prs_uint32("info_level", ps, depth, &(hdr->info_level));
01833         prs_uint32("length    ", ps, depth, &(hdr->length    ));
01834         prs_uint32("buffer    ", ps, depth, &(hdr->buffer    ));
01835 
01836         return True;
01837 }
01838 
01839 /*******************************************************************
01840 reads or writes a BUFHDR4 structure.
01841 ********************************************************************/
01842 BOOL smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
01843 {
01844         prs_debug(ps, depth, desc, "smb_io_bufhdr4");
01845         depth++;
01846 
01847         prs_align(ps);
01848         prs_uint32("size", ps, depth, &hdr->size);
01849         prs_uint32("buffer", ps, depth, &hdr->buffer);
01850 
01851         return True;
01852 }
01853 
01854 /*******************************************************************
01855 reads or writes a RPC_DATA_BLOB structure.
01856 ********************************************************************/
01857 
01858 BOOL smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
01859 {
01860         prs_debug(ps, depth, desc, "smb_io_rpc_blob");
01861         depth++;
01862 
01863         prs_align(ps);
01864         if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
01865                 return False;
01866 
01867         if ( blob->buf_len == 0 )
01868                 return True;
01869 
01870         if (UNMARSHALLING(ps)) {
01871                 blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
01872                 if (!blob->buffer) {
01873                         return False;
01874                 }
01875         }
01876 
01877         if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
01878                 return False;
01879 
01880         return True;
01881 }
01882 
01883 /*******************************************************************
01884 creates a UNIHDR structure.
01885 ********************************************************************/
01886 
01887 BOOL make_uni_hdr(UNIHDR *hdr, int len)
01888 {
01889         if (hdr == NULL)
01890         {
01891                 return False;
01892         }
01893         hdr->uni_str_len = 2 * len;
01894         hdr->uni_max_len = 2 * len;
01895         hdr->buffer      = len != 0 ? 1 : 0;
01896 
01897         return True;
01898 }
01899 
01900 /*******************************************************************
01901 creates a BUFHDR2 structure.
01902 ********************************************************************/
01903 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
01904 {
01905         hdr->info_level = info_level;
01906         hdr->length     = length;
01907         hdr->buffer     = buffer;
01908 
01909         return True;
01910 }
01911 
01912 /*******************************************************************
01913 return the length of a UNISTR string.
01914 ********************************************************************/  
01915 
01916 uint32 str_len_uni(UNISTR *source)
01917 {
01918         uint32 i=0;
01919 
01920         if (!source->buffer)
01921                 return 0;
01922 
01923         while (source->buffer[i])
01924                 i++;
01925 
01926         return i;
01927 }
01928 
01929 

Sambaに対してSat Aug 29 21:23:17 2009に生成されました。  doxygen 1.4.7