Moved code that changes the pw_passwd entry (i.e shadow password and
[ira/wip.git] / source3 / smbd / service.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    service (connection) opening and closing
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26 extern time_t smb_last_time;
27 extern int case_default;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
30 extern BOOL case_mangle;
31 extern BOOL case_sensitive;
32 extern BOOL use_mangled_map;
33 extern fstring remote_machine;
34 extern pstring sesssetup_user;
35 extern fstring remote_machine;
36
37
38 /****************************************************************************
39 load parameters specific to a connection/service
40 ****************************************************************************/
41 BOOL become_service(connection_struct *conn,BOOL do_chdir)
42 {
43         extern char magic_char;
44         static connection_struct *last_conn;
45         int snum;
46
47         if (!conn)  {
48                 last_conn = NULL;
49                 return(False);
50         }
51
52         conn->lastused = smb_last_time;
53
54         snum = SNUM(conn);
55   
56         if (do_chdir &&
57             dos_ChDir(conn->connectpath) != 0 &&
58             dos_ChDir(conn->origpath) != 0) {
59                 DEBUG(0,("chdir (%s) failed\n",
60                          conn->connectpath));
61                 return(False);
62         }
63
64         if (conn == last_conn)
65                 return(True);
66
67         last_conn = conn;
68
69         case_default = lp_defaultcase(snum);
70         case_preserve = lp_preservecase(snum);
71         short_case_preserve = lp_shortpreservecase(snum);
72         case_mangle = lp_casemangle(snum);
73         case_sensitive = lp_casesensitive(snum);
74         magic_char = lp_magicchar(snum);
75         use_mangled_map = (*lp_mangled_map(snum) ? True:False);
76         return(True);
77 }
78
79
80 /****************************************************************************
81   find a service entry
82 ****************************************************************************/
83 int find_service(char *service)
84 {
85    int iService;
86
87    string_sub(service,"\\","/");
88
89    iService = lp_servicenumber(service);
90
91    /* now handle the special case of a home directory */
92    if (iService < 0)
93    {
94       char *phome_dir = get_home_dir(service);
95         pstring home_dir;
96
97       if(phome_dir == NULL)
98       {
99         /*
100          * Try mapping the servicename, it may
101          * be a Windows to unix mapped user name.
102          */
103         if(map_username(service))
104           phome_dir = get_home_dir(service);
105       }
106
107       DEBUG(3,("checking for home directory %s gave %s\n",service,
108             phome_dir?phome_dir:"(NULL)"));
109
110       if (phome_dir)
111       {   
112         int iHomeService;
113         pstrcpy(home_dir, phome_dir);
114         if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
115         {
116           lp_add_home(service,iHomeService,home_dir);
117           iService = lp_servicenumber(service);
118         }
119       }
120    }
121
122    /* If we still don't have a service, attempt to add it as a printer. */
123    if (iService < 0)
124    {
125       int iPrinterService;
126
127       if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
128       {
129          char *pszTemp;
130
131          DEBUG(3,("checking whether %s is a valid printer name...\n", service));
132          pszTemp = PRINTCAP;
133          if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
134          {
135             DEBUG(3,("%s is a valid printer name\n", service));
136             DEBUG(3,("adding %s as a printer service\n", service));
137             lp_add_printer(service,iPrinterService);
138             iService = lp_servicenumber(service);
139             if (iService < 0)
140                DEBUG(0,("failed to add %s as a printer service!\n", service));
141          }
142          else
143             DEBUG(3,("%s is not a valid printer name\n", service));
144       }
145    }
146
147    /* Check for default vfs service?  Unsure whether to implement this */
148    if (iService < 0)
149    {
150    }
151
152    /* just possibly it's a default service? */
153    if (iService < 0) 
154    {
155      char *pdefservice = lp_defaultservice();
156      if (pdefservice && *pdefservice && 
157          !strequal(pdefservice,service) &&
158          !strstr(service,".."))
159      {
160        /*
161         * We need to do a local copy here as lp_defaultservice() 
162         * returns one of the rotating lp_string buffers that
163         * could get overwritten by the recursive find_service() call
164         * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
165         */
166        pstring defservice;
167        pstrcpy(defservice, pdefservice);
168        iService = find_service(defservice);
169        if (iService >= 0)
170        {
171          string_sub(service,"_","/");
172          iService = lp_add_service(service,iService);
173        }
174      }
175    }
176
177    if (iService >= 0)
178      if (!VALID_SNUM(iService))
179      {
180        DEBUG(0,("Invalid snum %d for %s\n",iService,service));
181        iService = -1;
182      }
183
184    if (iService < 0)
185      DEBUG(3,("find_service() failed to find service %s\n", service));
186
187    return (iService);
188 }
189
190
191 /****************************************************************************
192   make a connection to a service
193 ****************************************************************************/
194 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
195 {
196         int snum;
197         const struct passwd *pass = NULL;
198         BOOL guest = False;
199         BOOL force = False;
200         extern int Client;
201         connection_struct *conn;
202
203         strlower(service);
204
205         snum = find_service(service);
206         if (snum < 0) {
207                 extern int Client;
208                 if (strequal(service,"IPC$")) {
209                         DEBUG(3,("refusing IPC connection\n"));
210                         *ecode = ERRnoipc;
211                         return NULL;
212                 }
213
214                 DEBUG(0,("%s (%s) couldn't find service %s\n",
215                          remote_machine, client_addr(Client), service));
216                 *ecode = ERRinvnetname;
217                 return NULL;
218         }
219
220         if (strequal(service,HOMES_NAME)) {
221                 if (*user && Get_Pwnam(user,True))
222                         return(make_connection(user,user,password,
223                                                pwlen,dev,vuid,ecode));
224
225                 if(lp_security() != SEC_SHARE) {
226                         if (validated_username(vuid)) {
227                                 pstrcpy(user,validated_username(vuid));
228                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
229                         }
230                 } else {
231                         /* Security = share. Try with sesssetup_user
232                          * as the username.  */
233                         if(*sesssetup_user) {
234                                 pstrcpy(user,sesssetup_user);
235                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
236                         }
237                 }
238         }
239
240         if (!lp_snum_ok(snum) || 
241             !check_access(Client, 
242                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
243                 *ecode = ERRaccess;
244                 return NULL;
245         }
246
247         /* you can only connect to the IPC$ service as an ipc device */
248         if (strequal(service,"IPC$"))
249                 pstrcpy(dev,"IPC");
250         
251         if (*dev == '?' || !*dev) {
252                 if (lp_print_ok(snum)) {
253                         pstrcpy(dev,"LPT1:");
254                 } else {
255                         pstrcpy(dev,"A:");
256                 }
257         }
258
259         /* if the request is as a printer and you can't print then refuse */
260         strupper(dev);
261         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
262                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
263                 *ecode = ERRinvdevice;
264                 return NULL;
265         }
266
267         /* lowercase the user name */
268         strlower(user);
269
270         /* add it as a possible user name */
271         add_session_user(service);
272
273         /* shall we let them in? */
274         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
275                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
276                 *ecode = ERRbadpw;
277                 return NULL;
278         }
279   
280         conn = conn_new();
281         if (!conn) {
282                 DEBUG(0,("Couldn't find free connection.\n"));
283                 *ecode = ERRnoresource;
284                 conn_free(conn);
285                 return NULL;
286         }
287
288         /* find out some info about the user */
289         pass = Get_Pwnam(user,True);
290
291         if (pass == NULL) {
292                 DEBUG(0,( "Couldn't find account %s\n",user));
293                 *ecode = ERRbaduid;
294                 conn_free(conn);
295                 return NULL;
296         }
297
298         conn->read_only = lp_readonly(snum);
299
300         {
301                 pstring list;
302                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
303                 string_sub(list,"%S",service);
304
305                 if (user_in_list(user,list))
306                         conn->read_only = True;
307                 
308                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
309                 string_sub(list,"%S",service);
310                 
311                 if (user_in_list(user,list))
312                         conn->read_only = False;    
313         }
314
315         /* admin user check */
316         
317         /* JRA - original code denied admin user if the share was
318            marked read_only. Changed as I don't think this is needed,
319            but old code left in case there is a problem here.
320         */
321         if (user_in_list(user,lp_admin_users(snum)) 
322 #if 0
323             && !conn->read_only
324 #endif
325             ) {
326                 conn->admin_user = True;
327                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
328         } else {
329                 conn->admin_user = False;
330         }
331     
332         conn->force_user = force;
333         conn->vuid = vuid;
334         conn->uid = pass->pw_uid;
335         conn->gid = pass->pw_gid;
336         conn->num_files_open = 0;
337         conn->lastused = time(NULL);
338         conn->service = snum;
339         conn->used = True;
340         conn->printer = (strncmp(dev,"LPT",3) == 0);
341         conn->ipc = (strncmp(dev,"IPC",3) == 0);
342         conn->dirptr = NULL;
343         conn->veto_list = NULL;
344         conn->hide_list = NULL;
345         conn->veto_oplock_list = NULL;
346         string_set(&conn->dirpath,"");
347         string_set(&conn->user,user);
348
349         /* Initialise VFS function pointers */
350
351         if (*lp_vfsobj(SNUM(conn))) {
352
353 #ifdef HAVE_LIBDL
354
355             /* Loadable object file */
356
357             if (!vfs_init_custom(conn)) {
358                 return NULL;
359             }
360 #else
361             DEBUG(0, ("No libdl present - cannot use VFS objects\n"));
362             conn_free(conn);
363             return NULL;
364 #endif
365
366         } else {
367
368             /* Normal share - initialise with disk access functions */
369
370             vfs_init_default(conn);
371         }
372
373 #ifdef HAVE_GETGRNAM 
374         if (*lp_force_group(snum)) {
375                 struct group *gptr;
376                 pstring gname;
377                 
378                 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
379                 /* default service may be a group name          */
380                 string_sub(gname,"%S",service);
381                 gptr = (struct group *)getgrnam(gname);
382                 
383                 if (gptr) {
384                         conn->gid = gptr->gr_gid;
385                         DEBUG(3,("Forced group %s\n",gname));
386                 } else {
387                         DEBUG(1,("Couldn't find group %s\n",gname));
388                 }
389         }
390 #endif
391         
392         if (*lp_force_user(snum)) {
393                 struct passwd *pass2;
394                 fstring fuser;
395                 fstrcpy(fuser,lp_force_user(snum));
396                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
397                 if (pass2) {
398                         conn->uid = pass2->pw_uid;
399                         string_set(&conn->user,fuser);
400                         fstrcpy(user,fuser);
401                         conn->force_user = True;
402                         DEBUG(3,("Forced user %s\n",fuser));      
403                 } else {
404                         DEBUG(1,("Couldn't find user %s\n",fuser));
405                 }
406         }
407
408         {
409                 pstring s;
410                 pstrcpy(s,lp_pathname(snum));
411                 standard_sub(conn,s);
412                 string_set(&conn->connectpath,s);
413                 DEBUG(3,("Connect path is %s\n",s));
414         }
415
416         /* groups stuff added by ih */
417         conn->ngroups = 0;
418         conn->groups = NULL;
419         
420         if (!IS_IPC(conn)) {
421                 /* Find all the groups this uid is in and
422                    store them. Used by become_user() */
423                 get_unixgroups(conn->user,conn->uid,conn->gid,
424                              &conn->ngroups,&conn->groups);
425                 
426                 /* check number of connections */
427                 if (!claim_connection(conn,
428                                       lp_servicename(SNUM(conn)),
429                                       lp_max_connections(SNUM(conn)),
430                                       False)) {
431                         DEBUG(1,("too many connections - rejected\n"));
432                         *ecode = ERRnoresource;
433                         conn_free(conn);
434                         return NULL;
435                 }  
436                 
437                 if (lp_status(SNUM(conn)))
438                         claim_connection(conn,"STATUS.",
439                                          MAXSTATUS,False);
440         } /* IS_IPC */
441         
442         /* execute any "root preexec = " line */
443         if (*lp_rootpreexec(SNUM(conn))) {
444                 pstring cmd;
445                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
446                 standard_sub(conn,cmd);
447                 DEBUG(5,("cmd=%s\n",cmd));
448                 smbrun(cmd,NULL,False);
449         }
450         
451         if (!become_user(conn, conn->vuid)) {
452                 DEBUG(0,("Can't become connected user!\n"));
453                 if (!IS_IPC(conn)) {
454                         yield_connection(conn,
455                                          lp_servicename(SNUM(conn)),
456                                          lp_max_connections(SNUM(conn)));
457                         if (lp_status(SNUM(conn))) {
458                                 yield_connection(conn,"STATUS.",MAXSTATUS);
459                         }
460                 }
461                 conn_free(conn);
462                 *ecode = ERRbadpw;
463                 return NULL;
464         }
465         
466         if (dos_ChDir(conn->connectpath) != 0) {
467                 DEBUG(0,("Can't change directory to %s (%s)\n",
468                          conn->connectpath,strerror(errno)));
469                 unbecome_user();
470                 if (!IS_IPC(conn)) {
471                         yield_connection(conn,
472                                          lp_servicename(SNUM(conn)),
473                                          lp_max_connections(SNUM(conn)));
474                         if (lp_status(SNUM(conn))) 
475                                 yield_connection(conn,"STATUS.",MAXSTATUS);
476                 }
477                 conn_free(conn);
478                 *ecode = ERRinvnetname;
479                 return NULL;
480         }
481         
482         string_set(&conn->origpath,conn->connectpath);
483         
484 #if SOFTLINK_OPTIMISATION
485         /* resolve any soft links early */
486         {
487                 pstring s;
488                 pstrcpy(s,conn->connectpath);
489                 dos_GetWd(s);
490                 string_set(&conn->connectpath,s);
491                 dos_ChDir(conn->connectpath);
492         }
493 #endif
494         
495         add_session_user(user);
496                 
497         /* execute any "preexec = " line */
498         if (*lp_preexec(SNUM(conn))) {
499                 pstring cmd;
500                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
501                 standard_sub(conn,cmd);
502                 smbrun(cmd,NULL,False);
503         }
504         
505         /* we've finished with the sensitive stuff */
506         unbecome_user();
507         
508         /* Add veto/hide lists */
509         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
510                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
511                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
512                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
513         }
514         
515         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
516                 extern int Client;
517                 
518                 dbgtext( "%s (%s) ", remote_machine, client_addr(Client) );
519                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)));
520                 dbgtext( "as user %s ", user );
521                 dbgtext( "(uid=%d, gid=%d) ", (int)conn->uid, (int)conn->gid );
522                 dbgtext( "(pid %d)\n", (int)getpid() );
523         }
524
525         /* Invoke make connection hook */
526
527         if (conn->vfs_ops.connect) {
528             struct vfs_connection_struct *vconn;
529
530             vconn = (struct vfs_connection_struct *)
531                 malloc(sizeof(struct vfs_connection_struct));
532
533             if (vconn == NULL) {
534                 DEBUG(0, ("No memory to create vfs_connection_struct"));
535                 return NULL;
536             }
537
538             ZERO_STRUCTP(vconn);
539
540             /* Copy across relevant data from connection struct */
541
542             vconn->printer = conn->printer;
543             vconn->ipc = conn->ipc;
544             vconn->read_only = conn->read_only;
545             vconn->admin_user = conn->admin_user;
546
547             pstrcpy(vconn->dirpath, conn->dirpath);
548             pstrcpy(vconn->connectpath, conn->connectpath);
549             pstrcpy(vconn->origpath, conn->origpath);
550
551             pstrcpy(vconn->user, conn->user);
552             vconn->uid = conn->uid;
553             vconn->gid = conn->gid;
554             vconn->ngroups = conn->ngroups;
555             vconn->groups = (gid_t *)malloc(conn->ngroups * sizeof(gid_t));
556             if (vconn->groups != NULL) {
557                 memcpy(vconn->groups, conn->groups, 
558                        conn->ngroups * sizeof(gid_t));
559             }
560
561             /* Call connect hook */
562             
563             if (conn->vfs_ops.connect(vconn, service, user) < 0) {
564                 return NULL;
565             }
566         }
567         
568         return(conn);
569 }
570
571
572 /****************************************************************************
573 close a cnum
574 ****************************************************************************/
575 void close_cnum(connection_struct *conn, uint16 vuid)
576 {
577         extern int Client;
578         DirCacheFlush(SNUM(conn));
579
580         unbecome_user();
581
582         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
583                                  remote_machine,client_addr(Client),
584                                  lp_servicename(SNUM(conn))));
585
586         if (conn->vfs_ops.disconnect != NULL) {
587
588             /* Call disconnect hook */
589             
590             conn->vfs_ops.disconnect();
591             
592             /* Free vfs_connection_struct */
593             
594             if (conn->vfs_conn != NULL) {
595                 if (conn->vfs_conn->groups != NULL) {
596                     free(conn->vfs_conn->groups);
597                 }
598                 free(conn->vfs_conn);
599             }
600         }
601
602         yield_connection(conn,
603                          lp_servicename(SNUM(conn)),
604                          lp_max_connections(SNUM(conn)));
605
606         if (lp_status(SNUM(conn)))
607                 yield_connection(conn,"STATUS.",MAXSTATUS);
608
609         file_close_conn(conn);
610         dptr_closecnum(conn);
611
612         /* execute any "postexec = " line */
613         if (*lp_postexec(SNUM(conn)) && 
614             become_user(conn, vuid))  {
615                 pstring cmd;
616                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
617                 standard_sub(conn,cmd);
618                 smbrun(cmd,NULL,False);
619                 unbecome_user();
620         }
621
622         unbecome_user();
623         /* execute any "root postexec = " line */
624         if (*lp_rootpostexec(SNUM(conn)))  {
625                 pstring cmd;
626                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
627                 standard_sub(conn,cmd);
628                 smbrun(cmd,NULL,False);
629         }
630         
631         conn_free(conn);
632 }
633
634