2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
24 /***************************************************************
25 Signal function to tell us we timed out.
26 ****************************************************************/
28 static void gotalarm_sig(void)
33 /***************************************************************
34 Lock or unlock a fd for a known lock type. Abandon after waitsecs
36 ****************************************************************/
38 BOOL
do_file_lock(int fd
, int waitsecs
, int type
)
40 SMB_STRUCT_FLOCK lock
;
44 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
47 lock
.l_whence
= SEEK_SET
;
53 /* Note we must *NOT* use sys_fcntl here ! JRA */
54 ret
= fcntl(fd
, SMB_F_SETLKW
, &lock
);
56 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
59 DEBUG(0, ("do_file_lock: failed to %s file.\n",
60 type
== F_UNLCK
? "unlock" : "lock"));
68 /***************************************************************
69 Lock an fd. Abandon after waitsecs seconds.
70 ****************************************************************/
72 BOOL
file_lock(int fd
, int type
, int secs
, int *plock_depth
)
79 if ((*plock_depth
) == 0)
81 if (!do_file_lock(fd
, secs
, type
)) {
82 DEBUG(10,("file_lock: locking file failed, error = %s.\n",
91 /***************************************************************
92 Unlock an fd. Abandon after waitsecs seconds.
93 ****************************************************************/
95 BOOL
file_unlock(int fd
, int *plock_depth
)
100 ret
= do_file_lock(fd
, 5, F_UNLCK
);
105 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n",
110 /***************************************************************
111 locks a file for enumeration / modification.
112 update to be set = True if modification is required.
113 ****************************************************************/
115 void *startfilepwent(char *pfile
, char *s_readbuf
, int bufsize
,
116 int *file_lock_depth
, BOOL update
)
122 DEBUG(0, ("startfilepwent: No file set\n"));
125 DEBUG(10, ("startfilepwent: opening file %s\n", pfile
));
127 fp
= sys_fopen(pfile
, update
? "r+b" : "rb");
130 DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile
));
134 /* Set a buffer to do more efficient reads */
135 setvbuf(fp
, s_readbuf
, _IOFBF
, bufsize
);
137 if (!file_lock(fileno(fp
), (update
? F_WRLCK
: F_RDLCK
), 5, file_lock_depth
))
139 DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile
));
144 /* Make sure it is only rw by the owner */
147 /* We have a lock on the file. */
151 /***************************************************************
152 End enumeration of the file.
153 ****************************************************************/
154 void endfilepwent(void *vp
, int *file_lock_depth
)
156 FILE *fp
= (FILE *)vp
;
158 file_unlock(fileno(fp
), file_lock_depth
);
160 DEBUG(7, ("endfilepwent: closed file.\n"));
163 /*************************************************************************
164 Return the current position in the file list as an SMB_BIG_UINT.
165 This must be treated as an opaque token.
166 *************************************************************************/
167 SMB_BIG_UINT
getfilepwpos(void *vp
)
169 return (SMB_BIG_UINT
)sys_ftell((FILE *)vp
);
172 /*************************************************************************
173 Set the current position in the file list from an SMB_BIG_UINT.
174 This must be treated as an opaque token.
175 *************************************************************************/
176 BOOL
setfilepwpos(void *vp
, SMB_BIG_UINT tok
)
178 return !sys_fseek((FILE *)vp
, (SMB_OFF_T
)tok
, SEEK_SET
);
181 /*************************************************************************
182 gets a line out of a file.
183 line is of format "xxxx:xxxxxx:xxxxx:".
184 lines with "#" at the front are ignored.
185 *************************************************************************/
186 int getfileline(void *vp
, char *linebuf
, int linebuf_size
)
188 /* Static buffers we will return. */
189 FILE *fp
= (FILE *)vp
;
196 DEBUG(0,("getfileline: Bad file pointer.\n"));
201 * Scan the file, a line at a time.
207 fgets(linebuf
, linebuf_size
, fp
);
214 * Check if the string is terminated with a newline - if not
215 * then we must keep reading and discard until we get one.
218 linebuf_len
= strlen(linebuf
);
219 if (linebuf_len
== 0)
225 if (linebuf
[linebuf_len
- 1] != '\n')
228 while (!ferror(fp
) && !feof(fp
))
239 linebuf
[linebuf_len
- 1] = '\0';
242 #ifdef DEBUG_PASSWORD
243 DEBUG(100, ("getfileline: got line |%s|\n", linebuf
));
245 if ((linebuf
[0] == 0) && feof(fp
))
247 DEBUG(4, ("getfileline: end of file reached\n"));
251 if (linebuf
[0] == '#' || linebuf
[0] == '\0')
253 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
257 p
= (unsigned char *) strchr(linebuf
, ':');
260 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
269 /****************************************************************************
270 read a line from a file with possible \ continuation chars.
271 Blanks at the start or end of a line are stripped.
272 The string will be allocated if s2 is NULL
273 ****************************************************************************/
274 char *fgets_slash(char *s2
,int maxlen
,FILE *f
)
279 BOOL start_of_line
= True
;
284 if (maxlen
<2) return(NULL
);
290 maxlen
= MIN(maxlen
,8);
291 t
= (char *)Realloc(s
,maxlen
);
293 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
305 while (len
< maxlen
-1)
313 while (len
> 0 && s
[len
-1] == ' ')
317 if (len
> 0 && s
[len
-1] == '\\')
320 start_of_line
= True
;
327 return(len
>0?s
:NULL
);
332 start_of_line
= False
;
336 if (!s2
&& len
> maxlen
-3) {
340 t
= (char *)Realloc(s
,maxlen
);
342 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
353 /****************************************************************************
354 load from a pipe into memory
355 ****************************************************************************/
356 char *file_pload(const char *syscmd
, size_t *size
)
363 fd
= sys_popen(syscmd
);
364 if (fd
== -1) return NULL
;
369 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0) {
370 tp
= Realloc(p
, total
+ n
+ 1);
372 DEBUG(0,("file_pload: failed to exand buffer!\n"));
378 memcpy(p
+total
, buf
, n
);
385 if (size
) *size
= total
;
390 /****************************************************************************
391 load a file into memory from a fd.
392 ****************************************************************************/
394 char *fd_load(int fd
, size_t *size
)
396 SMB_STRUCT_STAT sbuf
;
399 if (sys_fstat(fd
, &sbuf
) != 0) return NULL
;
401 p
= (char *)malloc(sbuf
.st_size
+1);
404 if (read(fd
, p
, sbuf
.st_size
) != sbuf
.st_size
) {
410 if (size
) *size
= sbuf
.st_size
;
415 /****************************************************************************
416 load a file into memory
417 ****************************************************************************/
418 char *file_load(const char *fname
, size_t *size
)
423 if (!fname
|| !*fname
) return NULL
;
425 fd
= open(fname
,O_RDONLY
);
426 if (fd
== -1) return NULL
;
428 p
= fd_load(fd
, size
);
436 /****************************************************************************
437 parse a buffer into lines
438 ****************************************************************************/
439 static char **file_lines_parse(char *p
, size_t size
, int *numlines
, BOOL convert
)
446 for (s
= p
, i
=0; s
< p
+size
; s
++) {
447 if (s
[0] == '\n') i
++;
450 ret
= (char **)malloc(sizeof(ret
[0])*(i
+2));
455 memset(ret
, 0, sizeof(ret
[0])*(i
+2));
456 if (numlines
) *numlines
= i
;
459 for (s
= p
, i
=0; s
< p
+size
; s
++) {
465 if (s
[0] == '\r') s
[0] = 0;
469 for (i
= 0; ret
[i
]; i
++)
477 /****************************************************************************
478 load a file into memory and return an array of pointers to lines in the file
479 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
481 ****************************************************************************/
482 char **file_lines_load(const char *fname
, int *numlines
, BOOL convert
)
487 p
= file_load(fname
, &size
);
490 return file_lines_parse(p
, size
, numlines
, convert
);
493 /****************************************************************************
494 load a fd into memory and return an array of pointers to lines in the file
495 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
497 ****************************************************************************/
498 char **fd_lines_load(int fd
, int *numlines
, BOOL convert
)
503 p
= fd_load(fd
, &size
);
506 return file_lines_parse(p
, size
, numlines
, convert
);
510 /****************************************************************************
511 load a pipe into memory and return an array of pointers to lines in the data
512 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
514 ****************************************************************************/
515 char **file_lines_pload(const char *syscmd
, int *numlines
, BOOL convert
)
520 p
= file_pload(syscmd
, &size
);
523 return file_lines_parse(p
, size
, numlines
, convert
);
526 /****************************************************************************
527 free lines loaded with file_lines_load
528 ****************************************************************************/
529 void file_lines_free(char **lines
)
537 /****************************************************************************
538 take a lislist of lines and modify them to produce a list where \ continues
540 ****************************************************************************/
541 void file_lines_slashcont(char **lines
)
545 for (i
=0; lines
[i
];) {
546 int len
= strlen(lines
[i
]);
547 if (lines
[i
][len
-1] == '\\') {
548 lines
[i
][len
-1] = ' ';
550 char *p
= &lines
[i
][len
];
551 while (p
< lines
[i
+1]) *p
++ = ' ';
552 for (j
= i
+1; lines
[j
]; j
++) lines
[j
] = lines
[j
+1];