📄 clirap2.c
字号:
/* Samba Unix/Linux SMB client library More client RAP (SMB Remote Procedure Calls) functions Copyright (C) 2001 Steve French (sfrench@us.ibm.com) Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*//*****************************************************//* *//* Additional RAP functionality *//* *//* RAP is the original SMB RPC, documented *//* by Microsoft and X/Open in the 1990s and *//* supported by most SMB/CIFS servers although *//* it is unlikely that any one implementation *//* supports all RAP command codes since some *//* are quite obsolete and a few are specific *//* to a particular network operating system *//* */ /* Although it has largely been replaced */ /* for complex remote admistration and management *//* (of servers) by the relatively newer *//* DCE/RPC based remote API (which better handles *//* large >64K data structures), there are many *//* important administrative and resource location *//* tasks and user tasks (e.g. password change) *//* that are performed via RAP. *//* *//* Although a few of the RAP calls are implemented *//* in the Samba client library already (clirap.c) *//* the new ones are in clirap2.c for easy patching *//* and integration and a corresponding header *//* file, rap.h, has been created. *//* *//* This is based on data from the CIFS spec *//* and the LAN Server and LAN Manager *//* Programming Reference books and published *//* RAP document and CIFS forum postings and *//* lots of trial and error *//* *//* Function names changed from API_ (as they are *//* in the CIFS specification) to RAP_ in order *//* to avoid confusion with other API calls *//* sent via DCE RPC *//* *//*****************************************************//*****************************************************//* *//* cifsrap.c already includes support for: *//* *//* WshareEnum ( API number 0, level 1) *//* NetServerEnum2 (API num 104, level 1) *//* WWkstaUserLogon (132) *//* SamOEMchgPasswordUser2_P (214) *//* *//* cifsprint.c already includes support for: *//* *//* WPrintJobEnum (API num 76, level 2) *//* WPrintJobDel (API num 81) *//* *//*****************************************************/ #include "includes.h" #define WORDSIZE 2#define DWORDSIZE 4#define PUTBYTE(p,b) do {SCVAL(p,0,b); p++;} while(0)#define GETBYTE(p,b) do {b = CVAL(p,0); p++;} while(0)#define PUTWORD(p,w) do {SSVAL(p,0,w); p += WORDSIZE;} while(0)#define GETWORD(p,w) do {w = SVAL(p,0); p += WORDSIZE;} while(0)#define PUTDWORD(p,d) do {SIVAL(p,0,d); p += DWORDSIZE;} while(0)#define GETDWORD(p,d) do {d = IVAL(p,0); p += DWORDSIZE;} while(0)#define GETRES(p) p ? SVAL(p,0) : -1/* put string s at p with max len n and increment p past string */#define PUTSTRING(p,s,n) do {\ push_ascii(p,s?s:"",n?n:256,STR_TERMINATE);\ p = skip_string(p,1);\ } while(0)/* put string s and p, using fixed len l, and increment p by l */#define PUTSTRINGF(p,s,l) do {\ push_ascii(p,s?s:"",l,STR_TERMINATE);\ p += l;\ } while (0)/* put string pointer at p, supplying offset o from rdata r, store *//* dword offset at p, increment p by 4 and o by length of s. This *//* means on the first call, you must calc the offset yourself! */#define PUTSTRINGP(p,s,r,o) do {\ if (s) {\ push_ascii(r+o,s,strlen(s)+1,STR_TERMINATE);\ PUTDWORD(p,o);\ o += strlen(s) + 1;\ } else PUTDWORD(p,0);\ }while(0);/* get asciiz string s from p, increment p past string */#define GETSTRING(p,s) do {\ pull_ascii_pstring(s,p);\ p = skip_string(p,1);\ } while(0)/* get fixed length l string s from p, increment p by l */#define GETSTRINGF(p,s,l) do {\ pull_ascii_pstring(s,p);\ p += l;\ } while(0)/* get string s from offset (obtained at p) from rdata r - converter c */#define GETSTRINGP(p,s,r,c) do {\ uint32 off;\ GETDWORD(p,off);\ off &= 0x0000FFFF; /* mask the obsolete segment number from the offset */ \ pull_ascii_pstring(s, off?(r+off-c):"");\ } while(0)static char *make_header(char *param, uint16 apinum, const char *reqfmt, const char *datafmt){ PUTWORD(param,apinum); if (reqfmt) PUTSTRING(param,reqfmt,0); else *param++ = (char) 0; if (datafmt) PUTSTRING(param,datafmt,0); else *param++ = (char) 0; return param;} /**************************************************************************** call a NetGroupDelete - delete user group from remote server****************************************************************************/int cli_NetGroupDelete(struct cli_state *cli, const char *group_name ){ char *rparam = NULL; char *rdata = NULL; char *p; unsigned int rdrcnt,rprcnt; int res; char param[WORDSIZE /* api number */ +sizeof(RAP_NetGroupDel_REQ) /* parm string */ +1 /* no ret string */ +RAP_GROUPNAME_LEN /* group to del */ +WORDSIZE]; /* reserved word */ /* now send a SMBtrans command with api GroupDel */ p = make_header(param, RAP_WGroupDel, RAP_NetGroupDel_REQ, NULL); PUTSTRING(p, group_name, RAP_GROUPNAME_LEN); PUTWORD(p,0); /* reserved word MBZ on input */ if (cli_api(cli, param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */ NULL, 0, 200, /* data, length, maxlen */ &rparam, &rprcnt, /* return params, length */ &rdata, &rdrcnt)) /* return data, length */ { res = GETRES(rparam); if (res == 0) { /* nothing to do */ } else if ((res == 5) || (res == 65)) { DEBUG(1, ("Access Denied\n")); } else if (res == 2220) { DEBUG (1, ("Group does not exist\n")); } else { DEBUG(4,("NetGroupDelete res=%d\n", res)); } } else { res = -1; DEBUG(4,("NetGroupDelete failed\n")); } SAFE_FREE(rparam); SAFE_FREE(rdata); return res;}/**************************************************************************** call a NetGroupAdd - add user group to remote server****************************************************************************/int cli_NetGroupAdd(struct cli_state *cli, RAP_GROUP_INFO_1 * grinfo ){ char *rparam = NULL; char *rdata = NULL; char *p; unsigned int rdrcnt,rprcnt; int res; char param[WORDSIZE /* api number */ +sizeof(RAP_NetGroupAdd_REQ) /* req string */ +sizeof(RAP_GROUP_INFO_L1) /* return string */ +WORDSIZE /* info level */ +WORDSIZE]; /* reserved word */ char data[1024]; /* offset into data of free format strings. Will be updated */ /* by PUTSTRINGP macro and end up with total data length. */ int soffset = RAP_GROUPNAME_LEN + 1 + DWORDSIZE; /* now send a SMBtrans command with api WGroupAdd */ p = make_header(param, RAP_WGroupAdd, RAP_NetGroupAdd_REQ, RAP_GROUP_INFO_L1); PUTWORD(p, 1); /* info level */ PUTWORD(p, 0); /* reserved word 0 */ p = data; PUTSTRINGF(p, grinfo->group_name, RAP_GROUPNAME_LEN); PUTBYTE(p, 0); /* pad byte 0 */ PUTSTRINGP(p, grinfo->comment, data, soffset); if (cli_api(cli, param, sizeof(param), 1024, /* Param, length, maxlen */ data, soffset, sizeof(data), /* data, length, maxlen */ &rparam, &rprcnt, /* return params, length */ &rdata, &rdrcnt)) /* return data, length */ { res = GETRES(rparam); if (res == 0) { /* nothing to do */ } else if ((res == 5) || (res == 65)) { DEBUG(1, ("Access Denied\n")); } else if (res == 2223) { DEBUG (1, ("Group already exists\n")); } else { DEBUG(4,("NetGroupAdd res=%d\n", res)); } } else { res = -1; DEBUG(4,("NetGroupAdd failed\n")); } SAFE_FREE(rparam); SAFE_FREE(rdata); return res;}/****************************************************************************call a NetGroupEnum - try and list user groups on a different host****************************************************************************/int cli_RNetGroupEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state){ char param[WORDSIZE /* api number */ +sizeof(RAP_NetGroupEnum_REQ) /* parm string */ +sizeof(RAP_GROUP_INFO_L1) /* return string */ +WORDSIZE /* info level */ +WORDSIZE]; /* buffer size */ char *p; char *rparam = NULL; char *rdata = NULL; unsigned int rprcnt, rdrcnt; int res = -1; memset(param, '\0', sizeof(param)); p = make_header(param, RAP_WGroupEnum, RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L1); PUTWORD(p,1); /* Info level 1 */ /* add level 0 */ PUTWORD(p,0xFFE0); /* Return buffer size */ if (cli_api(cli, param, PTR_DIFF(p,param),8, NULL, 0, 0xFFE0 /* data area size */, &rparam, &rprcnt, &rdata, &rdrcnt)) { res = GETRES(rparam); cli->rap_error = res; if(cli->rap_error == 234) DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n")); else if (cli->rap_error != 0) { DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error)); } } if (rdata) { if (res == 0 || res == ERRmoredata) { int i, converter, count; p = rparam + WORDSIZE; /* skip result */ GETWORD(p, converter); GETWORD(p, count); for (i=0,p=rdata;i<count;i++) { pstring comment; char groupname[RAP_GROUPNAME_LEN]; GETSTRINGF(p, groupname, RAP_GROUPNAME_LEN); p++; /* pad byte */ GETSTRINGP(p, comment, rdata, converter); fn(groupname, comment, cli); } } else { DEBUG(4,("NetGroupEnum res=%d\n", res)); } } else { DEBUG(4,("NetGroupEnum no data returned\n")); } SAFE_FREE(rparam); SAFE_FREE(rdata); return res;}int cli_RNetGroupEnum0(struct cli_state *cli, void (*fn)(const char *, void *), void *state){ char param[WORDSIZE /* api number */ +sizeof(RAP_NetGroupEnum_REQ) /* parm string */ +sizeof(RAP_GROUP_INFO_L0) /* return string */ +WORDSIZE /* info level */ +WORDSIZE]; /* buffer size */ char *p; char *rparam = NULL; char *rdata = NULL; unsigned int rprcnt, rdrcnt; int res = -1; memset(param, '\0', sizeof(param)); p = make_header(param, RAP_WGroupEnum, RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L0); PUTWORD(p,0); /* Info level 0 */ /* Hmmm. I *very* much suspect this is the resume count, at least that's what smbd believes... */ PUTWORD(p,0xFFE0); /* Return buffer size */ if (cli_api(cli, param, PTR_DIFF(p,param),8, NULL, 0, 0xFFE0 /* data area size */, &rparam, &rprcnt, &rdata, &rdrcnt)) { res = GETRES(rparam); cli->rap_error = res; if(cli->rap_error == 234) DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n")); else if (cli->rap_error != 0) { DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error)); } } if (rdata) { if (res == 0 || res == ERRmoredata) { int i, count; p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */ GETWORD(p, count); for (i=0,p=rdata;i<count;i++) { char groupname[RAP_GROUPNAME_LEN]; GETSTRINGF(p, groupname, RAP_GROUPNAME_LEN); fn(groupname, cli); } } else { DEBUG(4,("NetGroupEnum res=%d\n", res)); } } else { DEBUG(4,("NetGroupEnum no data returned\n")); } SAFE_FREE(rparam); SAFE_FREE(rdata); return res;}int cli_NetGroupDelUser(struct cli_state * cli, const char *group_name, const char *user_name){ char *rparam = NULL; char *rdata = NULL; char *p; unsigned int rdrcnt,rprcnt; int res; char param[WORDSIZE /* api number */ +sizeof(RAP_NetGroupDelUser_REQ) /* parm string */ +1 /* no ret string */ +RAP_GROUPNAME_LEN /* group name */ +RAP_USERNAME_LEN]; /* user to del */ /* now send a SMBtrans command with api GroupMemberAdd */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -