util.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. Samba utility functions
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
  20. #ifdef WITH_NISPLUS_HOME
  21. #ifdef BROKEN_NISPLUS_INCLUDE_FILES
  22. /*
  23. * The following lines are needed due to buggy include files
  24. * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
  25. * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
  26. * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
  27. * an enum in /usr/include/rpcsvc/nis.h.
  28. */
  29. #if defined(GROUP)
  30. #undef GROUP
  31. #endif
  32. #if defined(GROUP_OBJ)
  33. #undef GROUP_OBJ
  34. #endif
  35. #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
  36. #include <rpcsvc/nis.h>
  37. #else /* !WITH_NISPLUS_HOME */
  38. #include "rpcsvc/ypclnt.h"
  39. #endif /* WITH_NISPLUS_HOME */
  40. #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
  41. #ifdef WITH_SSL
  42. #include <ssl.h>
  43. #undef Realloc /* SSLeay defines this and samba has a function of this name */
  44. extern SSL *ssl;
  45. extern int sslFd;
  46. #endif /* WITH_SSL */
  47. extern int DEBUGLEVEL;
  48. #if 0
  49. int Protocol = PROTOCOL_COREPLUS;
  50. #endif /*0 */
  51. /* a default finfo structure to ensure all fields are sensible */
  52. file_info const def_finfo = {-1,0,0,0,0,0,0,""};
  53. /* the client file descriptor */
  54. extern int Client;
  55. /* this is used by the chaining code */
  56. const int chain_size = 0;
  57. #if 0
  58. int trans_num = 0;
  59. #endif /*0 */
  60. /*
  61. case handling on filenames
  62. */
  63. const int case_default = CASE_LOWER;
  64. #if 0
  65. /* the following control case operations - they are put here so the
  66. client can link easily */
  67. BOOL case_sensitive;
  68. BOOL case_preserve;
  69. BOOL use_mangled_map = False;
  70. BOOL short_case_preserve;
  71. BOOL case_mangle;
  72. #endif /*0 */
  73. static const char *remote_machine = "";
  74. static const char *local_machine = "";
  75. static const char *remote_arch = "UNKNOWN";
  76. #if 0
  77. static enum remote_arch_types ra_type = RA_UNKNOWN;
  78. #endif
  79. static const char *remote_proto = "UNKNOWN";
  80. pstring myhostname="";
  81. pstring user_socket_options="";
  82. pstring sesssetup_user="";
  83. static const char * const samlogon_user = "";
  84. const BOOL sam_logon_in_ssb = False;
  85. pstring global_myname = "";
  86. #if 0
  87. char **my_netbios_names;
  88. #endif /*0 */
  89. /****************************************************************************
  90. find a suitable temporary directory. The result should be copied immediately
  91. as it may be overwritten by a subsequent call
  92. ****************************************************************************/
  93. const char *tmpdir(void)
  94. {
  95. char *p;
  96. if ((p = getenv("MC_TMPDIR")) || (p = getenv("TMPDIR"))) {
  97. return p;
  98. }
  99. return "/tmp";
  100. }
  101. /****************************************************************************
  102. determine whether we are in the specified group
  103. ****************************************************************************/
  104. #if 0
  105. BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups)
  106. {
  107. int i;
  108. if (group == current_gid) return(True);
  109. for (i=0;i<ngroups;i++)
  110. if (group == groups[i])
  111. return(True);
  112. return(False);
  113. }
  114. /****************************************************************************
  115. like atoi but gets the value up to the separater character
  116. ****************************************************************************/
  117. char *Atoic(char *p, int *n, char *c)
  118. {
  119. if (!isdigit((int)*p))
  120. {
  121. DEBUG(5, ("Atoic: malformed number\n"));
  122. return NULL;
  123. }
  124. (*n) = atoi(p);
  125. while ((*p) && isdigit((int)*p))
  126. {
  127. p++;
  128. }
  129. if (strchr(c, *p) == NULL)
  130. {
  131. DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c));
  132. return NULL;
  133. }
  134. return p;
  135. }
  136. /*************************************************************************
  137. reads a list of numbers
  138. *************************************************************************/
  139. char *get_numlist(char *p, uint32 **num, int *count)
  140. {
  141. int val;
  142. if (num == NULL || count == NULL)
  143. {
  144. return NULL;
  145. }
  146. (*count) = 0;
  147. (*num ) = NULL;
  148. while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':')
  149. {
  150. (*num) = Realloc((*num), ((*count)+1) * sizeof(uint32));
  151. if ((*num) == NULL)
  152. {
  153. return NULL;
  154. }
  155. (*num)[(*count)] = val;
  156. (*count)++;
  157. p++;
  158. }
  159. return p;
  160. }
  161. #endif /* 0 */
  162. /*******************************************************************
  163. copy an IP address from one buffer to another
  164. ********************************************************************/
  165. void putip(void *dest,void *src)
  166. {
  167. memcpy(dest,src,4);
  168. }
  169. #define TRUNCATE_NETBIOS_NAME 1
  170. #if 0
  171. /*******************************************************************
  172. convert, possibly using a stupid microsoft-ism which has destroyed
  173. the transport independence of netbios (for CIFS vendors that usually
  174. use the Win95-type methods, not for NT to NT communication, which uses
  175. DCE/RPC and therefore full-length unicode strings...) a dns name into
  176. a netbios name.
  177. the netbios name (NOT necessarily null-terminated) is truncated to 15
  178. characters.
  179. ******************************************************************/
  180. char *dns_to_netbios_name(char *dns_name)
  181. {
  182. static char netbios_name[16];
  183. int i;
  184. StrnCpy(netbios_name, dns_name, 15);
  185. netbios_name[15] = 0;
  186. #ifdef TRUNCATE_NETBIOS_NAME
  187. /* ok. this is because of a stupid microsoft-ism. if the called host
  188. name contains a '.', microsoft clients expect you to truncate the
  189. netbios name up to and including the '.' this even applies, by
  190. mistake, to workgroup (domain) names, which is _really_ daft.
  191. */
  192. for (i = 15; i >= 0; i--)
  193. {
  194. if (netbios_name[i] == '.')
  195. {
  196. netbios_name[i] = 0;
  197. break;
  198. }
  199. }
  200. #endif /* TRUNCATE_NETBIOS_NAME */
  201. return netbios_name;
  202. }
  203. /****************************************************************************
  204. interpret the weird netbios "name". Return the name type
  205. ****************************************************************************/
  206. static int name_interpret(char *in,char *out)
  207. {
  208. int ret;
  209. int len = (*in++) / 2;
  210. *out=0;
  211. if (len > 30 || len<1) return(0);
  212. while (len--)
  213. {
  214. if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
  215. *out = 0;
  216. return(0);
  217. }
  218. *out = ((in[0]-'A')<<4) + (in[1]-'A');
  219. in += 2;
  220. out++;
  221. }
  222. *out = 0;
  223. ret = out[-1];
  224. #ifdef NETBIOS_SCOPE
  225. /* Handle any scope names */
  226. while(*in)
  227. {
  228. *out++ = '.'; /* Scope names are separated by periods */
  229. len = *(unsigned char *)in++;
  230. StrnCpy(out, in, len);
  231. out += len;
  232. *out=0;
  233. in += len;
  234. }
  235. #endif
  236. return(ret);
  237. }
  238. #endif /* 0 */
  239. /****************************************************************************
  240. mangle a name into netbios format
  241. Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
  242. ****************************************************************************/
  243. int name_mangle( char *In, char *Out, char name_type )
  244. {
  245. int i;
  246. int c;
  247. int len;
  248. char buf[20];
  249. char *p = Out;
  250. extern pstring global_scope;
  251. /* Safely copy the input string, In, into buf[]. */
  252. (void)memset( buf, 0, 20 );
  253. if (strcmp(In,"*") == 0)
  254. buf[0] = '*';
  255. else
  256. (void)slprintf( buf, sizeof(buf) - 1, "%-15.15s%c", In, name_type );
  257. /* Place the length of the first field into the output buffer. */
  258. p[0] = 32;
  259. p++;
  260. /* Now convert the name to the rfc1001/1002 format. */
  261. for( i = 0; i < 16; i++ )
  262. {
  263. c = toupper( buf[i] );
  264. p[i*2] = ( (c >> 4) & 0x000F ) + 'A';
  265. p[(i*2)+1] = (c & 0x000F) + 'A';
  266. }
  267. p += 32;
  268. p[0] = '\0';
  269. /* Add the scope string. */
  270. for( i = 0, len = 0; NULL != global_scope; i++, len++ )
  271. {
  272. switch( global_scope[i] )
  273. {
  274. case '\0':
  275. p[0] = len;
  276. if( len > 0 )
  277. p[len+1] = 0;
  278. return( name_len(Out) );
  279. case '.':
  280. p[0] = len;
  281. p += (len + 1);
  282. len = -1;
  283. break;
  284. default:
  285. p[len+1] = global_scope[i];
  286. break;
  287. }
  288. }
  289. return( name_len(Out) );
  290. } /* name_mangle */
  291. /*******************************************************************
  292. check if a file exists
  293. ********************************************************************/
  294. BOOL file_exist(char *fname,SMB_STRUCT_STAT *sbuf)
  295. {
  296. SMB_STRUCT_STAT st;
  297. if (!sbuf) sbuf = &st;
  298. if (sys_stat(fname,sbuf) != 0)
  299. return(False);
  300. return(S_ISREG(sbuf->st_mode));
  301. }
  302. /*******************************************************************
  303. check a files mod time
  304. ********************************************************************/
  305. time_t file_modtime(char *fname)
  306. {
  307. SMB_STRUCT_STAT st;
  308. if (sys_stat(fname,&st) != 0)
  309. return(0);
  310. return(st.st_mtime);
  311. }
  312. #if 0
  313. /*******************************************************************
  314. check if a directory exists
  315. ********************************************************************/
  316. BOOL directory_exist(char *dname,SMB_STRUCT_STAT *st)
  317. {
  318. SMB_STRUCT_STAT st2;
  319. BOOL ret;
  320. if (!st) st = &st2;
  321. if (sys_stat(dname,st) != 0)
  322. return(False);
  323. ret = S_ISDIR(st->st_mode);
  324. if(!ret)
  325. errno = ENOTDIR;
  326. return ret;
  327. }
  328. /*******************************************************************
  329. returns the size in bytes of the named file
  330. ********************************************************************/
  331. SMB_OFF_T file_size(char *file_name)
  332. {
  333. SMB_STRUCT_STAT buf;
  334. buf.st_size = 0;
  335. if(sys_stat(file_name,&buf) != 0)
  336. return (SMB_OFF_T)-1;
  337. return(buf.st_size);
  338. }
  339. #endif /* 0 */
  340. /*******************************************************************
  341. return a string representing an attribute for a file
  342. ********************************************************************/
  343. char *attrib_string(uint16 mode)
  344. {
  345. static fstring attrstr;
  346. attrstr[0] = 0;
  347. if (mode & aVOLID) fstrcat(attrstr,"V");
  348. if (mode & aDIR) fstrcat(attrstr,"D");
  349. if (mode & aARCH) fstrcat(attrstr,"A");
  350. if (mode & aHIDDEN) fstrcat(attrstr,"H");
  351. if (mode & aSYSTEM) fstrcat(attrstr,"S");
  352. if (mode & aRONLY) fstrcat(attrstr,"R");
  353. return(attrstr);
  354. }
  355. #if 0
  356. /****************************************************************************
  357. make a file into unix format
  358. ****************************************************************************/
  359. void unix_format(char *fname)
  360. {
  361. string_replace(fname,'\\','/');
  362. }
  363. /****************************************************************************
  364. make a file into dos format
  365. ****************************************************************************/
  366. void dos_format(char *fname)
  367. {
  368. string_replace(fname,'/','\\');
  369. }
  370. #endif /* 0 */
  371. /*******************************************************************
  372. show a smb message structure
  373. ********************************************************************/
  374. void show_msg(char *buf)
  375. {
  376. int i;
  377. int bcc=0;
  378. if (DEBUGLEVEL < 5) return;
  379. DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
  380. smb_len(buf),
  381. (int)CVAL(buf,smb_com),
  382. (int)CVAL(buf,smb_rcls),
  383. (int)CVAL(buf,smb_reh),
  384. (int)SVAL(buf,smb_err),
  385. (int)CVAL(buf,smb_flg),
  386. (int)SVAL(buf,smb_flg2)));
  387. DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
  388. (int)SVAL(buf,smb_tid),
  389. (int)SVAL(buf,smb_pid),
  390. (int)SVAL(buf,smb_uid),
  391. (int)SVAL(buf,smb_mid),
  392. (int)CVAL(buf,smb_wct)));
  393. for (i=0;i<(int)CVAL(buf,smb_wct);i++)
  394. {
  395. DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
  396. SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
  397. }
  398. bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
  399. DEBUG(5,("smb_bcc=%d\n",bcc));
  400. if (DEBUGLEVEL < 10) return;
  401. if (DEBUGLEVEL < 50)
  402. {
  403. bcc = MIN(bcc, 512);
  404. }
  405. dump_data(10, smb_buf(buf), bcc);
  406. }
  407. /*******************************************************************
  408. return the length of an smb packet
  409. ********************************************************************/
  410. int smb_len(char *buf)
  411. {
  412. return( PVAL(buf,3) | (PVAL(buf,2)<<8) | ((PVAL(buf,1)&1)<<16) );
  413. }
  414. /*******************************************************************
  415. set the length of an smb packet
  416. ********************************************************************/
  417. void _smb_setlen(char *buf,int len)
  418. {
  419. buf[0] = 0;
  420. buf[1] = (len&0x10000)>>16;
  421. buf[2] = (len&0xFF00)>>8;
  422. buf[3] = len&0xFF;
  423. }
  424. /*******************************************************************
  425. set the length and marker of an smb packet
  426. ********************************************************************/
  427. void smb_setlen(char *buf,int len)
  428. {
  429. _smb_setlen(buf,len);
  430. CVAL(buf,4) = 0xFF;
  431. CVAL(buf,5) = 'S';
  432. CVAL(buf,6) = 'M';
  433. CVAL(buf,7) = 'B';
  434. }
  435. /*******************************************************************
  436. setup the word count and byte count for a smb message
  437. ********************************************************************/
  438. int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
  439. {
  440. if (zero)
  441. memset(buf + smb_size,'\0',num_words*2 + num_bytes);
  442. CVAL(buf,smb_wct) = num_words;
  443. SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
  444. smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
  445. return (smb_size + num_words*2 + num_bytes);
  446. }
  447. /*******************************************************************
  448. return the number of smb words
  449. ********************************************************************/
  450. static int smb_numwords(char *buf)
  451. {
  452. return (CVAL(buf,smb_wct));
  453. }
  454. /*******************************************************************
  455. return the size of the smb_buf region of a message
  456. ********************************************************************/
  457. int smb_buflen(char *buf)
  458. {
  459. return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));
  460. }
  461. /*******************************************************************
  462. return a pointer to the smb_buf data area
  463. ********************************************************************/
  464. static int smb_buf_ofs(char *buf)
  465. {
  466. return (smb_size + CVAL(buf,smb_wct)*2);
  467. }
  468. /*******************************************************************
  469. return a pointer to the smb_buf data area
  470. ********************************************************************/
  471. char *smb_buf(char *buf)
  472. {
  473. return (buf + smb_buf_ofs(buf));
  474. }
  475. /*******************************************************************
  476. return the SMB offset into an SMB buffer
  477. ********************************************************************/
  478. int smb_offset(char *p,char *buf)
  479. {
  480. return(PTR_DIFF(p,buf+4) + chain_size);
  481. }
  482. #if 0
  483. /*******************************************************************
  484. reduce a file name, removing .. elements.
  485. ********************************************************************/
  486. void dos_clean_name(char *s)
  487. {
  488. char *p=NULL;
  489. DEBUG(3,("dos_clean_name [%s]\n",s));
  490. /* remove any double slashes */
  491. string_sub(s, "\\\\", "\\");
  492. while ((p = strstr(s,"\\..\\")) != NULL)
  493. {
  494. pstring s1;
  495. *p = 0;
  496. pstrcpy(s1,p+3);
  497. if ((p=strrchr(s,'\\')) != NULL)
  498. *p = 0;
  499. else
  500. *s = 0;
  501. pstrcat(s,s1);
  502. }
  503. trim_string(s,NULL,"\\..");
  504. string_sub(s, "\\.\\", "\\");
  505. }
  506. /*******************************************************************
  507. reduce a file name, removing .. elements.
  508. ********************************************************************/
  509. void unix_clean_name(char *s)
  510. {
  511. char *p=NULL;
  512. DEBUG(3,("unix_clean_name [%s]\n",s));
  513. /* remove any double slashes */
  514. string_sub(s, "//","/");
  515. /* Remove leading ./ characters */
  516. if(strncmp(s, "./", 2) == 0) {
  517. trim_string(s, "./", NULL);
  518. if(*s == 0)
  519. pstrcpy(s,"./");
  520. }
  521. while ((p = strstr(s,"/../")) != NULL)
  522. {
  523. pstring s1;
  524. *p = 0;
  525. pstrcpy(s1,p+3);
  526. if ((p=strrchr(s,'/')) != NULL)
  527. *p = 0;
  528. else
  529. *s = 0;
  530. pstrcat(s,s1);
  531. }
  532. trim_string(s,NULL,"/..");
  533. }
  534. /*******************************************************************
  535. reduce a file name, removing .. elements and checking that
  536. it is below dir in the heirachy. This uses dos_GetWd() and so must be run
  537. on the system that has the referenced file system.
  538. widelinks are allowed if widelinks is true
  539. ********************************************************************/
  540. BOOL reduce_name(char *s,char *dir,BOOL widelinks)
  541. {
  542. #ifndef REDUCE_PATHS
  543. return True;
  544. #else
  545. pstring dir2;
  546. pstring wd;
  547. pstring base_name;
  548. pstring newname;
  549. char *p=NULL;
  550. BOOL relative = (*s != '/');
  551. *dir2 = *wd = *base_name = *newname = 0;
  552. if (widelinks)
  553. {
  554. unix_clean_name(s);
  555. /* can't have a leading .. */
  556. if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/'))
  557. {
  558. DEBUG(3,("Illegal file name? (%s)\n",s));
  559. return(False);
  560. }
  561. if (strlen(s) == 0)
  562. pstrcpy(s,"./");
  563. return(True);
  564. }
  565. DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
  566. /* remove any double slashes */
  567. string_sub(s,"//","/");
  568. pstrcpy(base_name,s);
  569. p = strrchr(base_name,'/');
  570. if (!p)
  571. return(True);
  572. if (!dos_GetWd(wd))
  573. {
  574. DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
  575. return(False);
  576. }
  577. if (dos_ChDir(dir) != 0)
  578. {
  579. DEBUG(0,("couldn't chdir to %s\n",dir));
  580. return(False);
  581. }
  582. if (!dos_GetWd(dir2))
  583. {
  584. DEBUG(0,("couldn't getwd for %s\n",dir));
  585. dos_ChDir(wd);
  586. return(False);
  587. }
  588. if (p && (p != base_name))
  589. {
  590. *p = 0;
  591. if (strcmp(p+1,".")==0)
  592. p[1]=0;
  593. if (strcmp(p+1,"..")==0)
  594. *p = '/';
  595. }
  596. if (dos_ChDir(base_name) != 0)
  597. {
  598. dos_ChDir(wd);
  599. DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,base_name));
  600. return(False);
  601. }
  602. if (!dos_GetWd(newname))
  603. {
  604. dos_ChDir(wd);
  605. DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
  606. return(False);
  607. }
  608. if (p && (p != base_name))
  609. {
  610. pstrcat(newname,"/");
  611. pstrcat(newname,p+1);
  612. }
  613. {
  614. size_t l = strlen(dir2);
  615. if (dir2[l-1] == '/')
  616. l--;
  617. if (strncmp(newname,dir2,l) != 0)
  618. {
  619. dos_ChDir(wd);
  620. DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,(int)l));
  621. return(False);
  622. }
  623. if (relative)
  624. {
  625. if (newname[l] == '/')
  626. pstrcpy(s,newname + l + 1);
  627. else
  628. pstrcpy(s,newname+l);
  629. }
  630. else
  631. pstrcpy(s,newname);
  632. }
  633. dos_ChDir(wd);
  634. if (strlen(s) == 0)
  635. pstrcpy(s,"./");
  636. DEBUG(3,("reduced to %s\n",s));
  637. return(True);
  638. #endif
  639. }
  640. /****************************************************************************
  641. expand some *s
  642. ****************************************************************************/
  643. static void expand_one(char *Mask,int len)
  644. {
  645. char *p1;
  646. while ((p1 = strchr(Mask,'*')) != NULL)
  647. {
  648. int lfill = (len+1) - strlen(Mask);
  649. int l1= (p1 - Mask);
  650. pstring tmp;
  651. pstrcpy(tmp,Mask);
  652. memset(tmp+l1,'?',lfill);
  653. pstrcpy(tmp + l1 + lfill,Mask + l1 + 1);
  654. pstrcpy(Mask,tmp);
  655. }
  656. }
  657. /****************************************************************************
  658. parse out a directory name from a path name. Assumes dos style filenames.
  659. ****************************************************************************/
  660. static void dirname_dos(char *path,char *buf)
  661. {
  662. split_at_last_component(path, buf, '\\', NULL);
  663. }
  664. /****************************************************************************
  665. parse out a filename from a path name. Assumes dos style filenames.
  666. ****************************************************************************/
  667. static char *filename_dos(char *path,char *buf)
  668. {
  669. char *p = strrchr(path,'\\');
  670. if (!p)
  671. pstrcpy(buf,path);
  672. else
  673. pstrcpy(buf,p+1);
  674. return(buf);
  675. }
  676. /****************************************************************************
  677. expand a wildcard expression, replacing *s with ?s
  678. ****************************************************************************/
  679. void expand_mask(char *Mask,BOOL doext)
  680. {
  681. pstring mbeg,mext;
  682. pstring dirpart;
  683. pstring filepart;
  684. BOOL hasdot = False;
  685. char *p1;
  686. BOOL absolute = (*Mask == '\\');
  687. *mbeg = *mext = *dirpart = *filepart = 0;
  688. /* parse the directory and filename */
  689. if (strchr(Mask,'\\'))
  690. dirname_dos(Mask,dirpart);
  691. filename_dos(Mask,filepart);
  692. pstrcpy(mbeg,filepart);
  693. if ((p1 = strchr(mbeg,'.')) != NULL)
  694. {
  695. hasdot = True;
  696. *p1 = 0;
  697. p1++;
  698. pstrcpy(mext,p1);
  699. }
  700. else
  701. {
  702. pstrcpy(mext,"");
  703. if (strlen(mbeg) > 8)
  704. {
  705. pstrcpy(mext,mbeg + 8);
  706. mbeg[8] = 0;
  707. }
  708. }
  709. if (*mbeg == 0)
  710. pstrcpy(mbeg,"????????");
  711. if ((*mext == 0) && doext && !hasdot)
  712. pstrcpy(mext,"???");
  713. if (strequal(mbeg,"*") && *mext==0)
  714. pstrcpy(mext,"*");
  715. /* expand *'s */
  716. expand_one(mbeg,8);
  717. if (*mext)
  718. expand_one(mext,3);
  719. pstrcpy(Mask,dirpart);
  720. if (*dirpart || absolute) pstrcat(Mask,"\\");
  721. pstrcat(Mask,mbeg);
  722. pstrcat(Mask,".");
  723. pstrcat(Mask,mext);
  724. DEBUG(6,("Mask expanded to [%s]\n",Mask));
  725. }
  726. /****************************************************************************
  727. make a dir struct
  728. ****************************************************************************/
  729. void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,time_t date)
  730. {
  731. char *p;
  732. pstring mask2;
  733. pstrcpy(mask2,mask);
  734. if ((mode & aDIR) != 0)
  735. size = 0;
  736. memset(buf+1,' ',11);
  737. if ((p = strchr(mask2,'.')) != NULL)
  738. {
  739. *p = 0;
  740. memcpy(buf+1,mask2,MIN(strlen(mask2),8));
  741. memcpy(buf+9,p+1,MIN(strlen(p+1),3));
  742. *p = '.';
  743. }
  744. else
  745. memcpy(buf+1,mask2,MIN(strlen(mask2),11));
  746. memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
  747. CVAL(buf,21) = mode;
  748. put_dos_date(buf,22,date);
  749. SSVAL(buf,26,size & 0xFFFF);
  750. SSVAL(buf,28,(size >> 16)&0xFFFF);
  751. StrnCpy(buf+30,fname,12);
  752. if (!case_sensitive)
  753. strupper(buf+30);
  754. DEBUG(8,("put name [%s] into dir struct\n",buf+30));
  755. }
  756. /*******************************************************************
  757. close the low 3 fd's and open dev/null in their place
  758. ********************************************************************/
  759. void close_low_fds(void)
  760. {
  761. int fd;
  762. int i;
  763. close(0); close(1); close(2);
  764. /* try and use up these file descriptors, so silly
  765. library routines writing to stdout etc won't cause havoc */
  766. for (i=0;i<3;i++) {
  767. fd = sys_open("/dev/null",O_RDWR,0);
  768. if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0);
  769. if (fd < 0) {
  770. DEBUG(0,("Cannot open /dev/null\n"));
  771. return;
  772. }
  773. if (fd != i) {
  774. DEBUG(0,("Didn't get file descriptor %d\n",i));
  775. return;
  776. }
  777. }
  778. }
  779. #endif /* 0 */
  780. /****************************************************************************
  781. Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
  782. else
  783. if SYSV use O_NDELAY
  784. if BSD use FNDELAY
  785. ****************************************************************************/
  786. int set_blocking(int fd, BOOL set)
  787. {
  788. int val;
  789. #ifdef O_NONBLOCK
  790. #define FLAG_TO_SET O_NONBLOCK
  791. #else
  792. #ifdef SYSV
  793. #define FLAG_TO_SET O_NDELAY
  794. #else /* BSD */
  795. #define FLAG_TO_SET FNDELAY
  796. #endif
  797. #endif
  798. if((val = fcntl(fd, F_GETFL, 0)) == -1)
  799. return -1;
  800. if(set) /* Turn blocking on - ie. clear nonblock flag */
  801. val &= ~FLAG_TO_SET;
  802. else
  803. val |= FLAG_TO_SET;
  804. return fcntl( fd, F_SETFL, val);
  805. #undef FLAG_TO_SET
  806. }
  807. /*******************************************************************
  808. find the difference in milliseconds between two struct timeval
  809. values
  810. ********************************************************************/
  811. int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
  812. {
  813. return((tvalnew->tv_sec - tvalold->tv_sec)*1000 +
  814. ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);
  815. }
  816. #if 0
  817. /****************************************************************************
  818. transfer some data between two fd's
  819. ****************************************************************************/
  820. SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n,char *header,int headlen,int align)
  821. {
  822. static char *buf=NULL;
  823. static int size=0;
  824. char *buf1,*abuf;
  825. SMB_OFF_T total = 0;
  826. DEBUG(4,("transfer_file n=%.0f (head=%d) called\n",(double)n,headlen));
  827. if (size == 0) {
  828. size = lp_readsize();
  829. size = MAX(size,1024);
  830. }
  831. while (!buf && size>0) {
  832. buf = (char *)Realloc(buf,size+8);
  833. if (!buf) size /= 2;
  834. }
  835. if (!buf) {
  836. DEBUG(0,("Cannot allocate transfer buffer!\n"));
  837. exit(1);
  838. }
  839. abuf = buf + (align%8);
  840. if (header)
  841. n += headlen;
  842. while (n > 0)
  843. {
  844. int s = (int)MIN(n,(SMB_OFF_T)size);
  845. int ret,ret2=0;
  846. ret = 0;
  847. if (header && (headlen >= MIN(s,1024))) {
  848. buf1 = header;
  849. s = headlen;
  850. ret = headlen;
  851. headlen = 0;
  852. header = NULL;
  853. } else {
  854. buf1 = abuf;
  855. }
  856. if (header && headlen > 0)
  857. {
  858. ret = MIN(headlen,size);
  859. memcpy(buf1,header,ret);
  860. headlen -= ret;
  861. header += ret;
  862. if (headlen <= 0) header = NULL;
  863. }
  864. if (s > ret)
  865. ret += read(infd,buf1+ret,s-ret);
  866. if (ret > 0)
  867. {
  868. ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
  869. if (ret2 > 0) total += ret2;
  870. /* if we can't write then dump excess data */
  871. if (ret2 != ret)
  872. transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
  873. }
  874. if (ret <= 0 || ret2 != ret)
  875. return(total);
  876. n -= ret;
  877. }
  878. return(total);
  879. }
  880. /****************************************************************************
  881. find a pointer to a netbios name
  882. ****************************************************************************/
  883. static char *name_ptr(char *buf,int ofs)
  884. {
  885. unsigned char c = *(unsigned char *)(buf+ofs);
  886. if ((c & 0xC0) == 0xC0)
  887. {
  888. uint16 l;
  889. char p[2];
  890. memcpy(p,buf+ofs,2);
  891. p[0] &= ~0xC0;
  892. l = RSVAL(p,0);
  893. DEBUG(5,("name ptr to pos %d from %d is %s\n",l,ofs,buf+l));
  894. return(buf + l);
  895. }
  896. else
  897. return(buf+ofs);
  898. }
  899. /****************************************************************************
  900. extract a netbios name from a buf
  901. ****************************************************************************/
  902. int name_extract(char *buf,int ofs,char *name)
  903. {
  904. char *p = name_ptr(buf,ofs);
  905. int d = PTR_DIFF(p,buf+ofs);
  906. pstrcpy(name,"");
  907. if (d < -50 || d > 50) return(0);
  908. return(name_interpret(p,name));
  909. }
  910. #endif /* 0 */
  911. /****************************************************************************
  912. return the total storage length of a mangled name
  913. ****************************************************************************/
  914. int name_len(char *s1)
  915. {
  916. /* NOTE: this argument _must_ be unsigned */
  917. unsigned char *s = (unsigned char *)s1;
  918. int len;
  919. /* If the two high bits of the byte are set, return 2. */
  920. if (0xC0 == (*s & 0xC0))
  921. return(2);
  922. /* Add up the length bytes. */
  923. for (len = 1; (*s); s += (*s) + 1) {
  924. len += *s + 1;
  925. SMB_ASSERT(len < 80);
  926. }
  927. return(len);
  928. } /* name_len */
  929. /*******************************************************************
  930. sleep for a specified number of milliseconds
  931. ********************************************************************/
  932. void msleep(int t)
  933. {
  934. int tdiff=0;
  935. struct timeval tval,t1,t2;
  936. fd_set fds;
  937. GetTimeOfDay(&t1);
  938. GetTimeOfDay(&t2);
  939. while (tdiff < t) {
  940. tval.tv_sec = (t-tdiff)/1000;
  941. tval.tv_usec = 1000*((t-tdiff)%1000);
  942. FD_ZERO(&fds);
  943. errno = 0;
  944. sys_select(0,&fds,&tval);
  945. GetTimeOfDay(&t2);
  946. tdiff = TvalDiff(&t1,&t2);
  947. }
  948. }
  949. #if 0
  950. /*********************************************************
  951. * Recursive routine that is called by unix_mask_match.
  952. * Does the actual matching. This is the 'original code'
  953. * used by the unix matcher.
  954. *********************************************************/
  955. static BOOL unix_do_match(char *str, char *regexp, int case_sig)
  956. {
  957. char *p;
  958. for( p = regexp; *p && *str; ) {
  959. switch(*p) {
  960. case '?':
  961. str++; p++;
  962. break;
  963. case '*':
  964. /* Look for a character matching
  965. the one after the '*' */
  966. p++;
  967. if(!*p)
  968. return True; /* Automatic match */
  969. while(*str) {
  970. while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
  971. str++;
  972. if(unix_do_match(str,p,case_sig))
  973. return True;
  974. if(!*str)
  975. return False;
  976. else
  977. str++;
  978. }
  979. return False;
  980. default:
  981. if(case_sig) {
  982. if(*str != *p)
  983. return False;
  984. } else {
  985. if(toupper(*str) != toupper(*p))
  986. return False;
  987. }
  988. str++, p++;
  989. break;
  990. }
  991. }
  992. if(!*p && !*str)
  993. return True;
  994. if (!*p && str[0] == '.' && str[1] == 0)
  995. return(True);
  996. if (!*str && *p == '?')
  997. {
  998. while (*p == '?') p++;
  999. return(!*p);
  1000. }
  1001. if(!*str && (*p == '*' && p[1] == '\0'))
  1002. return True;
  1003. return False;
  1004. }
  1005. /*********************************************************
  1006. * Routine to match a given string with a regexp - uses
  1007. * simplified regexp that takes * and ? only. Case can be
  1008. * significant or not.
  1009. * This is the 'original code' used by the unix matcher.
  1010. *********************************************************/
  1011. static BOOL unix_mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
  1012. {
  1013. char *p;
  1014. pstring p1, p2;
  1015. fstring ebase,eext,sbase,sext;
  1016. BOOL matched;
  1017. /* Make local copies of str and regexp */
  1018. StrnCpy(p1,regexp,sizeof(pstring)-1);
  1019. StrnCpy(p2,str,sizeof(pstring)-1);
  1020. if (!strchr(p2,'.')) {
  1021. pstrcat(p2,".");
  1022. }
  1023. /* Remove any *? and ** as they are meaningless */
  1024. for(p = p1; *p; p++)
  1025. while( *p == '*' && (p[1] == '?' ||p[1] == '*'))
  1026. (void)pstrcpy( &p[1], &p[2]);
  1027. if (strequal(p1,"*")) return(True);
  1028. DEBUG(8,("unix_mask_match str=<%s> regexp=<%s>, case_sig = %d\n", p2, p1, case_sig));
  1029. if (trans2) {
  1030. fstrcpy(ebase,p1);
  1031. fstrcpy(sbase,p2);
  1032. } else {
  1033. if ((p=strrchr(p1,'.'))) {
  1034. *p = 0;
  1035. fstrcpy(ebase,p1);
  1036. fstrcpy(eext,p+1);
  1037. } else {
  1038. fstrcpy(ebase,p1);
  1039. eext[0] = 0;
  1040. }
  1041. if (!strequal(p2,".") && !strequal(p2,"..") && (p=strrchr(p2,'.'))) {
  1042. *p = 0;
  1043. fstrcpy(sbase,p2);
  1044. fstrcpy(sext,p+1);
  1045. } else {
  1046. fstrcpy(sbase,p2);
  1047. fstrcpy(sext,"");
  1048. }
  1049. }
  1050. matched = unix_do_match(sbase,ebase,case_sig) &&
  1051. (trans2 || unix_do_match(sext,eext,case_sig));
  1052. DEBUG(8,("unix_mask_match returning %d\n", matched));
  1053. return matched;
  1054. }
  1055. /*********************************************************
  1056. * Recursive routine that is called by mask_match.
  1057. * Does the actual matching. Returns True if matched,
  1058. * False if failed. This is the 'new' NT style matcher.
  1059. *********************************************************/
  1060. BOOL do_match(char *str, char *regexp, int case_sig)
  1061. {
  1062. char *p;
  1063. for( p = regexp; *p && *str; ) {
  1064. switch(*p) {
  1065. case '?':
  1066. str++; p++;
  1067. break;
  1068. case '*':
  1069. /* Look for a character matching
  1070. the one after the '*' */
  1071. p++;
  1072. if(!*p)
  1073. return True; /* Automatic match */
  1074. while(*str) {
  1075. while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
  1076. str++;
  1077. /* Now eat all characters that match, as
  1078. we want the *last* character to match. */
  1079. while(*str && (case_sig ? (*p == *str) : (toupper(*p)==toupper(*str))))
  1080. str++;
  1081. str--; /* We've eaten the match char after the '*' */
  1082. if(do_match(str,p,case_sig)) {
  1083. return True;
  1084. }
  1085. if(!*str) {
  1086. return False;
  1087. } else {
  1088. str++;
  1089. }
  1090. }
  1091. return False;
  1092. default:
  1093. if(case_sig) {
  1094. if(*str != *p) {
  1095. return False;
  1096. }
  1097. } else {
  1098. if(toupper(*str) != toupper(*p)) {
  1099. return False;
  1100. }
  1101. }
  1102. str++, p++;
  1103. break;
  1104. }
  1105. }
  1106. if(!*p && !*str)
  1107. return True;
  1108. if (!*p && str[0] == '.' && str[1] == 0) {
  1109. return(True);
  1110. }
  1111. if (!*str && *p == '?') {
  1112. while (*p == '?')
  1113. p++;
  1114. return(!*p);
  1115. }
  1116. if(!*str && (*p == '*' && p[1] == '\0')) {
  1117. return True;
  1118. }
  1119. return False;
  1120. }
  1121. /*********************************************************
  1122. * Routine to match a given string with a regexp - uses
  1123. * simplified regexp that takes * and ? only. Case can be
  1124. * significant or not.
  1125. * The 8.3 handling was rewritten by Ums Harald <Harald.Ums@pro-sieben.de>
  1126. * This is the new 'NT style' matcher.
  1127. *********************************************************/
  1128. BOOL mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
  1129. {
  1130. char *p;
  1131. pstring t_pattern, t_filename, te_pattern, te_filename;
  1132. fstring ebase,eext,sbase,sext;
  1133. BOOL matched = False;
  1134. /* Make local copies of str and regexp */
  1135. pstrcpy(t_pattern,regexp);
  1136. pstrcpy(t_filename,str);
  1137. if(trans2) {
  1138. /* a special case for 16 bit apps */
  1139. if (strequal(t_pattern,"????????.???"))
  1140. pstrcpy(t_pattern,"*");
  1141. #if 0
  1142. /*
  1143. * Handle broken clients that send us old 8.3 format.
  1144. */
  1145. string_sub(t_pattern,"????????","*");
  1146. string_sub(t_pattern,".???",".*");
  1147. #endif
  1148. }
  1149. #if 0
  1150. /*
  1151. * Not sure if this is a good idea. JRA.
  1152. */
  1153. if(trans2 && is_8_3(t_pattern,False) && is_8_3(t_filename,False))
  1154. trans2 = False;
  1155. #endif
  1156. #if 0
  1157. if (!strchr(t_filename,'.')) {
  1158. pstrcat(t_filename,".");
  1159. }
  1160. #endif
  1161. /* Remove any *? and ** as they are meaningless */
  1162. string_sub(t_pattern, "*?", "*");
  1163. string_sub(t_pattern, "**", "*");
  1164. if (strequal(t_pattern,"*"))
  1165. return(True);
  1166. DEBUG(8,("mask_match str=<%s> regexp=<%s>, case_sig = %d\n", t_filename, t_pattern, case_sig));
  1167. if(trans2) {
  1168. /*
  1169. * Match each component of the regexp, split up by '.'
  1170. * characters.
  1171. */
  1172. char *fp, *rp, *cp2, *cp1;
  1173. BOOL last_wcard_was_star = False;
  1174. int num_path_components, num_regexp_components;
  1175. pstrcpy(te_pattern,t_pattern);
  1176. pstrcpy(te_filename,t_filename);
  1177. /*
  1178. * Remove multiple "*." patterns.
  1179. */
  1180. string_sub(te_pattern, "*.*.", "*.");
  1181. num_regexp_components = count_chars(te_pattern, '.');
  1182. num_path_components = count_chars(te_filename, '.');
  1183. /*
  1184. * Check for special 'hack' case of "DIR a*z". - needs to match a.b.c...z
  1185. */
  1186. if(num_regexp_components == 0)
  1187. matched = do_match( te_filename, te_pattern, case_sig);
  1188. else {
  1189. for( cp1 = te_pattern, cp2 = te_filename; cp1;) {
  1190. fp = strchr(cp2, '.');
  1191. if(fp)
  1192. *fp = '\0';
  1193. rp = strchr(cp1, '.');
  1194. if(rp)
  1195. *rp = '\0';
  1196. if(cp1[strlen(cp1)-1] == '*')
  1197. last_wcard_was_star = True;
  1198. else
  1199. last_wcard_was_star = False;
  1200. if(!do_match(cp2, cp1, case_sig))
  1201. break;
  1202. cp1 = rp ? rp + 1 : NULL;
  1203. cp2 = fp ? fp + 1 : "";
  1204. if(last_wcard_was_star || ((cp1 != NULL) && (*cp1 == '*'))) {
  1205. /* Eat the extra path components. */
  1206. int i;
  1207. for(i = 0; i < num_path_components - num_regexp_components; i++) {
  1208. fp = strchr(cp2, '.');
  1209. if(fp)
  1210. *fp = '\0';
  1211. if((cp1 != NULL) && do_match( cp2, cp1, case_sig)) {
  1212. cp2 = fp ? fp + 1 : "";
  1213. break;
  1214. }
  1215. cp2 = fp ? fp + 1 : "";
  1216. }
  1217. num_path_components -= i;
  1218. }
  1219. }
  1220. if(cp1 == NULL && ((*cp2 == '\0') || last_wcard_was_star))
  1221. matched = True;
  1222. }
  1223. } else {
  1224. /* -------------------------------------------------
  1225. * Behaviour of Win95
  1226. * for 8.3 filenames and 8.3 Wildcards
  1227. * -------------------------------------------------
  1228. */
  1229. if (strequal (t_filename, ".")) {
  1230. /*
  1231. * Patterns: *.* *. ?. ? ????????.??? are valid.
  1232. *
  1233. */
  1234. if(strequal(t_pattern, "*.*") || strequal(t_pattern, "*.") ||
  1235. strequal(t_pattern, "????????.???") ||
  1236. strequal(t_pattern, "?.") || strequal(t_pattern, "?"))
  1237. matched = True;
  1238. } else if (strequal (t_filename, "..")) {
  1239. /*
  1240. * Patterns: *.* *. ?. ? *.? ????????.??? are valid.
  1241. *
  1242. */
  1243. if(strequal(t_pattern, "*.*") || strequal(t_pattern, "*.") ||
  1244. strequal(t_pattern, "?.") || strequal(t_pattern, "?") ||
  1245. strequal(t_pattern, "????????.???") ||
  1246. strequal(t_pattern, "*.?") || strequal(t_pattern, "?.*"))
  1247. matched = True;
  1248. } else {
  1249. if ((p = strrchr (t_pattern, '.'))) {
  1250. /*
  1251. * Wildcard has a suffix.
  1252. */
  1253. *p = 0;
  1254. fstrcpy (ebase, t_pattern);
  1255. if (p[1]) {
  1256. fstrcpy (eext, p + 1);
  1257. } else {
  1258. /* pattern ends in DOT: treat as if there is no DOT */
  1259. *eext = 0;
  1260. if (strequal (ebase, "*"))
  1261. return (True);
  1262. }
  1263. } else {
  1264. /*
  1265. * No suffix for wildcard.
  1266. */
  1267. fstrcpy (ebase, t_pattern);
  1268. eext[0] = 0;
  1269. }
  1270. p = strrchr (t_filename, '.');
  1271. if (p && (p[1] == 0) ) {
  1272. /*
  1273. * Filename has an extension of '.' only.
  1274. */
  1275. *p = 0; /* nuke dot at end of string */
  1276. p = 0; /* and treat it as if there is no extension */
  1277. }
  1278. if (p) {
  1279. /*
  1280. * Filename has an extension.
  1281. */
  1282. *p = 0;
  1283. fstrcpy (sbase, t_filename);
  1284. fstrcpy (sext, p + 1);
  1285. if (*eext) {
  1286. matched = do_match(sbase, ebase, case_sig)
  1287. && do_match(sext, eext, case_sig);
  1288. } else {
  1289. /* pattern has no extension */
  1290. /* Really: match complete filename with pattern ??? means exactly 3 chars */
  1291. matched = do_match(str, ebase, case_sig);
  1292. }
  1293. } else {
  1294. /*
  1295. * Filename has no extension.
  1296. */
  1297. fstrcpy (sbase, t_filename);
  1298. fstrcpy (sext, "");
  1299. if (*eext) {
  1300. /* pattern has extension */
  1301. matched = do_match(sbase, ebase, case_sig)
  1302. && do_match(sext, eext, case_sig);
  1303. } else {
  1304. matched = do_match(sbase, ebase, case_sig);
  1305. #ifdef EMULATE_WEIRD_W95_MATCHING
  1306. /*
  1307. * Even Microsoft has some problems
  1308. * Behaviour Win95 -> local disk
  1309. * is different from Win95 -> smb drive from Nt 4.0
  1310. * This branch would reflect the Win95 local disk behaviour
  1311. */
  1312. if (!matched) {
  1313. /* a? matches aa and a in w95 */
  1314. fstrcat (sbase, ".");
  1315. matched = do_match(sbase, ebase, case_sig);
  1316. }
  1317. #endif
  1318. }
  1319. }
  1320. }
  1321. }
  1322. DEBUG(8,("mask_match returning %d\n", matched));
  1323. return matched;
  1324. }
  1325. #endif /* 0 */
  1326. #if 0
  1327. /****************************************************************************
  1328. set the length of a file from a filedescriptor.
  1329. Returns 0 on success, -1 on failure.
  1330. ****************************************************************************/
  1331. int set_filelen(int fd, SMB_OFF_T len)
  1332. {
  1333. /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
  1334. extend a file with ftruncate. Provide alternate implementation
  1335. for this */
  1336. #ifdef HAVE_FTRUNCATE_EXTEND
  1337. return sys_ftruncate(fd, len);
  1338. #else
  1339. SMB_STRUCT_STAT st;
  1340. char c = 0;
  1341. SMB_OFF_T currpos = sys_lseek(fd, (SMB_OFF_T)0, SEEK_CUR);
  1342. if(currpos == -1)
  1343. return -1;
  1344. /* Do an fstat to see if the file is longer than
  1345. the requested size (call ftruncate),
  1346. or shorter, in which case seek to len - 1 and write 1
  1347. byte of zero */
  1348. if(sys_fstat(fd, &st)<0)
  1349. return -1;
  1350. #ifdef S_ISFIFO
  1351. if (S_ISFIFO(st.st_mode))
  1352. return 0;
  1353. #endif
  1354. if(st.st_size == len)
  1355. return 0;
  1356. if(st.st_size > len)
  1357. return sys_ftruncate(fd, len);
  1358. if(sys_lseek(fd, len-1, SEEK_SET) != len -1)
  1359. return -1;
  1360. if(write(fd, &c, 1)!=1)
  1361. return -1;
  1362. /* Seek to where we were */
  1363. if(sys_lseek(fd, currpos, SEEK_SET) != currpos)
  1364. return -1;
  1365. return 0;
  1366. #endif
  1367. }
  1368. #ifdef HPUX
  1369. /****************************************************************************
  1370. this is a version of setbuffer() for those machines that only have setvbuf
  1371. ****************************************************************************/
  1372. void setbuffer(FILE *f,char *buf,int bufsize)
  1373. {
  1374. setvbuf(f,buf,_IOFBF,bufsize);
  1375. }
  1376. #endif
  1377. #endif /* 0 */
  1378. /****************************************************************************
  1379. expand a pointer to be a particular size
  1380. ****************************************************************************/
  1381. void *Realloc(void *p,size_t size)
  1382. {
  1383. void *ret=NULL;
  1384. if (size == 0) {
  1385. if (p) free(p);
  1386. DEBUG(5,("Realloc asked for 0 bytes\n"));
  1387. return NULL;
  1388. }
  1389. if (!p)
  1390. ret = (void *)malloc(size);
  1391. else
  1392. ret = (void *)realloc(p,size);
  1393. #ifdef MEM_MAN
  1394. {
  1395. extern FILE *dbf;
  1396. smb_mem_write_info(ret, dbf);
  1397. }
  1398. #endif
  1399. if (!ret)
  1400. DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
  1401. return(ret);
  1402. }
  1403. /****************************************************************************
  1404. get my own name and IP
  1405. ****************************************************************************/
  1406. BOOL get_myname(char *my_name,struct in_addr *ip)
  1407. {
  1408. struct hostent *hp;
  1409. pstring hostname;
  1410. *hostname = 0;
  1411. /* get my host name */
  1412. if (gethostname(hostname, sizeof(hostname)) == -1)
  1413. {
  1414. DEBUG(0,("gethostname failed\n"));
  1415. return False;
  1416. }
  1417. /* Ensure null termination. */
  1418. hostname[sizeof(hostname)-1] = '\0';
  1419. /* get host info */
  1420. if ((hp = Get_Hostbyname(hostname)) == 0)
  1421. {
  1422. DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",hostname));
  1423. return False;
  1424. }
  1425. if (my_name)
  1426. {
  1427. /* split off any parts after an initial . */
  1428. char *p = strchr(hostname,'.');
  1429. if (p) *p = 0;
  1430. fstrcpy(my_name,hostname);
  1431. }
  1432. if (ip)
  1433. putip((char *)ip,(char *)hp->h_addr);
  1434. return(True);
  1435. }
  1436. /****************************************************************************
  1437. true if two IP addresses are equal
  1438. ****************************************************************************/
  1439. BOOL ip_equal(struct in_addr ip1,struct in_addr ip2)
  1440. {
  1441. uint32 a1,a2;
  1442. a1 = ntohl(ip1.s_addr);
  1443. a2 = ntohl(ip2.s_addr);
  1444. return(a1 == a2);
  1445. }
  1446. #if 0 /* May be useful one day */
  1447. /****************************************************************************
  1448. interpret a protocol description string, with a default
  1449. ****************************************************************************/
  1450. int interpret_protocol(char *str,int def)
  1451. {
  1452. if (strequal(str,"NT1"))
  1453. return(PROTOCOL_NT1);
  1454. if (strequal(str,"LANMAN2"))
  1455. return(PROTOCOL_LANMAN2);
  1456. if (strequal(str,"LANMAN1"))
  1457. return(PROTOCOL_LANMAN1);
  1458. if (strequal(str,"CORE"))
  1459. return(PROTOCOL_CORE);
  1460. if (strequal(str,"COREPLUS"))
  1461. return(PROTOCOL_COREPLUS);
  1462. if (strequal(str,"CORE+"))
  1463. return(PROTOCOL_COREPLUS);
  1464. DEBUG(0,("Unrecognised protocol level %s\n",str));
  1465. return(def);
  1466. }
  1467. #endif /* 0 */
  1468. /****************************************************************************
  1469. interpret an internet address or name into an IP address in 4 byte form
  1470. ****************************************************************************/
  1471. uint32 interpret_addr(const char *str)
  1472. {
  1473. struct hostent *hp;
  1474. uint32 res;
  1475. int i;
  1476. BOOL pure_address = True;
  1477. if (strcmp(str,"0.0.0.0") == 0) return(0);
  1478. if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
  1479. for (i=0; pure_address && str[i]; i++)
  1480. if (!(isdigit((int)str[i]) || str[i] == '.'))
  1481. pure_address = False;
  1482. /* if it's in the form of an IP address then get the lib to interpret it */
  1483. if (pure_address) {
  1484. res = inet_addr(str);
  1485. } else {
  1486. /* otherwise assume it's a network name of some sort and use
  1487. Get_Hostbyname */
  1488. if ((hp = Get_Hostbyname(str)) == 0) {
  1489. DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str));
  1490. return 0;
  1491. }
  1492. if(hp->h_addr == NULL) {
  1493. DEBUG(3,("Get_Hostbyname: host address is invalid for host %s\n",str));
  1494. return 0;
  1495. }
  1496. putip((char *)&res,(char *)hp->h_addr);
  1497. }
  1498. if (res == (uint32)-1) return(0);
  1499. return(res);
  1500. }
  1501. /*******************************************************************
  1502. a convenient addition to interpret_addr()
  1503. ******************************************************************/
  1504. struct in_addr *interpret_addr2(const char *str)
  1505. {
  1506. static struct in_addr ret;
  1507. uint32 a = interpret_addr(str);
  1508. ret.s_addr = a;
  1509. return(&ret);
  1510. }
  1511. /*******************************************************************
  1512. check if an IP is the 0.0.0.0
  1513. ******************************************************************/
  1514. BOOL zero_ip(struct in_addr ip)
  1515. {
  1516. uint32 a;
  1517. putip((char *)&a,(char *)&ip);
  1518. return(a == 0);
  1519. }
  1520. /*******************************************************************
  1521. matchname - determine if host name matches IP address
  1522. ******************************************************************/
  1523. BOOL matchname(char *remotehost,struct in_addr addr)
  1524. {
  1525. struct hostent *hp;
  1526. int i;
  1527. if ((hp = Get_Hostbyname(remotehost)) == 0) {
  1528. DEBUG(0,("Get_Hostbyname(%s): lookup failure.\n", remotehost));
  1529. return False;
  1530. }
  1531. /*
  1532. * Make sure that gethostbyname() returns the "correct" host name.
  1533. * Unfortunately, gethostbyname("localhost") sometimes yields
  1534. * "localhost.domain". Since the latter host name comes from the
  1535. * local DNS, we just have to trust it (all bets are off if the local
  1536. * DNS is perverted). We always check the address list, though.
  1537. */
  1538. if (strcasecmp(remotehost, hp->h_name)
  1539. && strcasecmp(remotehost, "localhost")) {
  1540. DEBUG(0,("host name/name mismatch: %s != %s\n",
  1541. remotehost, hp->h_name));
  1542. return False;
  1543. }
  1544. /* Look up the host address in the address list we just got. */
  1545. for (i = 0; hp->h_addr_list[i]; i++) {
  1546. if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
  1547. return True;
  1548. }
  1549. /*
  1550. * The host name does not map to the original host address. Perhaps
  1551. * someone has compromised a name server. More likely someone botched
  1552. * it, but that could be dangerous, too.
  1553. */
  1554. DEBUG(0,("host name/address mismatch: %s != %s\n",
  1555. inet_ntoa(addr), hp->h_name));
  1556. return False;
  1557. }
  1558. #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
  1559. /******************************************************************
  1560. Remove any mount options such as -rsize=2048,wsize=2048 etc.
  1561. Based on a fix from <Thomas.Hepper@icem.de>.
  1562. *******************************************************************/
  1563. static void strip_mount_options( pstring *str)
  1564. {
  1565. if (**str == '-')
  1566. {
  1567. char *p = *str;
  1568. while(*p && !isspace(*p))
  1569. p++;
  1570. while(*p && isspace(*p))
  1571. p++;
  1572. if(*p) {
  1573. pstring tmp_str;
  1574. pstrcpy(tmp_str, p);
  1575. pstrcpy(*str, tmp_str);
  1576. }
  1577. }
  1578. }
  1579. /*******************************************************************
  1580. Patch from jkf@soton.ac.uk
  1581. Split Luke's automount_server into YP lookup and string splitter
  1582. so can easily implement automount_path().
  1583. As we may end up doing both, cache the last YP result.
  1584. *******************************************************************/
  1585. #ifdef WITH_NISPLUS_HOME
  1586. static char *automount_lookup(char *user_name)
  1587. {
  1588. static fstring last_key = "";
  1589. static pstring last_value = "";
  1590. char *nis_map = (char *)lp_nis_home_map_name();
  1591. char buffer[NIS_MAXATTRVAL + 1];
  1592. nis_result *result;
  1593. nis_object *object;
  1594. entry_obj *entry;
  1595. DEBUG(5, ("NIS+ Domain: %s\n", (char *)nis_local_directory()));
  1596. if (strcmp(user_name, last_key))
  1597. {
  1598. slprintf(buffer, sizeof(buffer)-1, "[%s=%s]%s.%s", "key", user_name, nis_map,
  1599. (char *)nis_local_directory());
  1600. DEBUG(5, ("NIS+ querystring: %s\n", buffer));
  1601. if (result = nis_list(buffer, RETURN_RESULT, NULL, NULL))
  1602. {
  1603. if (result->status != NIS_SUCCESS)
  1604. {
  1605. DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
  1606. fstrcpy(last_key, ""); pstrcpy(last_value, "");
  1607. }
  1608. else
  1609. {
  1610. object = result->objects.objects_val;
  1611. if (object->zo_data.zo_type == ENTRY_OBJ)
  1612. {
  1613. entry = &object->zo_data.objdata_u.en_data;
  1614. DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
  1615. DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
  1616. pstrcpy(last_value, entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
  1617. string_sub(last_value, "&", user_name);
  1618. fstrcpy(last_key, user_name);
  1619. }
  1620. }
  1621. }
  1622. nis_freeresult(result);
  1623. }
  1624. strip_mount_options(&last_value);
  1625. DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value));
  1626. return last_value;
  1627. }
  1628. #else /* WITH_NISPLUS_HOME */
  1629. static char *automount_lookup(char *user_name)
  1630. {
  1631. static fstring last_key = "";
  1632. static pstring last_value = "";
  1633. int nis_error; /* returned by yp all functions */
  1634. char *nis_result; /* yp_match inits this */
  1635. int nis_result_len; /* and set this */
  1636. char *nis_domain; /* yp_get_default_domain inits this */
  1637. char *nis_map = (char *)lp_nis_home_map_name();
  1638. if ((nis_error = yp_get_default_domain(&nis_domain)) != 0)
  1639. {
  1640. DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
  1641. return last_value;
  1642. }
  1643. DEBUG(5, ("NIS Domain: %s\n", nis_domain));
  1644. if (!strcmp(user_name, last_key))
  1645. {
  1646. nis_result = last_value;
  1647. nis_result_len = strlen(last_value);
  1648. nis_error = 0;
  1649. }
  1650. else
  1651. {
  1652. if ((nis_error = yp_match(nis_domain, nis_map,
  1653. user_name, strlen(user_name),
  1654. &nis_result, &nis_result_len)) != 0)
  1655. {
  1656. DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
  1657. yperr_string(nis_error), user_name, nis_map));
  1658. }
  1659. if (!nis_error && nis_result_len >= sizeof(pstring))
  1660. {
  1661. nis_result_len = sizeof(pstring)-1;
  1662. }
  1663. fstrcpy(last_key, user_name);
  1664. strncpy(last_value, nis_result, nis_result_len);
  1665. last_value[nis_result_len] = '\0';
  1666. }
  1667. strip_mount_options(&last_value);
  1668. DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, last_value));
  1669. return last_value;
  1670. }
  1671. #endif /* WITH_NISPLUS_HOME */
  1672. #endif
  1673. /*******************************************************************
  1674. Patch from jkf@soton.ac.uk
  1675. This is Luke's original function with the NIS lookup code
  1676. moved out to a separate function.
  1677. *******************************************************************/
  1678. static char *automount_server(const char *user_name)
  1679. {
  1680. static pstring server_name;
  1681. (void) user_name;
  1682. /* use the local machine name as the default */
  1683. /* this will be the default if WITH_AUTOMOUNT is not used or fails */
  1684. pstrcpy(server_name, local_machine);
  1685. #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
  1686. if (lp_nis_home_map())
  1687. {
  1688. int home_server_len;
  1689. char *automount_value = automount_lookup(user_name);
  1690. home_server_len = strcspn(automount_value,":");
  1691. DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
  1692. if (home_server_len > sizeof(pstring))
  1693. {
  1694. home_server_len = sizeof(pstring);
  1695. }
  1696. strncpy(server_name, automount_value, home_server_len);
  1697. server_name[home_server_len] = '\0';
  1698. }
  1699. #endif
  1700. DEBUG(4,("Home server: %s\n", server_name));
  1701. return server_name;
  1702. }
  1703. /*******************************************************************
  1704. Patch from jkf@soton.ac.uk
  1705. Added this to implement %p (NIS auto-map version of %H)
  1706. *******************************************************************/
  1707. static char *automount_path(char *user_name)
  1708. {
  1709. static pstring server_path;
  1710. /* use the passwd entry as the default */
  1711. /* this will be the default if WITH_AUTOMOUNT is not used or fails */
  1712. /* pstrcpy() copes with get_home_dir() returning NULL */
  1713. pstrcpy(server_path, get_home_dir(user_name));
  1714. #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
  1715. if (lp_nis_home_map())
  1716. {
  1717. char *home_path_start;
  1718. char *automount_value = automount_lookup(user_name);
  1719. home_path_start = strchr(automount_value,':');
  1720. if (home_path_start != NULL)
  1721. {
  1722. DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
  1723. home_path_start?(home_path_start+1):""));
  1724. pstrcpy(server_path, home_path_start+1);
  1725. }
  1726. }
  1727. #endif
  1728. DEBUG(4,("Home server path: %s\n", server_path));
  1729. return server_path;
  1730. }
  1731. /*******************************************************************
  1732. sub strings with useful parameters
  1733. Rewritten by Stefaan A Eeckels <Stefaan.Eeckels@ecc.lu> and
  1734. Paul Rippin <pr3245@nopc.eurostat.cec.be>
  1735. ********************************************************************/
  1736. void standard_sub_basic(char *str)
  1737. {
  1738. char *s, *p;
  1739. char pidstr[10];
  1740. struct passwd *pass;
  1741. const char *username = sam_logon_in_ssb ? samlogon_user : sesssetup_user;
  1742. for (s = str ; s && *s && (p = strchr(s,'%')); s = p )
  1743. {
  1744. switch (*(p+1))
  1745. {
  1746. case 'G' :
  1747. {
  1748. if ((pass = Get_Pwnam(username))!=NULL)
  1749. {
  1750. string_sub(p,"%G",gidtoname(pass->pw_gid));
  1751. }
  1752. else
  1753. {
  1754. p += 2;
  1755. }
  1756. break;
  1757. }
  1758. case 'N' : string_sub(p,"%N", automount_server(username)); break;
  1759. case 'I' : string_sub(p,"%I", client_addr(Client)); break;
  1760. case 'L' : string_sub(p,"%L", local_machine); break;
  1761. case 'M' : string_sub(p,"%M", client_name(Client)); break;
  1762. case 'R' : string_sub(p,"%R", remote_proto); break;
  1763. case 'T' : string_sub(p,"%T", timestring()); break;
  1764. case 'U' : string_sub(p,"%U", username); break;
  1765. case 'a' : string_sub(p,"%a", remote_arch); break;
  1766. case 'd' :
  1767. {
  1768. slprintf(pidstr,sizeof(pidstr) - 1, "%d",(int)getpid());
  1769. string_sub(p,"%d", pidstr);
  1770. break;
  1771. }
  1772. case 'h' : string_sub(p,"%h", myhostname); break;
  1773. case 'm' : string_sub(p,"%m", remote_machine); break;
  1774. case 'v' : string_sub(p,"%v", VERSION); break;
  1775. case '$' : /* Expand environment variables */
  1776. {
  1777. /* Contributed by Branko Cibej <branko.cibej@hermes.si> */
  1778. fstring envname;
  1779. char *envval;
  1780. char *q, *r;
  1781. int copylen;
  1782. if (*(p+2) != '(')
  1783. {
  1784. p+=2;
  1785. break;
  1786. }
  1787. if ((q = strchr(p,')')) == NULL)
  1788. {
  1789. DEBUG(0,("standard_sub_basic: Unterminated environment \
  1790. variable [%s]\n", p));
  1791. p+=2;
  1792. break;
  1793. }
  1794. r = p+3;
  1795. copylen = MIN((size_t)(q-r),(size_t)(sizeof(envname)-1));
  1796. strncpy(envname,r,copylen);
  1797. envname[copylen] = '\0';
  1798. if ((envval = getenv(envname)) == NULL)
  1799. {
  1800. DEBUG(0,("standard_sub_basic: Environment variable [%s] not set\n",
  1801. envname));
  1802. p+=2;
  1803. break;
  1804. }
  1805. copylen = MIN((size_t)(q+1-p),(size_t)(sizeof(envname)-1));
  1806. strncpy(envname,p,copylen);
  1807. envname[copylen] = '\0';
  1808. string_sub(p,envname,envval);
  1809. break;
  1810. }
  1811. case '\0': p++; break; /* don't run off end if last character is % */
  1812. default : p+=2; break;
  1813. }
  1814. }
  1815. return;
  1816. }
  1817. /****************************************************************************
  1818. do some standard substitutions in a string
  1819. ****************************************************************************/
  1820. void standard_sub(connection_struct *conn,char *str)
  1821. {
  1822. char *p, *s;
  1823. const char *home;
  1824. for (s=str; (p=strchr(s, '%'));s=p) {
  1825. switch (*(p+1)) {
  1826. case 'H':
  1827. if ((home = get_home_dir(conn->user))) {
  1828. string_sub(p,"%H",home);
  1829. } else {
  1830. p += 2;
  1831. }
  1832. break;
  1833. case 'P':
  1834. string_sub(p,"%P",conn->connectpath);
  1835. break;
  1836. case 'S':
  1837. string_sub(p,"%S",
  1838. lp_servicename(SNUM(conn)));
  1839. break;
  1840. case 'g':
  1841. string_sub(p,"%g",
  1842. gidtoname(conn->gid));
  1843. break;
  1844. case 'u':
  1845. string_sub(p,"%u",conn->user);
  1846. break;
  1847. /* Patch from jkf@soton.ac.uk Left the %N (NIS
  1848. * server name) in standard_sub_basic as it is
  1849. * a feature for logon servers, hence uses the
  1850. * username. The %p (NIS server path) code is
  1851. * here as it is used instead of the default
  1852. * "path =" string in [homes] and so needs the
  1853. * service name, not the username. */
  1854. case 'p':
  1855. string_sub(p,"%p",
  1856. automount_path(lp_servicename(SNUM(conn))));
  1857. break;
  1858. case '\0':
  1859. p++;
  1860. break; /* don't run off the end of the string
  1861. */
  1862. default: p+=2;
  1863. break;
  1864. }
  1865. }
  1866. standard_sub_basic(str);
  1867. }
  1868. /*******************************************************************
  1869. are two IPs on the same subnet?
  1870. ********************************************************************/
  1871. BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
  1872. {
  1873. uint32 net1,net2,nmask;
  1874. nmask = ntohl(mask.s_addr);
  1875. net1 = ntohl(ip1.s_addr);
  1876. net2 = ntohl(ip2.s_addr);
  1877. return((net1 & nmask) == (net2 & nmask));
  1878. }
  1879. /****************************************************************************
  1880. a wrapper for gethostbyname() that tries with all lower and all upper case
  1881. if the initial name fails
  1882. ****************************************************************************/
  1883. struct hostent *Get_Hostbyname(const char *name)
  1884. {
  1885. char *name2 = strdup(name);
  1886. struct hostent *ret;
  1887. if (!name2)
  1888. {
  1889. DEBUG(0,("Memory allocation error in Get_Hostbyname! panic\n"));
  1890. exit(0);
  1891. }
  1892. /*
  1893. * This next test is redundent and causes some systems (with
  1894. * broken isalnum() calls) problems.
  1895. * JRA.
  1896. */
  1897. #if 0
  1898. if (!isalnum(*name2))
  1899. {
  1900. free(name2);
  1901. return(NULL);
  1902. }
  1903. #endif /* 0 */
  1904. ret = sys_gethostbyname(name2);
  1905. if (ret != NULL)
  1906. {
  1907. free(name2);
  1908. return(ret);
  1909. }
  1910. /* try with all lowercase */
  1911. strlower(name2);
  1912. ret = sys_gethostbyname(name2);
  1913. if (ret != NULL)
  1914. {
  1915. free(name2);
  1916. return(ret);
  1917. }
  1918. /* try with all uppercase */
  1919. strupper(name2);
  1920. ret = sys_gethostbyname(name2);
  1921. if (ret != NULL)
  1922. {
  1923. free(name2);
  1924. return(ret);
  1925. }
  1926. /* nothing works :-( */
  1927. free(name2);
  1928. return(NULL);
  1929. }
  1930. /*******************************************************************
  1931. turn a uid into a user name
  1932. ********************************************************************/
  1933. char *uidtoname(uid_t uid)
  1934. {
  1935. static char name[40];
  1936. struct passwd *pass = getpwuid(uid);
  1937. if (pass) return(pass->pw_name);
  1938. slprintf(name, sizeof(name) - 1, "%d",(int)uid);
  1939. return(name);
  1940. }
  1941. /*******************************************************************
  1942. turn a gid into a group name
  1943. ********************************************************************/
  1944. char *gidtoname(gid_t gid)
  1945. {
  1946. static char name[40];
  1947. struct group *grp = getgrgid(gid);
  1948. if (grp) return(grp->gr_name);
  1949. slprintf(name,sizeof(name) - 1, "%d",(int)gid);
  1950. return(name);
  1951. }
  1952. /*******************************************************************
  1953. turn a user name into a uid
  1954. ********************************************************************/
  1955. uid_t nametouid(const char *name)
  1956. {
  1957. struct passwd *pass = getpwnam(name);
  1958. if (pass) return(pass->pw_uid);
  1959. return (uid_t)-1;
  1960. }
  1961. /*******************************************************************
  1962. something really nasty happened - panic!
  1963. ********************************************************************/
  1964. void smb_panic(const char *why)
  1965. {
  1966. const char *cmd = lp_panic_action();
  1967. if (cmd && *cmd) {
  1968. system(cmd);
  1969. }
  1970. DEBUG(0,("PANIC: %s\n", why));
  1971. dbgflush();
  1972. abort();
  1973. }
  1974. #if 0
  1975. /*******************************************************************
  1976. a readdir wrapper which just returns the file name
  1977. ********************************************************************/
  1978. char *readdirname(DIR *p)
  1979. {
  1980. SMB_STRUCT_DIRENT *ptr;
  1981. char *dname;
  1982. if (!p) return(NULL);
  1983. ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
  1984. if (!ptr) return(NULL);
  1985. dname = ptr->d_name;
  1986. #ifdef NEXT2
  1987. if (telldir(p) < 0) return(NULL);
  1988. #endif
  1989. #ifdef HAVE_BROKEN_READDIR
  1990. /* using /usr/ucb/cc is BAD */
  1991. dname = dname - 2;
  1992. #endif
  1993. {
  1994. static pstring buf;
  1995. memcpy(buf, dname, NAMLEN(ptr)+1);
  1996. dname = buf;
  1997. }
  1998. return(dname);
  1999. }
  2000. /*******************************************************************
  2001. Utility function used to decide if the last component
  2002. of a path matches a (possibly wildcarded) entry in a namelist.
  2003. ********************************************************************/
  2004. BOOL is_in_path(char *name, name_compare_entry *namelist)
  2005. {
  2006. pstring last_component;
  2007. char *p;
  2008. DEBUG(8, ("is_in_path: %s\n", name));
  2009. /* if we have no list it's obviously not in the path */
  2010. if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL)))
  2011. {
  2012. DEBUG(8,("is_in_path: no name list.\n"));
  2013. return False;
  2014. }
  2015. /* Get the last component of the unix name. */
  2016. p = strrchr(name, '/');
  2017. strncpy(last_component, p ? ++p : name, sizeof(last_component)-1);
  2018. last_component[sizeof(last_component)-1] = '\0';
  2019. for(; namelist->name != NULL; namelist++)
  2020. {
  2021. if(namelist->is_wild)
  2022. {
  2023. /*
  2024. * Look for a wildcard match. Use the old
  2025. * 'unix style' mask match, rather than the
  2026. * new NT one.
  2027. */
  2028. if (unix_mask_match(last_component, namelist->name, case_sensitive, False))
  2029. {
  2030. DEBUG(8,("is_in_path: mask match succeeded\n"));
  2031. return True;
  2032. }
  2033. }
  2034. else
  2035. {
  2036. if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
  2037. (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
  2038. {
  2039. DEBUG(8,("is_in_path: match succeeded\n"));
  2040. return True;
  2041. }
  2042. }
  2043. }
  2044. DEBUG(8,("is_in_path: match not found\n"));
  2045. return False;
  2046. }
  2047. /*******************************************************************
  2048. Strip a '/' separated list into an array of
  2049. name_compare_enties structures suitable for
  2050. passing to is_in_path(). We do this for
  2051. speed so we can pre-parse all the names in the list
  2052. and don't do it for each call to is_in_path().
  2053. namelist is modified here and is assumed to be
  2054. a copy owned by the caller.
  2055. We also check if the entry contains a wildcard to
  2056. remove a potentially expensive call to mask_match
  2057. if possible.
  2058. ********************************************************************/
  2059. void set_namearray(name_compare_entry **ppname_array, char *namelist)
  2060. {
  2061. char *name_end;
  2062. char *nameptr = namelist;
  2063. int num_entries = 0;
  2064. int i;
  2065. (*ppname_array) = NULL;
  2066. if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0')))
  2067. return;
  2068. /* We need to make two passes over the string. The
  2069. first to count the number of elements, the second
  2070. to split it.
  2071. */
  2072. while(*nameptr)
  2073. {
  2074. if ( *nameptr == '/' )
  2075. {
  2076. /* cope with multiple (useless) /s) */
  2077. nameptr++;
  2078. continue;
  2079. }
  2080. /* find the next / */
  2081. name_end = strchr(nameptr, '/');
  2082. /* oops - the last check for a / didn't find one. */
  2083. if (name_end == NULL)
  2084. break;
  2085. /* next segment please */
  2086. nameptr = name_end + 1;
  2087. num_entries++;
  2088. }
  2089. if(num_entries == 0)
  2090. return;
  2091. if(( (*ppname_array) = (name_compare_entry *)malloc(
  2092. (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
  2093. {
  2094. DEBUG(0,("set_namearray: malloc fail\n"));
  2095. return;
  2096. }
  2097. /* Now copy out the names */
  2098. nameptr = namelist;
  2099. i = 0;
  2100. while(*nameptr)
  2101. {
  2102. if ( *nameptr == '/' )
  2103. {
  2104. /* cope with multiple (useless) /s) */
  2105. nameptr++;
  2106. continue;
  2107. }
  2108. /* find the next / */
  2109. if ((name_end = strchr(nameptr, '/')) != NULL)
  2110. {
  2111. *name_end = 0;
  2112. }
  2113. /* oops - the last check for a / didn't find one. */
  2114. if(name_end == NULL)
  2115. break;
  2116. (*ppname_array)[i].is_wild = ((strchr( nameptr, '?')!=NULL) ||
  2117. (strchr( nameptr, '*')!=NULL));
  2118. if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
  2119. {
  2120. DEBUG(0,("set_namearray: malloc fail (1)\n"));
  2121. return;
  2122. }
  2123. /* next segment please */
  2124. nameptr = name_end + 1;
  2125. i++;
  2126. }
  2127. (*ppname_array)[i].name = NULL;
  2128. return;
  2129. }
  2130. /****************************************************************************
  2131. routine to free a namearray.
  2132. ****************************************************************************/
  2133. void free_namearray(name_compare_entry *name_array)
  2134. {
  2135. if(name_array == 0)
  2136. return;
  2137. if(name_array->name != NULL)
  2138. free(name_array->name);
  2139. free((char *)name_array);
  2140. }
  2141. /****************************************************************************
  2142. routine to do file locking
  2143. ****************************************************************************/
  2144. BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
  2145. {
  2146. #ifdef HAVE_FCNTL_LOCK
  2147. SMB_STRUCT_FLOCK lock;
  2148. int ret;
  2149. if(lp_ole_locking_compat()) {
  2150. SMB_OFF_T mask2= ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
  2151. SMB_OFF_T mask = (mask2<<2);
  2152. /* make sure the count is reasonable, we might kill the lockd otherwise */
  2153. count &= ~mask;
  2154. /* the offset is often strange - remove 2 of its bits if either of
  2155. the top two bits are set. Shift the top ones by two bits. This
  2156. still allows OLE2 apps to operate, but should stop lockd from
  2157. dieing */
  2158. if ((offset & mask) != 0)
  2159. offset = (offset & ~mask) | (((offset & mask) >> 2) & mask2);
  2160. } else {
  2161. SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
  2162. SMB_OFF_T mask = (mask2<<1);
  2163. SMB_OFF_T neg_mask = ~mask;
  2164. /* interpret negative counts as large numbers */
  2165. if (count < 0)
  2166. count &= ~mask;
  2167. /* no negative offsets */
  2168. if(offset < 0)
  2169. offset &= ~mask;
  2170. /* count + offset must be in range */
  2171. while ((offset < 0 || (offset + count < 0)) && mask)
  2172. {
  2173. offset &= ~mask;
  2174. mask = ((mask >> 1) & neg_mask);
  2175. }
  2176. }
  2177. DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd,op,(double)offset,(double)count,type));
  2178. lock.l_type = type;
  2179. lock.l_whence = SEEK_SET;
  2180. lock.l_start = offset;
  2181. lock.l_len = count;
  2182. lock.l_pid = 0;
  2183. errno = 0;
  2184. ret = fcntl(fd,op,&lock);
  2185. if (errno == EFBIG)
  2186. {
  2187. if( DEBUGLVL( 0 ))
  2188. {
  2189. dbgtext("fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n", (double)offset,(double)count);
  2190. dbgtext("a 'file too large' error. This can happen when using 64 bit lock offsets\n");
  2191. dbgtext("on 32 bit NFS mounted file systems. Retrying with 32 bit truncated length.\n");
  2192. }
  2193. /* 32 bit NFS file system, retry with smaller offset */
  2194. errno = 0;
  2195. lock.l_len = count & 0xffffffff;
  2196. ret = fcntl(fd,op,&lock);
  2197. }
  2198. if (errno != 0)
  2199. DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
  2200. /* a lock query */
  2201. if (op == SMB_F_GETLK)
  2202. {
  2203. if ((ret != -1) &&
  2204. (lock.l_type != F_UNLCK) &&
  2205. (lock.l_pid != 0) &&
  2206. (lock.l_pid != getpid()))
  2207. {
  2208. DEBUG(3,("fd %d is locked by pid %d\n",fd,(int)lock.l_pid));
  2209. return(True);
  2210. }
  2211. /* it must be not locked or locked by me */
  2212. return(False);
  2213. }
  2214. /* a lock set or unset */
  2215. if (ret == -1)
  2216. {
  2217. DEBUG(3,("lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
  2218. (double)offset,(double)count,op,type,strerror(errno)));
  2219. /* perhaps it doesn't support this sort of locking?? */
  2220. if (errno == EINVAL)
  2221. {
  2222. DEBUG(3,("locking not supported? returning True\n"));
  2223. return(True);
  2224. }
  2225. return(False);
  2226. }
  2227. /* everything went OK */
  2228. DEBUG(8,("Lock call successful\n"));
  2229. return(True);
  2230. #else
  2231. return(False);
  2232. #endif
  2233. }
  2234. /*******************************************************************
  2235. is the name specified one of my netbios names
  2236. returns true is it is equal, false otherwise
  2237. ********************************************************************/
  2238. BOOL is_myname(char *s)
  2239. {
  2240. int n;
  2241. BOOL ret = False;
  2242. for (n=0; my_netbios_names[n]; n++) {
  2243. if (strequal(my_netbios_names[n], s))
  2244. ret=True;
  2245. }
  2246. DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
  2247. return(ret);
  2248. }
  2249. #endif /* 0 */
  2250. #if 0 /* Can be useful one day */
  2251. /*******************************************************************
  2252. set the horrid remote_arch string based on an enum.
  2253. ********************************************************************/
  2254. void set_remote_arch(enum remote_arch_types type)
  2255. {
  2256. ra_type = type;
  2257. switch( type )
  2258. {
  2259. case RA_WFWG:
  2260. remote_arch = "WfWg";
  2261. return;
  2262. case RA_OS2:
  2263. remote_arch = "OS2";
  2264. return;
  2265. case RA_WIN95:
  2266. remote_arch = "Win95";
  2267. return;
  2268. case RA_WINNT:
  2269. remote_arch = "WinNT";
  2270. return;
  2271. case RA_SAMBA:
  2272. remote_arch = "Samba";
  2273. return;
  2274. default:
  2275. ra_type = RA_UNKNOWN;
  2276. remote_arch = "UNKNOWN";
  2277. break;
  2278. }
  2279. }
  2280. /*******************************************************************
  2281. Get the remote_arch type.
  2282. ********************************************************************/
  2283. enum remote_arch_types get_remote_arch(void)
  2284. {
  2285. return ra_type;
  2286. }
  2287. #endif /* 0 */
  2288. #if 0
  2289. /*******************************************************************
  2290. align a pointer to a multiple of 2 bytes
  2291. ********************************************************************/
  2292. char *align2(char *q, char *base)
  2293. {
  2294. if ((q - base) & 1)
  2295. {
  2296. q++;
  2297. }
  2298. return q;
  2299. }
  2300. void out_ascii(FILE *f, unsigned char *buf,int len)
  2301. {
  2302. int i;
  2303. for (i=0;i<len;i++)
  2304. {
  2305. fprintf(f, "%c", isprint(buf[i])?buf[i]:'.');
  2306. }
  2307. }
  2308. void out_data(FILE *f,char *buf1,int len, int per_line)
  2309. {
  2310. unsigned char *buf = (unsigned char *)buf1;
  2311. int i=0;
  2312. if (len<=0)
  2313. {
  2314. return;
  2315. }
  2316. fprintf(f, "[%03X] ",i);
  2317. for (i=0;i<len;)
  2318. {
  2319. fprintf(f, "%02X ",(int)buf[i]);
  2320. i++;
  2321. if (i%(per_line/2) == 0) fprintf(f, " ");
  2322. if (i%per_line == 0)
  2323. {
  2324. out_ascii(f,&buf[i-per_line ],per_line/2); fprintf(f, " ");
  2325. out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n");
  2326. if (i<len) fprintf(f, "[%03X] ",i);
  2327. }
  2328. }
  2329. if ((i%per_line) != 0)
  2330. {
  2331. int n;
  2332. n = per_line - (i%per_line);
  2333. fprintf(f, " ");
  2334. if (n>(per_line/2)) fprintf(f, " ");
  2335. while (n--)
  2336. {
  2337. fprintf(f, " ");
  2338. }
  2339. n = MIN(per_line/2,i%per_line);
  2340. out_ascii(f,&buf[i-(i%per_line)],n); fprintf(f, " ");
  2341. n = (i%per_line) - n;
  2342. if (n>0) out_ascii(f,&buf[i-n],n);
  2343. fprintf(f, "\n");
  2344. }
  2345. }
  2346. #endif /* 0 */
  2347. void print_asc(int level, unsigned char *buf,int len)
  2348. {
  2349. int i;
  2350. for (i=0;i<len;i++)
  2351. DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
  2352. }
  2353. void dump_data(int level,char *buf1,int len)
  2354. {
  2355. unsigned char *buf = (unsigned char *)buf1;
  2356. int i=0;
  2357. if (len<=0) return;
  2358. DEBUG(level,("[%03X] ",i));
  2359. for (i=0;i<len;) {
  2360. DEBUG(level,("%02X ",(int)buf[i]));
  2361. i++;
  2362. if (i%8 == 0) DEBUG(level,(" "));
  2363. if (i%16 == 0) {
  2364. print_asc(level,&buf[i-16],8); DEBUG(level,(" "));
  2365. print_asc(level,&buf[i-8],8); DEBUG(level,("\n"));
  2366. if (i<len) DEBUG(level,("[%03X] ",i));
  2367. }
  2368. }
  2369. if (i%16) {
  2370. int n;
  2371. n = 16 - (i%16);
  2372. DEBUG(level,(" "));
  2373. if (n>8) DEBUG(level,(" "));
  2374. while (n--) DEBUG(level,(" "));
  2375. n = MIN(8,i%16);
  2376. print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" "));
  2377. n = (i%16) - n;
  2378. if (n>0) print_asc(level,&buf[i-n],n);
  2379. DEBUG(level,("\n"));
  2380. }
  2381. }
  2382. /*****************************************************************************
  2383. * Provide a checksum on a string
  2384. *
  2385. * Input: s - the null-terminated character string for which the checksum
  2386. * will be calculated.
  2387. *
  2388. * Output: The checksum value calculated for s.
  2389. *
  2390. * ****************************************************************************
  2391. */
  2392. int str_checksum(const char *s)
  2393. {
  2394. int res = 0;
  2395. int c;
  2396. int i=0;
  2397. while(*s) {
  2398. c = *s;
  2399. res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
  2400. s++;
  2401. i++;
  2402. }
  2403. return(res);
  2404. } /* str_checksum */
  2405. #if 0
  2406. /*****************************************************************
  2407. zero a memory area then free it. Used to catch bugs faster
  2408. *****************************************************************/
  2409. void zero_free(void *p, size_t size)
  2410. {
  2411. memset(p, 0, size);
  2412. free(p);
  2413. }
  2414. /*****************************************************************
  2415. set our open file limit to a requested max and return the limit
  2416. *****************************************************************/
  2417. int set_maxfiles(int requested_max)
  2418. {
  2419. #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
  2420. struct rlimit rlp;
  2421. int saved_current_limit;
  2422. if(getrlimit(RLIMIT_NOFILE, &rlp)) {
  2423. DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
  2424. strerror(errno) ));
  2425. /* just guess... */
  2426. return requested_max;
  2427. }
  2428. /*
  2429. * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
  2430. * account for the extra fd we need
  2431. * as well as the log files and standard
  2432. * handles etc. Save the limit we want to set in case
  2433. * we are running on an OS that doesn't support this limit (AIX)
  2434. * which always returns RLIM_INFINITY for rlp.rlim_max.
  2435. */
  2436. saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
  2437. if(setrlimit(RLIMIT_NOFILE, &rlp)) {
  2438. DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n",
  2439. (int)rlp.rlim_cur, strerror(errno) ));
  2440. /* just guess... */
  2441. return saved_current_limit;
  2442. }
  2443. if(getrlimit(RLIMIT_NOFILE, &rlp)) {
  2444. DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
  2445. strerror(errno) ));
  2446. /* just guess... */
  2447. return saved_current_limit;
  2448. }
  2449. #if defined(RLIM_INFINITY)
  2450. if(rlp.rlim_cur == RLIM_INFINITY)
  2451. return saved_current_limit;
  2452. #endif
  2453. if((int)rlp.rlim_cur > saved_current_limit)
  2454. return saved_current_limit;
  2455. return rlp.rlim_cur;
  2456. #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
  2457. /*
  2458. * No way to know - just guess...
  2459. */
  2460. return requested_max;
  2461. #endif
  2462. }
  2463. #endif /* 0 */