"For I have laboured mightily on Luke's code, and hath broken
[samba.git] / source3 / passdb / smbpass.c
1 /*
2  * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4  * 
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)
8  * any later version.
9  * 
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
13  * more details.
14  * 
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.
18  */
19
20 #include "includes.h"
21
22 extern int DEBUGLEVEL;
23
24 static int gotalarm;
25 static char s_readbuf[16 * 1024];
26
27 /***************************************************************
28  Signal function to tell us we timed out.
29 ****************************************************************/
30
31 static void gotalarm_sig()
32 {
33   gotalarm = 1;
34 }
35
36 /***************************************************************
37  Lock or unlock a fd for a known lock type. Abandon after waitsecs 
38  seconds.
39 ****************************************************************/
40
41 static int do_pw_lock(int fd, int waitsecs, int type)
42 {
43   struct flock    lock;
44   int             ret;
45
46   gotalarm = 0;
47   signal(SIGALRM, SIGNAL_CAST gotalarm_sig);
48
49   lock.l_type = type;
50   lock.l_whence = SEEK_SET;
51   lock.l_start = 0;
52   lock.l_len = 1;
53   lock.l_pid = 0;
54
55   alarm(5);
56   ret = fcntl(fd, F_SETLKW, &lock);
57   alarm(0);
58   signal(SIGALRM, SIGNAL_CAST SIG_DFL);
59
60   if (gotalarm) {
61     DEBUG(0, ("do_pw_lock: failed to %s SMB passwd file.\n",
62                 type == F_UNLCK ? "unlock" : "lock"));
63     return -1;
64   }
65   return ret;
66 }
67
68 /***************************************************************
69  Lock an fd. Abandon after waitsecs seconds.
70 ****************************************************************/
71
72 int pw_file_lock(int fd, int type, int secs)
73 {
74   if (fd < 0)
75     return (-1);
76   if (do_pw_lock(fd, secs, type)) {
77     return -1;
78   }
79   return fd;
80 }
81
82 /***************************************************************
83  Unlock an fd. Abandon after waitsecs seconds.
84 ****************************************************************/
85
86 int pw_file_unlock(int fd)
87 {
88  return do_pw_lock(fd, 5, F_UNLCK);
89 }
90
91 /***************************************************************
92  Open the smbpasswd file - get ready to enumerate it.
93 ****************************************************************/
94
95 FILE *startsmbpwent(BOOL update)
96 {
97   FILE *fp = NULL;
98   char *pfile = lp_smb_passwd_file();
99
100   if (!*pfile) {
101     DEBUG(0, ("startsmbpwent: No SMB password file set\n"));
102     return (NULL);
103   }
104   DEBUG(10, ("startsmbpwent: opening file %s\n", pfile));
105
106   fp = fopen(pfile, update ? "r+b" : "rb");
107
108   if (fp == NULL) {
109     DEBUG(0, ("startsmbpwent: unable to open file %s\n", pfile));
110     return NULL;
111   }
112
113   /* Set a 16k buffer to do more efficient reads */
114   setvbuf(fp, s_readbuf, _IOFBF, sizeof(s_readbuf));
115
116   if ((pw_file_lock(fileno(fp), F_RDLCK | (update ? F_WRLCK : 0), 5)) < 0) {
117     DEBUG(0, ("startsmbpwent: unable to lock file %s\n", pfile));
118     fclose(fp);
119     return NULL;
120   }
121
122   /* Make sure it is only rw by the owner */
123   chmod(pfile, 0600);
124
125   /* We have a lock on the file. */
126   return fp;
127 }
128
129 /***************************************************************
130  Close the smbpasswd file - end enumeration.
131 ****************************************************************/
132
133 void endsmbpwent(FILE *fp)
134 {
135   pw_file_unlock(fileno(fp));
136   fclose(fp);
137   DEBUG(7, ("endsmbpwent: closed password file.\n"));
138 }
139
140 /*************************************************************
141  Routine to get the next 32 hex characters and turn them
142  into a 16 byte array.
143 **************************************************************/
144
145 static int gethexpwd(char *p, char *pwd)
146 {
147   int i;
148   unsigned char   lonybble, hinybble;
149   char           *hexchars = "0123456789ABCDEF";
150   char           *p1, *p2;
151
152   for (i = 0; i < 32; i += 2) {
153     hinybble = toupper(p[i]);
154     lonybble = toupper(p[i + 1]);
155  
156     p1 = strchr(hexchars, hinybble);
157     p2 = strchr(hexchars, lonybble);
158     if (!p1 || !p2)
159       return (False);
160     hinybble = PTR_DIFF(p1, hexchars);
161     lonybble = PTR_DIFF(p2, hexchars);
162  
163     pwd[i / 2] = (hinybble << 4) | lonybble;
164   }
165   return (True);
166 }
167
168 /*************************************************************************
169  Routine to return the next entry in the smbpasswd file.
170  *************************************************************************/
171
172 struct smb_passwd *getsmbpwent(FILE *fp)
173 {
174   /* Static buffers we will return. */
175   static struct smb_passwd pw_buf;
176   static pstring  user_name;
177   static unsigned char smbpwd[16];
178   static unsigned char smbntpwd[16];
179   char            linebuf[256];
180   unsigned char   c;
181   unsigned char  *p;
182   long            uidval;
183   long            linebuf_len;
184
185   if(fp == NULL) {
186     DEBUG(0,("getsmbpwent: Bad password file pointer.\n"));
187     return NULL;
188   }
189
190   /*
191    * Scan the file, a line at a time and check if the name matches.
192    */
193   while (!feof(fp)) {
194     linebuf[0] = '\0';
195
196     fgets(linebuf, 256, fp);
197     if (ferror(fp)) {
198       return NULL;
199     }
200
201     /*
202      * Check if the string is terminated with a newline - if not
203      * then we must keep reading and discard until we get one.
204      */
205     linebuf_len = strlen(linebuf);
206     if (linebuf[linebuf_len - 1] != '\n') {
207       c = '\0';
208       while (!ferror(fp) && !feof(fp)) {
209         c = fgetc(fp);
210         if (c == '\n')
211           break;
212       }
213     } else
214       linebuf[linebuf_len - 1] = '\0';
215
216 #ifdef DEBUG_PASSWORD
217     DEBUG(100, ("getsmbpwent: got line |%s|\n", linebuf));
218 #endif
219     if ((linebuf[0] == 0) && feof(fp)) {
220       DEBUG(4, ("getsmbpwent: end of file reached\n"));
221       break;
222     }
223     /*
224      * The line we have should be of the form :-
225      * 
226      * username:uid:[32hex bytes]:....other flags presently
227      * ignored....
228      * 
229      * or,
230      *
231      * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
232      *
233      * if Windows NT compatible passwords are also present.
234      */
235
236     if (linebuf[0] == '#' || linebuf[0] == '\0') {
237       DEBUG(6, ("getsmbpwent: skipping comment or blank line\n"));
238       continue;
239     }
240     p = (unsigned char *) strchr(linebuf, ':');
241     if (p == NULL) {
242       DEBUG(0, ("getsmbpwent: malformed password entry (no :)\n"));
243       continue;
244     }
245     /*
246      * As 256 is shorter than a pstring we don't need to check
247      * length here - if this ever changes....
248      */
249     strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
250     user_name[PTR_DIFF(p, linebuf)] = '\0';
251
252     /* Get smb uid. */
253
254     p++;                /* Go past ':' */
255     if (!isdigit(*p)) {
256       DEBUG(0, ("getsmbpwent: malformed password entry (uid not number)\n"));
257       continue;
258     }
259
260     uidval = atoi((char *) p);
261
262     while (*p && isdigit(*p))
263       p++;
264
265     if (*p != ':') {
266       DEBUG(0, ("getsmbpwent: malformed password entry (no : after uid)\n"));
267       continue;
268     }
269
270     pw_buf.smb_name = user_name;
271     pw_buf.smb_userid = uidval;
272
273     /*
274      * Now get the password value - this should be 32 hex digits
275      * which are the ascii representations of a 16 byte string.
276      * Get two at a time and put them into the password.
277      */
278
279     /* Skip the ':' */
280     p++;
281
282     if (*p == '*' || *p == 'X') {
283       /* Password deliberately invalid - end here. */
284       DEBUG(10, ("getsmbpwent: entry invalidated for user %s\n", user_name));
285       pw_buf.smb_nt_passwd = NULL;
286       pw_buf.smb_passwd = NULL;
287       pw_buf.acct_ctrl |= ACB_DISABLED;
288       return &pw_buf;
289     }
290
291     if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
292       DEBUG(0, ("getsmbpwent: malformed password entry (passwd too short)\n"));
293       continue;
294     }
295
296     if (p[32] != ':') {
297       DEBUG(0, ("getsmbpwent: malformed password entry (no terminating :)\n"));
298       continue;
299     }
300
301     if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
302       pw_buf.smb_passwd = NULL;
303       pw_buf.acct_ctrl |= ACB_PWNOTREQ;
304     } else {
305       if (!gethexpwd((char *)p, (char *)smbpwd)) {
306         DEBUG(0, ("getsmbpwent: Malformed Lanman password entry (non hex chars)\n"));
307         continue;
308       }
309       pw_buf.smb_passwd = smbpwd;
310     }
311
312     /* 
313      * Now check if the NT compatible password is
314      * available.
315      */
316     pw_buf.smb_nt_passwd = NULL;
317
318     p += 33; /* Move to the first character of the line after
319                 the lanman password. */
320     if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
321       if (*p != '*' && *p != 'X') {
322         if(gethexpwd((char *)p,(char *)smbntpwd))
323           pw_buf.smb_nt_passwd = smbntpwd;
324       }
325       p += 33; /* Move to the first character of the line after
326                   the NT password. */
327     }
328
329     DEBUG(5, ("getsmbpwent: returning passwd entry for user %s, uid %d\n",
330                           user_name, uidval));
331
332     /*
333      * Check if the account type bits have been encoded after the
334      * NT password (in the form [NDHTUWSLXI]).
335      */
336
337     if (*p == '[') {
338       BOOL finished = False;
339
340       pw_buf.acct_ctrl = 0;
341
342       for(p++;*p && !finished; p++) {
343         switch (*p) {
344           case 'N':
345             /* 'N'o password. */
346             pw_buf.acct_ctrl |= ACB_PWNOTREQ;
347             break;
348           case 'D':
349             /* 'D'isabled. */
350             pw_buf.acct_ctrl |= ACB_DISABLED;
351             break;
352           case 'H':
353             /* 'H'omedir required. */
354             pw_buf.acct_ctrl |= ACB_HOMDIRREQ;
355             break;
356           case 'T':
357             /* 'T'emp account. */
358             pw_buf.acct_ctrl |= ACB_TEMPDUP;
359             break;
360           case 'U':
361             /* 'U'ser account (normal). */
362             pw_buf.acct_ctrl |= ACB_NORMAL;
363             break;
364           case 'M':
365             /* 'M'NS logon user account. What is this ? */
366             pw_buf.acct_ctrl |= ACB_MNS;
367             break;
368           case 'W':
369             /* 'W'orkstation account. */
370             pw_buf.acct_ctrl |= ACB_WSTRUST;
371             break;
372           case 'S':
373             /* 'S'erver account. */
374             pw_buf.acct_ctrl |= ACB_SVRTRUST;
375             break;
376           case 'L':
377             /* 'L'ocked account. */
378             pw_buf.acct_ctrl |= ACB_AUTOLOCK;
379             break;
380           case 'X':
381             /* No 'X'piry. */
382             pw_buf.acct_ctrl |= ACB_PWNOEXP;
383             break;
384           case 'I':
385             /* 'I'nterdomain trust account. */
386             pw_buf.acct_ctrl |= ACB_DOMTRUST;
387             break;
388
389           case ':':
390           case '\n':
391           case '\0': 
392           case ']':
393           default:
394             finished = True;
395         }
396       }
397
398       /* Must have some account type set. */
399       if(pw_buf.acct_ctrl == 0)
400         pw_buf.acct_ctrl = ACB_NORMAL;
401
402     } else {
403       /* 'Old' style file. Fake up based on user name. */
404       /*
405        * Currently machine accounts are kept in the same
406        * password file as 'normal accounts'. If this changes
407        * we will have to fix this code. JRA.
408        */
409       if(pw_buf.smb_name[strlen(pw_buf.smb_name) - 1] == '$')
410         pw_buf.acct_ctrl = ACB_WSTRUST;
411       else
412         pw_buf.acct_ctrl = ACB_NORMAL;
413     }
414
415     return &pw_buf;
416   }
417
418   DEBUG(5,("getsmbpwent: end of file reached.\n"));
419   return NULL;
420 }
421
422 /*************************************************************************
423  Routine to search the smbpasswd file for an entry matching the username
424  or user id.  if the name is NULL, then the smb_uid is used instead.
425  *************************************************************************/
426
427 struct smb_passwd *get_smbpwd_entry(char *name, int smb_userid)
428 {
429   struct smb_passwd *pwd = NULL;
430   FILE *fp = NULL;
431
432   if (name != NULL) {
433     DEBUG(10, ("get_smbpwd_entry: search by name: %s\n", name));
434   } else {
435     DEBUG(10, ("get_smbpwd_entry: search by smb_userid: %x\n", smb_userid));
436   }
437
438   /* Open the smbpassword file - not for update. */
439   fp = startsmbpwent(False);
440
441   if (fp == NULL) {
442     DEBUG(0, ("get_smbpwd_entry: unable to open password file.\n"));
443     return NULL;
444   }
445
446   /*
447    * Scan the file, a line at a time and check if the name 
448    * or uid matches.
449    */
450
451   while ((pwd = getsmbpwent(fp)) != NULL) {
452     if (name != NULL) {
453       /* Search is by user name */
454       if (!strequal(pwd->smb_name, name))
455         continue;
456       DEBUG(10, ("get_smbpwd_entry: found by name: %s\n", name));
457       break;
458     } else {
459       /* Search is by user id */
460       if (pwd->smb_userid != smb_userid)
461         continue;
462       DEBUG(10, ("get_smbpwd_entry: found by smb_userid: %x\n", smb_userid));
463       break;
464     }
465   }
466
467   endsmbpwent(fp);
468   return pwd;
469 }
470
471 /************************************************************************
472  Routine to add an entry to the smbpasswd file.
473 *************************************************************************/
474
475 BOOL add_smbpwd_entry(struct smb_passwd *newpwd)
476 {
477   char *pfile = lp_smb_passwd_file();
478   struct smb_passwd *pwd = NULL;
479   FILE *fp = NULL;
480
481   int i;
482   int wr_len;
483
484   int fd;
485   int new_entry_length;
486   char *new_entry;
487   long offpos;
488   unsigned char *p;
489
490   /* Open the smbpassword file - for update. */
491   fp = startsmbpwent(True);
492
493   if (fp == NULL) {
494     DEBUG(0, ("add_smbpwd_entry: unable to open file.\n"));
495     return False;
496   }
497
498   /*
499    * Scan the file, a line at a time and check if the name matches.
500    */
501
502   while ((pwd = getsmbpwent(fp)) != NULL) {
503     if (strequal(newpwd->smb_name, pwd->smb_name)) {
504       DEBUG(0, ("add_smbpwd_entry: entry with name %s already exists\n", pwd->smb_name));
505       endsmbpwent(fp);
506       return False;
507     }
508   }
509
510   /* Ok - entry doesn't exist. We can add it */
511
512   /* Create a new smb passwd entry and set it to the given password. */
513   /* 
514    * The add user write needs to be atomic - so get the fd from 
515    * the fp and do a raw write() call.
516    */
517   fd = fileno(fp);
518
519   if((offpos = lseek(fd, 0, SEEK_END)) == -1) {
520     DEBUG(0, ("add_smbpwd_entry(lseek): Failed to add entry for user %s to file %s. \
521 Error was %s\n", pwd->smb_name, pfile, strerror(errno)));
522     endsmbpwent(fp);
523     return False;
524   }
525
526   new_entry_length = strlen(pwd->smb_name) + 1 + 15 + 1 + 32 + 1 + 32 + 1 + 2;
527
528   if((new_entry = (char *)malloc( new_entry_length )) == NULL) {
529     DEBUG(0, ("add_smbpwd_entry(malloc): Failed to add entry for user %s to file %s. \
530 Error was %s\n", pwd->smb_name, pfile, strerror(errno)));
531     endsmbpwent(fp);
532     return False;
533   }
534
535   sprintf(new_entry, "%s:%u:", pwd->smb_name, (unsigned)pwd->smb_userid);
536   p = (unsigned char *)&new_entry[strlen(new_entry)];
537
538   for( i = 0; i < 16; i++) {
539     sprintf((char *)&p[i*2], "%02X", pwd->smb_passwd[i]);
540   }
541
542   p += 32;
543
544   *p++ = ':';
545
546   for( i = 0; i < 16; i++) {
547     sprintf((char *)&p[i*2], "%02X", pwd->smb_nt_passwd[i]);
548   }
549   p += 32;
550
551   *p++ = ':';
552   sprintf((char *)p,"\n");
553
554 #ifdef DEBUG_PASSWORD
555   DEBUG(100, ("add_smbpwd_entry(%d): new_entry_len %d entry_len %d made line |%s|\n", 
556                              fd, new_entry_length, strlen(new_entry), new_entry));
557 #endif
558
559   if ((wr_len = write(fd, new_entry, strlen(new_entry))) != strlen(new_entry)) {
560     DEBUG(0, ("add_smbpwd_entry(write): %d Failed to add entry for user %s to file %s. \
561 Error was %s\n", wr_len, pwd->smb_name, pfile, strerror(errno)));
562
563     /* Remove the entry we just wrote. */
564     if(ftruncate(fd, offpos) == -1) {
565       DEBUG(0, ("add_smbpwd_entry: ERROR failed to ftruncate file %s. \
566 Error was %s. Password file may be corrupt ! Please examine by hand !\n", 
567              pwd->smb_name, strerror(errno)));
568     }
569
570     endsmbpwent(fp);
571     return False;
572   }
573
574   endsmbpwent(fp);
575   return True;
576 }
577
578 /************************************************************************
579  Routine to search the smbpasswd file for an entry matching the username.
580  and then modify its password entry. We can't use the startsmbpwent()/
581  getsmbpwent()/endsmbpwent() interfaces here as we depend on looking
582  in the actual file to decide how much room we have to write data.
583 ************************************************************************/
584
585 BOOL mod_smbpwd_entry(struct smb_passwd* pwd)
586 {
587   /* Static buffers we will return. */
588   static pstring  user_name;
589
590   char            linebuf[256];
591   char            readbuf[16 * 1024];
592   unsigned char   c;
593   char            ascii_p16[66];
594   unsigned char  *p = NULL;
595   long            linebuf_len = 0;
596   FILE           *fp;
597   int             lockfd;
598   char           *pfile = lp_smb_passwd_file();
599   BOOL found_entry = False;
600
601   long pwd_seekpos = 0;
602
603   int i;
604   int wr_len;
605   int fd;
606
607   if (!*pfile) {
608     DEBUG(0, ("No SMB password file set\n"));
609     return False;
610   }
611   DEBUG(10, ("mod_smbpwd_entry: opening file %s\n", pfile));
612
613   fp = fopen(pfile, "r+");
614
615   if (fp == NULL) {
616     DEBUG(0, ("mod_smbpwd_entry: unable to open file %s\n", pfile));
617     return False;
618   }
619   /* Set a 16k buffer to do more efficient reads */
620   setvbuf(fp, readbuf, _IOFBF, sizeof(readbuf));
621
622   if ((lockfd = pw_file_lock(fileno(fp), F_RDLCK | F_WRLCK, 5)) < 0) {
623     DEBUG(0, ("mod_smbpwd_entry: unable to lock file %s\n", pfile));
624     fclose(fp);
625     return False;
626   }
627
628   /* Make sure it is only rw by the owner */
629   chmod(pfile, 0600);
630
631   /* We have a write lock on the file. */
632   /*
633    * Scan the file, a line at a time and check if the name matches.
634    */
635   while (!feof(fp)) {
636     pwd_seekpos = ftell(fp);
637
638     linebuf[0] = '\0';
639
640     fgets(linebuf, 256, fp);
641     if (ferror(fp)) {
642       fclose(fp);
643       pw_file_unlock(lockfd);
644       return False;
645     }
646
647     /*
648      * Check if the string is terminated with a newline - if not
649      * then we must keep reading and discard until we get one.
650      */
651     linebuf_len = strlen(linebuf);
652     if (linebuf[linebuf_len - 1] != '\n') {
653       c = '\0';
654       while (!ferror(fp) && !feof(fp)) {
655         c = fgetc(fp);
656         if (c == '\n') {
657           break;
658         }
659       }
660     } else {
661       linebuf[linebuf_len - 1] = '\0';
662     }
663
664 #ifdef DEBUG_PASSWORD
665     DEBUG(100, ("mod_smbpwd_entry: got line |%s|\n", linebuf));
666 #endif
667
668     if ((linebuf[0] == 0) && feof(fp)) {
669       DEBUG(4, ("mod_smbpwd_entry: end of file reached\n"));
670       break;
671     }
672
673     /*
674      * The line we have should be of the form :-
675      * 
676      * username:uid:[32hex bytes]:....other flags presently
677      * ignored....
678      * 
679      * or,
680      *
681      * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
682      *
683      * if Windows NT compatible passwords are also present.
684      */
685
686     if (linebuf[0] == '#' || linebuf[0] == '\0') {
687       DEBUG(6, ("mod_smbpwd_entry: skipping comment or blank line\n"));
688       continue;
689     }
690
691     p = (unsigned char *) strchr(linebuf, ':');
692
693     if (p == NULL) {
694       DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no :)\n"));
695       continue;
696     }
697
698     /*
699      * As 256 is shorter than a pstring we don't need to check
700      * length here - if this ever changes....
701      */
702     strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
703     user_name[PTR_DIFF(p, linebuf)] = '\0';
704     if (strequal(user_name, pwd->smb_name)) {
705       found_entry = True;
706       break;
707     }
708   }
709
710   if (!found_entry) return False;
711
712   DEBUG(6, ("mod_smbpwd_entry: entry exists\n"));
713
714   /* User name matches - get uid and password */
715   p++;          /* Go past ':' */
716
717   if (!isdigit(*p)) {
718     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (uid not number)\n"));
719     fclose(fp);
720     pw_file_unlock(lockfd);
721     return False;
722   }
723
724   while (*p && isdigit(*p))
725     p++;
726   if (*p != ':') {
727     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no : after uid)\n"));
728     fclose(fp);
729     pw_file_unlock(lockfd);
730     return False;
731   }
732
733   /*
734    * Now get the password value - this should be 32 hex digits
735    * which are the ascii representations of a 16 byte string.
736    * Get two at a time and put them into the password.
737    */
738   p++;
739
740   /* Record exact password position */
741   pwd_seekpos += PTR_DIFF(p, linebuf);
742
743   if (*p == '*' || *p == 'X') {
744     /* Password deliberately invalid - end here. */
745     DEBUG(10, ("get_smbpwd_entry: entry invalidated for user %s\n", user_name));
746     fclose(fp);
747     pw_file_unlock(lockfd);
748     return False;
749   }
750
751   if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
752     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (passwd too short)\n"));
753     fclose(fp);
754     pw_file_unlock(lockfd);
755     return (False);
756   }
757
758   if (p[32] != ':') {
759     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no terminating :)\n"));
760     fclose(fp);
761     pw_file_unlock(lockfd);
762     return False;
763   }
764
765   if (*p == '*' || *p == 'X') {
766     fclose(fp);
767     pw_file_unlock(lockfd);
768     return False;
769   }
770
771   if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
772     fclose(fp);
773     pw_file_unlock(lockfd);
774     return False;
775   }
776
777   /* Now check if the NT compatible password is
778      available. */
779   p += 33; /* Move to the first character of the line after
780               the lanman password. */
781   if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
782     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (passwd too short)\n"));
783     fclose(fp);
784     pw_file_unlock(lockfd);
785     return (False);
786   }
787
788   if (p[32] != ':') {
789     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no terminating :)\n"));
790     fclose(fp);
791     pw_file_unlock(lockfd);
792     return False;
793   }
794
795   /* Entry is correctly formed. */
796
797   /*
798    * Do an atomic write into the file at the position defined by
799    * seekpos.
800    */
801
802   /* The mod user write needs to be atomic - so get the fd from 
803      the fp and do a raw write() call.
804    */
805
806   fd = fileno(fp);
807
808   if (lseek(fd, pwd_seekpos - 1, SEEK_SET) != pwd_seekpos - 1) {
809     DEBUG(0, ("mod_smbpwd_entry: seek fail on file %s.\n", pfile));
810     fclose(fp);
811     pw_file_unlock(lockfd);
812     return False;
813   }
814
815   /* Sanity check - ensure the character is a ':' */
816   if (read(fd, &c, 1) != 1) {
817     DEBUG(0, ("mod_smbpwd_entry: read fail on file %s.\n", pfile));
818     fclose(fp);
819     pw_file_unlock(lockfd);
820     return False;
821   }
822
823   if (c != ':') {
824     DEBUG(0, ("mod_smbpwd_entry: check on passwd file %s failed.\n", pfile));
825     fclose(fp);
826     pw_file_unlock(lockfd);
827     return False;
828   }
829  
830   /* Create the 32 byte representation of the new p16 */
831   for (i = 0; i < 16; i++) {
832     sprintf(&ascii_p16[i*2], "%02X", (uchar) pwd->smb_passwd[i]);
833   }
834
835   /* Add on the NT md4 hash */
836   ascii_p16[32] = ':';
837   wr_len = 65;
838   if (pwd->smb_nt_passwd != NULL) {
839     for (i = 0; i < 16; i++) {
840       sprintf(&ascii_p16[(i*2)+33], "%02X", (uchar) pwd->smb_nt_passwd[i]);
841     }
842   } else {
843     /* No NT hash - write out an 'invalid' string. */
844     strcpy(&ascii_p16[33], "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
845   }
846
847 #ifdef DEBUG_PASSWORD
848   DEBUG(100,("mod_smbpwd_entry: "));
849   dump_data(100, ascii_p16, wr_len);
850 #endif
851
852   if (write(fd, ascii_p16, wr_len) != wr_len) {
853     DEBUG(0, ("mod_smbpwd_entry: write failed in passwd file %s\n", pfile));
854     fclose(fp);
855     pw_file_unlock(lockfd);
856     return False;
857   }
858
859   fclose(fp);
860   pw_file_unlock(lockfd);
861   return True;
862 }