Added writing of '[XXX]' account control bits into gcos
[kai/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   pw_buf.acct_ctrl = ACB_NORMAL;  
191
192   /*
193    * Scan the file, a line at a time and check if the name matches.
194    */
195   while (!feof(fp)) {
196     linebuf[0] = '\0';
197
198     fgets(linebuf, 256, fp);
199     if (ferror(fp)) {
200       return NULL;
201     }
202
203     /*
204      * Check if the string is terminated with a newline - if not
205      * then we must keep reading and discard until we get one.
206      */
207     linebuf_len = strlen(linebuf);
208     if (linebuf[linebuf_len - 1] != '\n') {
209       c = '\0';
210       while (!ferror(fp) && !feof(fp)) {
211         c = fgetc(fp);
212         if (c == '\n')
213           break;
214       }
215     } else
216       linebuf[linebuf_len - 1] = '\0';
217
218 #ifdef DEBUG_PASSWORD
219     DEBUG(100, ("getsmbpwent: got line |%s|\n", linebuf));
220 #endif
221     if ((linebuf[0] == 0) && feof(fp)) {
222       DEBUG(4, ("getsmbpwent: end of file reached\n"));
223       break;
224     }
225     /*
226      * The line we have should be of the form :-
227      * 
228      * username:uid:[32hex bytes]:....other flags presently
229      * ignored....
230      * 
231      * or,
232      *
233      * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
234      *
235      * if Windows NT compatible passwords are also present.
236      */
237
238     if (linebuf[0] == '#' || linebuf[0] == '\0') {
239       DEBUG(6, ("getsmbpwent: skipping comment or blank line\n"));
240       continue;
241     }
242     p = (unsigned char *) strchr(linebuf, ':');
243     if (p == NULL) {
244       DEBUG(0, ("getsmbpwent: malformed password entry (no :)\n"));
245       continue;
246     }
247     /*
248      * As 256 is shorter than a pstring we don't need to check
249      * length here - if this ever changes....
250      */
251     strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
252     user_name[PTR_DIFF(p, linebuf)] = '\0';
253
254     /* Get smb uid. */
255
256     p++;                /* Go past ':' */
257     if (!isdigit(*p)) {
258       DEBUG(0, ("getsmbpwent: malformed password entry (uid not number)\n"));
259       continue;
260     }
261
262     uidval = atoi((char *) p);
263
264     while (*p && isdigit(*p))
265       p++;
266
267     if (*p != ':') {
268       DEBUG(0, ("getsmbpwent: malformed password entry (no : after uid)\n"));
269       continue;
270     }
271
272     pw_buf.smb_name = user_name;
273     pw_buf.smb_userid = uidval;
274
275     /*
276      * Now get the password value - this should be 32 hex digits
277      * which are the ascii representations of a 16 byte string.
278      * Get two at a time and put them into the password.
279      */
280
281     /* Skip the ':' */
282     p++;
283
284     if (*p == '*' || *p == 'X') {
285       /* Password deliberately invalid - end here. */
286       DEBUG(10, ("getsmbpwent: entry invalidated for user %s\n", user_name));
287       pw_buf.smb_nt_passwd = NULL;
288       pw_buf.smb_passwd = NULL;
289       pw_buf.acct_ctrl |= ACB_DISABLED;
290       return &pw_buf;
291     }
292
293     if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
294       DEBUG(0, ("getsmbpwent: malformed password entry (passwd too short)\n"));
295       continue;
296     }
297
298     if (p[32] != ':') {
299       DEBUG(0, ("getsmbpwent: malformed password entry (no terminating :)\n"));
300       continue;
301     }
302
303     if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
304       pw_buf.smb_passwd = NULL;
305       pw_buf.acct_ctrl |= ACB_PWNOTREQ;
306     } else {
307       if (!gethexpwd((char *)p, (char *)smbpwd)) {
308         DEBUG(0, ("getsmbpwent: Malformed Lanman password entry (non hex chars)\n"));
309         continue;
310       }
311       pw_buf.smb_passwd = smbpwd;
312     }
313
314     /* 
315      * Now check if the NT compatible password is
316      * available.
317      */
318     pw_buf.smb_nt_passwd = NULL;
319
320     p += 33; /* Move to the first character of the line after
321                 the lanman password. */
322     if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
323       if (*p != '*' && *p != 'X') {
324         if(gethexpwd((char *)p,(char *)smbntpwd))
325           pw_buf.smb_nt_passwd = smbntpwd;
326       }
327       p += 33; /* Move to the first character of the line after
328                   the NT password. */
329     }
330
331     DEBUG(5, ("getsmbpwent: returning passwd entry for user %s, uid %d\n",
332                           user_name, uidval));
333
334     /*
335      * Check if the account type bits have been encoded after the
336      * NT password (in the form [NDHTUWSLXI]).
337      */
338
339     if (*p == '[') {
340       BOOL finished = False;
341
342       pw_buf.acct_ctrl = 0;
343
344       for(p++;*p && !finished; p++) {
345         switch (*p) {
346 #if 0
347    /*
348     * Hmmm. Don't allow these to be set/read independently
349     * of the actual password fields. We don't want a mismatch.
350     * JRA.
351     */
352           case 'N':
353             /* 'N'o password. */
354             pw_buf.acct_ctrl |= ACB_PWNOTREQ;
355             break;
356           case 'D':
357             /* 'D'isabled. */
358             pw_buf.acct_ctrl |= ACB_DISABLED;
359             break;
360 #endif 
361           case 'H':
362             /* 'H'omedir required. */
363             pw_buf.acct_ctrl |= ACB_HOMDIRREQ;
364             break;
365           case 'T':
366             /* 'T'emp account. */
367             pw_buf.acct_ctrl |= ACB_TEMPDUP;
368             break;
369           case 'U':
370             /* 'U'ser account (normal). */
371             pw_buf.acct_ctrl |= ACB_NORMAL;
372             break;
373           case 'M':
374             /* 'M'NS logon user account. What is this ? */
375             pw_buf.acct_ctrl |= ACB_MNS;
376             break;
377           case 'W':
378             /* 'W'orkstation account. */
379             pw_buf.acct_ctrl |= ACB_WSTRUST;
380             break;
381           case 'S':
382             /* 'S'erver account. */
383             pw_buf.acct_ctrl |= ACB_SVRTRUST;
384             break;
385           case 'L':
386             /* 'L'ocked account. */
387             pw_buf.acct_ctrl |= ACB_AUTOLOCK;
388             break;
389           case 'X':
390             /* No 'X'piry. */
391             pw_buf.acct_ctrl |= ACB_PWNOEXP;
392             break;
393           case 'I':
394             /* 'I'nterdomain trust account. */
395             pw_buf.acct_ctrl |= ACB_DOMTRUST;
396             break;
397
398           case ':':
399           case '\n':
400           case '\0': 
401           case ']':
402           default:
403             finished = True;
404         }
405       }
406
407       /* Must have some account type set. */
408       if(pw_buf.acct_ctrl == 0)
409         pw_buf.acct_ctrl = ACB_NORMAL;
410
411     } else {
412       /* 'Old' style file. Fake up based on user name. */
413       /*
414        * Currently machine accounts are kept in the same
415        * password file as 'normal accounts'. If this changes
416        * we will have to fix this code. JRA.
417        */
418       if(pw_buf.smb_name[strlen(pw_buf.smb_name) - 1] == '$') {
419         pw_buf.acct_ctrl &= ~ACB_NORMAL;
420         pw_buf.acct_ctrl |= ACB_WSTRUST;
421       }
422     }
423
424     return &pw_buf;
425   }
426
427   DEBUG(5,("getsmbpwent: end of file reached.\n"));
428   return NULL;
429 }
430
431 /*************************************************************************
432  Routine to search the smbpasswd file for an entry matching the username
433  or user id.  if the name is NULL, then the smb_uid is used instead.
434  *************************************************************************/
435
436 struct smb_passwd *get_smbpwd_entry(char *name, int smb_userid)
437 {
438   struct smb_passwd *pwd = NULL;
439   FILE *fp = NULL;
440
441   if (name != NULL) {
442     DEBUG(10, ("get_smbpwd_entry: search by name: %s\n", name));
443   } else {
444     DEBUG(10, ("get_smbpwd_entry: search by smb_userid: %x\n", smb_userid));
445   }
446
447   /* Open the smbpassword file - not for update. */
448   fp = startsmbpwent(False);
449
450   if (fp == NULL) {
451     DEBUG(0, ("get_smbpwd_entry: unable to open password file.\n"));
452     return NULL;
453   }
454
455   /*
456    * Scan the file, a line at a time and check if the name 
457    * or uid matches.
458    */
459
460   while ((pwd = getsmbpwent(fp)) != NULL) {
461     if (name != NULL) {
462       /* Search is by user name */
463       if (!strequal(pwd->smb_name, name))
464         continue;
465       DEBUG(10, ("get_smbpwd_entry: found by name: %s\n", name));
466       break;
467     } else {
468       /* Search is by user id */
469       if (pwd->smb_userid != smb_userid)
470         continue;
471       DEBUG(10, ("get_smbpwd_entry: found by smb_userid: %x\n", smb_userid));
472       break;
473     }
474   }
475
476   endsmbpwent(fp);
477   return pwd;
478 }
479
480 /************************************************************************
481  Routine to add an entry to the smbpasswd file.
482 *************************************************************************/
483
484 BOOL add_smbpwd_entry(struct smb_passwd *newpwd)
485 {
486   char *pfile = lp_smb_passwd_file();
487   struct smb_passwd *pwd = NULL;
488   FILE *fp = NULL;
489
490   int i;
491   int wr_len;
492
493   int fd;
494   int new_entry_length;
495   char *new_entry;
496   long offpos;
497   unsigned char *p;
498
499   /* Open the smbpassword file - for update. */
500   fp = startsmbpwent(True);
501
502   if (fp == NULL) {
503     DEBUG(0, ("add_smbpwd_entry: unable to open file.\n"));
504     return False;
505   }
506
507   /*
508    * Scan the file, a line at a time and check if the name matches.
509    */
510
511   while ((pwd = getsmbpwent(fp)) != NULL) {
512     if (strequal(newpwd->smb_name, pwd->smb_name)) {
513       DEBUG(0, ("add_smbpwd_entry: entry with name %s already exists\n", pwd->smb_name));
514       endsmbpwent(fp);
515       return False;
516     }
517   }
518
519   /* Ok - entry doesn't exist. We can add it */
520
521   /* Create a new smb passwd entry and set it to the given password. */
522   /* 
523    * The add user write needs to be atomic - so get the fd from 
524    * the fp and do a raw write() call.
525    */
526   fd = fileno(fp);
527
528   if((offpos = lseek(fd, 0, SEEK_END)) == -1) {
529     DEBUG(0, ("add_smbpwd_entry(lseek): 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   new_entry_length = strlen(pwd->smb_name) + 1 + 15 + 1 + 32 + 1 + 32 + 1 + 2;
536
537   if((new_entry = (char *)malloc( new_entry_length )) == NULL) {
538     DEBUG(0, ("add_smbpwd_entry(malloc): Failed to add entry for user %s to file %s. \
539 Error was %s\n", pwd->smb_name, pfile, strerror(errno)));
540     endsmbpwent(fp);
541     return False;
542   }
543
544   sprintf(new_entry, "%s:%u:", pwd->smb_name, (unsigned)pwd->smb_userid);
545   p = (unsigned char *)&new_entry[strlen(new_entry)];
546
547   for( i = 0; i < 16; i++) {
548     sprintf((char *)&p[i*2], "%02X", pwd->smb_passwd[i]);
549   }
550
551   p += 32;
552
553   *p++ = ':';
554
555   for( i = 0; i < 16; i++) {
556     sprintf((char *)&p[i*2], "%02X", pwd->smb_nt_passwd[i]);
557   }
558   p += 32;
559
560   *p++ = ':';
561   sprintf((char *)p,"\n");
562
563 #ifdef DEBUG_PASSWORD
564   DEBUG(100, ("add_smbpwd_entry(%d): new_entry_len %d entry_len %d made line |%s|\n", 
565                              fd, new_entry_length, strlen(new_entry), new_entry));
566 #endif
567
568   if ((wr_len = write(fd, new_entry, strlen(new_entry))) != strlen(new_entry)) {
569     DEBUG(0, ("add_smbpwd_entry(write): %d Failed to add entry for user %s to file %s. \
570 Error was %s\n", wr_len, pwd->smb_name, pfile, strerror(errno)));
571
572     /* Remove the entry we just wrote. */
573     if(ftruncate(fd, offpos) == -1) {
574       DEBUG(0, ("add_smbpwd_entry: ERROR failed to ftruncate file %s. \
575 Error was %s. Password file may be corrupt ! Please examine by hand !\n", 
576              pwd->smb_name, strerror(errno)));
577     }
578
579     endsmbpwent(fp);
580     return False;
581   }
582
583   endsmbpwent(fp);
584   return True;
585 }
586
587 /************************************************************************
588  Routine to search the smbpasswd file for an entry matching the username.
589  and then modify its password entry. We can't use the startsmbpwent()/
590  getsmbpwent()/endsmbpwent() interfaces here as we depend on looking
591  in the actual file to decide how much room we have to write data.
592 ************************************************************************/
593
594 BOOL mod_smbpwd_entry(struct smb_passwd* pwd)
595 {
596   /* Static buffers we will return. */
597   static pstring  user_name;
598
599   char            linebuf[256];
600   char            readbuf[16 * 1024];
601   unsigned char   c;
602   char            ascii_p16[66];
603   unsigned char  *p = NULL;
604   long            linebuf_len = 0;
605   FILE           *fp;
606   int             lockfd;
607   char           *pfile = lp_smb_passwd_file();
608   BOOL found_entry = False;
609
610   long pwd_seekpos = 0;
611
612   int i;
613   int wr_len;
614   int fd;
615
616   if (!*pfile) {
617     DEBUG(0, ("No SMB password file set\n"));
618     return False;
619   }
620   DEBUG(10, ("mod_smbpwd_entry: opening file %s\n", pfile));
621
622   fp = fopen(pfile, "r+");
623
624   if (fp == NULL) {
625     DEBUG(0, ("mod_smbpwd_entry: unable to open file %s\n", pfile));
626     return False;
627   }
628   /* Set a 16k buffer to do more efficient reads */
629   setvbuf(fp, readbuf, _IOFBF, sizeof(readbuf));
630
631   if ((lockfd = pw_file_lock(fileno(fp), F_RDLCK | F_WRLCK, 5)) < 0) {
632     DEBUG(0, ("mod_smbpwd_entry: unable to lock file %s\n", pfile));
633     fclose(fp);
634     return False;
635   }
636
637   /* Make sure it is only rw by the owner */
638   chmod(pfile, 0600);
639
640   /* We have a write lock on the file. */
641   /*
642    * Scan the file, a line at a time and check if the name matches.
643    */
644   while (!feof(fp)) {
645     pwd_seekpos = ftell(fp);
646
647     linebuf[0] = '\0';
648
649     fgets(linebuf, 256, fp);
650     if (ferror(fp)) {
651       fclose(fp);
652       pw_file_unlock(lockfd);
653       return False;
654     }
655
656     /*
657      * Check if the string is terminated with a newline - if not
658      * then we must keep reading and discard until we get one.
659      */
660     linebuf_len = strlen(linebuf);
661     if (linebuf[linebuf_len - 1] != '\n') {
662       c = '\0';
663       while (!ferror(fp) && !feof(fp)) {
664         c = fgetc(fp);
665         if (c == '\n') {
666           break;
667         }
668       }
669     } else {
670       linebuf[linebuf_len - 1] = '\0';
671     }
672
673 #ifdef DEBUG_PASSWORD
674     DEBUG(100, ("mod_smbpwd_entry: got line |%s|\n", linebuf));
675 #endif
676
677     if ((linebuf[0] == 0) && feof(fp)) {
678       DEBUG(4, ("mod_smbpwd_entry: end of file reached\n"));
679       break;
680     }
681
682     /*
683      * The line we have should be of the form :-
684      * 
685      * username:uid:[32hex bytes]:....other flags presently
686      * ignored....
687      * 
688      * or,
689      *
690      * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
691      *
692      * if Windows NT compatible passwords are also present.
693      */
694
695     if (linebuf[0] == '#' || linebuf[0] == '\0') {
696       DEBUG(6, ("mod_smbpwd_entry: skipping comment or blank line\n"));
697       continue;
698     }
699
700     p = (unsigned char *) strchr(linebuf, ':');
701
702     if (p == NULL) {
703       DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no :)\n"));
704       continue;
705     }
706
707     /*
708      * As 256 is shorter than a pstring we don't need to check
709      * length here - if this ever changes....
710      */
711     strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
712     user_name[PTR_DIFF(p, linebuf)] = '\0';
713     if (strequal(user_name, pwd->smb_name)) {
714       found_entry = True;
715       break;
716     }
717   }
718
719   if (!found_entry) return False;
720
721   DEBUG(6, ("mod_smbpwd_entry: entry exists\n"));
722
723   /* User name matches - get uid and password */
724   p++;          /* Go past ':' */
725
726   if (!isdigit(*p)) {
727     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (uid not number)\n"));
728     fclose(fp);
729     pw_file_unlock(lockfd);
730     return False;
731   }
732
733   while (*p && isdigit(*p))
734     p++;
735   if (*p != ':') {
736     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no : after uid)\n"));
737     fclose(fp);
738     pw_file_unlock(lockfd);
739     return False;
740   }
741
742   /*
743    * Now get the password value - this should be 32 hex digits
744    * which are the ascii representations of a 16 byte string.
745    * Get two at a time and put them into the password.
746    */
747   p++;
748
749   /* Record exact password position */
750   pwd_seekpos += PTR_DIFF(p, linebuf);
751
752   if (*p == '*' || *p == 'X') {
753     /* Password deliberately invalid - end here. */
754     DEBUG(10, ("get_smbpwd_entry: entry invalidated for user %s\n", user_name));
755     fclose(fp);
756     pw_file_unlock(lockfd);
757     return False;
758   }
759
760   if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
761     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (passwd too short)\n"));
762     fclose(fp);
763     pw_file_unlock(lockfd);
764     return (False);
765   }
766
767   if (p[32] != ':') {
768     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no terminating :)\n"));
769     fclose(fp);
770     pw_file_unlock(lockfd);
771     return False;
772   }
773
774   if (*p == '*' || *p == 'X') {
775     fclose(fp);
776     pw_file_unlock(lockfd);
777     return False;
778   }
779
780   /* Now check if the NT compatible password is
781      available. */
782   p += 33; /* Move to the first character of the line after
783               the lanman password. */
784   if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
785     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (passwd too short)\n"));
786     fclose(fp);
787     pw_file_unlock(lockfd);
788     return (False);
789   }
790
791   if (p[32] != ':') {
792     DEBUG(0, ("mod_smbpwd_entry: malformed password entry (no terminating :)\n"));
793     fclose(fp);
794     pw_file_unlock(lockfd);
795     return False;
796   }
797
798   /* Entry is correctly formed. */
799
800   /*
801    * Do an atomic write into the file at the position defined by
802    * seekpos.
803    */
804
805   /* The mod user write needs to be atomic - so get the fd from 
806      the fp and do a raw write() call.
807    */
808
809   fd = fileno(fp);
810
811   if (lseek(fd, pwd_seekpos - 1, SEEK_SET) != pwd_seekpos - 1) {
812     DEBUG(0, ("mod_smbpwd_entry: seek fail on file %s.\n", pfile));
813     fclose(fp);
814     pw_file_unlock(lockfd);
815     return False;
816   }
817
818   /* Sanity check - ensure the character is a ':' */
819   if (read(fd, &c, 1) != 1) {
820     DEBUG(0, ("mod_smbpwd_entry: read fail on file %s.\n", pfile));
821     fclose(fp);
822     pw_file_unlock(lockfd);
823     return False;
824   }
825
826   if (c != ':') {
827     DEBUG(0, ("mod_smbpwd_entry: check on passwd file %s failed.\n", pfile));
828     fclose(fp);
829     pw_file_unlock(lockfd);
830     return False;
831   }
832  
833   /* Create the 32 byte representation of the new p16 */
834   for (i = 0; i < 16; i++) {
835     sprintf(&ascii_p16[i*2], "%02X", (uchar) pwd->smb_passwd[i]);
836   }
837
838   /* Add on the NT md4 hash */
839   ascii_p16[32] = ':';
840   wr_len = 65;
841   if (pwd->smb_nt_passwd != NULL) {
842     for (i = 0; i < 16; i++) {
843       sprintf(&ascii_p16[(i*2)+33], "%02X", (uchar) pwd->smb_nt_passwd[i]);
844     }
845   } else {
846     /* No NT hash - write out an 'invalid' string. */
847     strcpy(&ascii_p16[33], "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
848   }
849
850 #ifdef DEBUG_PASSWORD
851   DEBUG(100,("mod_smbpwd_entry: "));
852   dump_data(100, ascii_p16, wr_len);
853 #endif
854
855   if (write(fd, ascii_p16, wr_len) != wr_len) {
856     DEBUG(0, ("mod_smbpwd_entry: write failed in passwd file %s\n", pfile));
857     fclose(fp);
858     pw_file_unlock(lockfd);
859     return False;
860   }
861
862   fclose(fp);
863   pw_file_unlock(lockfd);
864   return True;
865 }