merging for 2.2.6pre1
[Samba.git] / source / smbd / service.c
blob4a02967866b8980e3a571006593f6996bd494727
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 service (connection) opening and closing
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
22 #include "includes.h"
24 #define CHECK_PATH_ON_TCONX 1
26 extern struct timeval 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 userdom_struct current_user_info;
35 extern fstring remote_machine;
38 /****************************************************************************
39 Load parameters specific to a connection/service.
40 ****************************************************************************/
42 BOOL set_current_service(connection_struct *conn,BOOL do_chdir)
44 static connection_struct *last_conn;
45 int snum;
47 if (!conn) {
48 last_conn = NULL;
49 return(False);
52 conn->lastused = smb_last_time.tv_sec;
54 snum = SNUM(conn);
56 if (do_chdir &&
57 vfs_ChDir(conn,conn->connectpath) != 0 &&
58 vfs_ChDir(conn,conn->origpath) != 0) {
59 DEBUG(0,("chdir (%s) failed\n",
60 conn->connectpath));
61 return(False);
64 if (conn == last_conn)
65 return(True);
67 last_conn = conn;
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 use_mangled_map = (*lp_mangled_map(snum) ? True:False);
75 return(True);
78 /****************************************************************************
79 Add a home service. Returns the new service number or -1 if fail.
80 ****************************************************************************/
82 int add_home_service(char *service, char *homedir)
84 int iHomeService;
85 int iService;
86 fstring new_service;
87 char *usr_p = NULL;
89 if (!service || !homedir)
90 return -1;
92 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
93 return -1;
96 * If this is a winbindd provided username, remove
97 * the domain component before adding the service.
98 * Log a warning if the "path=" parameter does not
99 * include any macros.
102 fstrcpy(new_service, service);
104 if ((usr_p = strchr(service,*lp_winbind_separator())) != NULL)
105 fstrcpy(new_service, usr_p+1);
107 lp_add_home(new_service,iHomeService,homedir);
108 iService = lp_servicenumber(new_service);
110 return iService;
113 /****************************************************************************
114 Find a service entry. service is always in dos codepage.
115 ****************************************************************************/
117 int find_service(char *service)
119 int iService;
121 all_string_sub(service,"\\","/",0);
123 iService = lp_servicenumber(service);
125 /* now handle the special case of a home directory */
126 if (iService < 0)
128 char *phome_dir = get_user_service_home_dir(service);
130 if(!phome_dir)
133 * Try mapping the servicename, it may
134 * be a Windows to unix mapped user name.
136 if(map_username(service))
137 phome_dir = get_user_service_home_dir(service);
140 DEBUG(3,("checking for home directory %s gave %s\n",service,
141 phome_dir?phome_dir:"(NULL)"));
143 iService = add_home_service(service,phome_dir);
146 /* If we still don't have a service, attempt to add it as a printer. */
147 if (iService < 0)
149 int iPrinterService;
151 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
153 char *pszTemp;
155 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
156 pszTemp = PRINTCAP;
157 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
159 DEBUG(3,("%s is a valid printer name\n", service));
160 DEBUG(3,("adding %s as a printer service\n", service));
161 lp_add_printer(service,iPrinterService);
162 iService = lp_servicenumber(service);
163 if (iService < 0)
164 DEBUG(0,("failed to add %s as a printer service!\n", service));
166 else
167 DEBUG(3,("%s is not a valid printer name\n", service));
171 /* Check for default vfs service? Unsure whether to implement this */
172 if (iService < 0)
176 /* just possibly it's a default service? */
177 if (iService < 0)
179 char *pdefservice = lp_defaultservice();
180 if (pdefservice && *pdefservice &&
181 !strequal(pdefservice,service) &&
182 !strstr(service,".."))
185 * We need to do a local copy here as lp_defaultservice()
186 * returns one of the rotating lp_string buffers that
187 * could get overwritten by the recursive find_service() call
188 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
190 pstring defservice;
191 pstrcpy(defservice, pdefservice);
192 iService = find_service(defservice);
193 if (iService >= 0)
195 all_string_sub(service,"_","/",0);
196 iService = lp_add_service(service,iService);
201 if (iService >= 0)
202 if (!VALID_SNUM(iService))
204 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
205 iService = -1;
208 if (iService < 0)
209 DEBUG(3,("find_service() failed to find service %s\n", service));
211 return (iService);
215 /****************************************************************************
216 Make a connection to a service. This function is designed to be called
217 AS ROOT and will return to being root on exit ! Modified current_user conn
218 and vuid elements.
219 ****************************************************************************/
221 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
223 int snum;
224 struct passwd *pass = NULL;
225 BOOL guest = False;
226 BOOL force = False;
227 connection_struct *conn;
228 #if !CHECK_PATH_ON_TCONX
229 struct stat st;
230 #endif
231 uid_t euid;
232 int ret;
234 /* This must ONLY BE CALLED AS ROOT. As it exits this function as root. */
236 if (!non_root_mode() && ((euid = geteuid()) != 0)) {
237 DEBUG(0,("make_connection: PANIC ERROR. Called as nonroot (%u)\n", (unsigned int)euid ));
238 smb_panic("make_connection: PANIC ERROR. Called as nonroot\n");
241 strlower(service);
243 snum = find_service(service);
244 if (snum < 0) {
245 if (strequal(service,"IPC$") || strequal(service,"ADMIN$")) {
246 DEBUG(3,("refusing IPC connection\n"));
247 *ecode = ERRnoipc;
248 return NULL;
251 DEBUG(0,("%s (%s) couldn't find service %s\n",
252 remote_machine, client_addr(), service));
253 *ecode = ERRnosuchshare;
254 return NULL;
257 if (strequal(service,HOMES_NAME)) {
258 if (*user && Get_Pwnam(user,True)) {
259 fstring dos_username;
260 fstrcpy(dos_username, user);
261 unix_to_dos(dos_username);
262 return(make_connection(dos_username,user,password,
263 pwlen,dev,vuid,ecode));
266 if(lp_security() != SEC_SHARE) {
267 if (validated_username(vuid)) {
268 fstring dos_username;
269 fstrcpy(user,validated_username(vuid));
270 fstrcpy(dos_username, user);
271 unix_to_dos(dos_username);
272 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
274 } else {
275 /* Security = share. Try with current_user_info.smb_name
276 * as the username. */
277 if(*current_user_info.smb_name) {
278 fstring dos_username;
279 fstrcpy(user,current_user_info.smb_name);
280 fstrcpy(dos_username, user);
281 unix_to_dos(dos_username);
282 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
287 if (!lp_snum_ok(snum) ||
288 !check_access(smbd_server_fd(),
289 lp_hostsallow(snum), lp_hostsdeny(snum))) {
290 *ecode = ERRaccess;
291 return NULL;
294 /* you can only connect to the IPC$ service as an ipc device */
295 if (strequal(service,"IPC$") || strequal(service,"ADMIN$"))
296 pstrcpy(dev,"IPC");
298 if (*dev == '?' || !*dev) {
299 if (lp_print_ok(snum)) {
300 pstrcpy(dev,"LPT1:");
301 } else {
302 pstrcpy(dev,"A:");
306 /* if the request is as a printer and you can't print then refuse */
307 strupper(dev);
308 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
309 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
310 *ecode = ERRinvdevice;
311 return NULL;
314 /* Behave as a printer if we are supposed to */
315 if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
316 pstrcpy(dev, "LPT1:");
319 /* lowercase the user name */
320 strlower(user);
322 /* add it as a possible user name if we
323 are in share mode security */
324 if (lp_security() == SEC_SHARE) {
325 add_session_user(service);
329 /* shall we let them in? */
330 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
331 DEBUG( 2, ( "Invalid username/password for %s [%s]\n", service, user ) );
332 *ecode = ERRbadpw;
333 return NULL;
336 add_session_user(user);
338 conn = conn_new();
339 if (!conn) {
340 DEBUG(0,("Couldn't find free connection.\n"));
341 *ecode = ERRnoresource;
342 return NULL;
345 /* find out some info about the user */
346 pass = smb_getpwnam(user,True);
348 if (pass == NULL) {
349 DEBUG(0,( "Couldn't find account %s\n",user));
350 *ecode = ERRbaduid;
351 conn_free(conn);
352 return NULL;
355 conn->read_only = lp_readonly(snum);
358 pstring list;
359 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
360 pstring_sub(list,"%S",service);
362 if (user_in_list(user,list))
363 conn->read_only = True;
365 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
366 pstring_sub(list,"%S",service);
368 if (user_in_list(user,list))
369 conn->read_only = False;
372 /* admin user check */
374 /* JRA - original code denied admin user if the share was
375 marked read_only. Changed as I don't think this is needed,
376 but old code left in case there is a problem here.
378 if (user_in_list(user,lp_admin_users(snum))
379 #if 0
380 && !conn->read_only
381 #endif
383 conn->admin_user = True;
384 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
385 } else {
386 conn->admin_user = False;
389 conn->force_user = force;
390 conn->vuid = vuid;
391 conn->uid = pass->pw_uid;
392 conn->gid = pass->pw_gid;
393 safe_strcpy(conn->client_address, client_addr(), sizeof(conn->client_address)-1);
394 conn->num_files_open = 0;
395 conn->lastused = time(NULL);
396 conn->service = snum;
397 conn->used = True;
398 conn->printer = (strncmp(dev,"LPT",3) == 0);
399 conn->ipc = ((strncmp(dev,"IPC",3) == 0) || strequal(dev,"ADMIN$"));
400 conn->dirptr = NULL;
401 conn->veto_list = NULL;
402 conn->hide_list = NULL;
403 conn->veto_oplock_list = NULL;
404 string_set(&conn->dirpath,"");
405 string_set(&conn->user,user);
406 conn->nt_user_token = NULL;
409 * If force user is true, then store the
410 * given userid and also the primary groupid
411 * of the user we're forcing.
414 if (*lp_force_user(snum)) {
415 struct passwd *pass2;
416 pstring fuser;
417 pstrcpy(fuser,lp_force_user(snum));
419 /* Allow %S to be used by force user. */
420 pstring_sub(fuser,"%S",service);
422 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
423 if (pass2) {
424 conn->uid = pass2->pw_uid;
425 conn->gid = pass2->pw_gid;
426 string_set(&conn->user,fuser);
427 fstrcpy(user,fuser);
428 conn->force_user = True;
429 DEBUG(3,("Forced user %s\n",fuser));
430 } else {
431 DEBUG(1,("Couldn't find user %s\n",fuser));
435 /* admin users always run as uid=0 */
436 if (conn->admin_user) {
437 conn->uid = 0;
440 #ifdef HAVE_GETGRNAM
442 * If force group is true, then override
443 * any groupid stored for the connecting user.
446 if (*lp_force_group(snum)) {
447 gid_t gid;
448 pstring gname;
449 pstring tmp_gname;
450 BOOL user_must_be_member = False;
452 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
454 if (tmp_gname[0] == '+') {
455 user_must_be_member = True;
456 StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
457 } else {
458 StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
460 /* default service may be a group name */
461 pstring_sub(gname,"%S",service);
462 gid = nametogid(gname);
464 if (gid != (gid_t)-1) {
466 * If the user has been forced and the forced group starts
467 * with a '+', then we only set the group to be the forced
468 * group if the forced user is a member of that group.
469 * Otherwise, the meaning of the '+' would be ignored.
471 if (conn->force_user && user_must_be_member) {
472 if (user_in_group_list( user, gname )) {
473 conn->gid = gid;
474 DEBUG(3,("Forced group %s for member %s\n",gname,user));
476 } else {
477 conn->gid = gid;
478 DEBUG(3,("Forced group %s\n",gname));
480 } else {
481 DEBUG(1,("Couldn't find group %s\n",gname));
484 #endif /* HAVE_GETGRNAM */
487 pstring s;
488 pstrcpy(s,lp_pathname(snum));
489 standard_sub_conn(conn,s,sizeof(s));
490 string_set(&conn->connectpath,s);
491 DEBUG(3,("Connect path is %s\n",s));
494 /* groups stuff added by ih */
495 conn->ngroups = 0;
496 conn->groups = NULL;
498 /* Find all the groups this uid is in and
499 store them. Used by change_to_user() */
500 initialise_groups(conn->user, conn->uid, conn->gid);
501 get_current_groups(conn->gid, &conn->ngroups,&conn->groups);
503 /* check number of connections */
504 if (!claim_connection(conn,
505 lp_servicename(SNUM(conn)),
506 lp_max_connections(SNUM(conn)),
507 False)) {
508 DEBUG(1,("too many connections - rejected\n"));
509 *ecode = ERRnoresource;
510 conn_free(conn);
511 return NULL;
514 conn->nt_user_token = create_nt_token(conn->uid, conn->gid,
515 conn->ngroups, conn->groups,
516 guest, NULL);
519 * New code to check if there's a share security descripter
520 * added from NT server manager. This is done after the
521 * smb.conf checks are done as we need a uid and token. JRA.
525 BOOL can_write = share_access_check(conn, snum, vuid, FILE_WRITE_DATA);
527 if (!can_write) {
528 if (!share_access_check(conn, snum, vuid, FILE_READ_DATA)) {
529 /* No access, read or write. */
530 *ecode = ERRaccess;
531 DEBUG(0,( "make_connection: connection to %s denied due to security descriptor.\n",
532 service ));
533 yield_connection(conn, lp_servicename(SNUM(conn)));
534 conn_free(conn);
535 return NULL;
536 } else {
537 conn->read_only = True;
541 /* Initialise VFS function pointers */
543 if (!smbd_vfs_init(conn)) {
544 DEBUG(0, ("smbd_vfs_init failed for service %s\n", lp_servicename(SNUM(conn))));
545 yield_connection(conn, lp_servicename(SNUM(conn)));
546 conn_free(conn);
547 return NULL;
550 /* execute any "root preexec = " line */
551 if (*lp_rootpreexec(SNUM(conn))) {
552 pstring cmd;
553 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
554 standard_sub_conn(conn,cmd,sizeof(cmd));
555 DEBUG(5,("cmd=%s\n",cmd));
556 ret = smbrun(cmd,NULL);
557 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
558 DEBUG(1,("preexec gave %d - failing connection\n", ret));
559 yield_connection(conn, lp_servicename(SNUM(conn)));
560 conn_free(conn);
561 *ecode = ERRsrverror;
562 return NULL;
566 if (!change_to_user(conn, conn->vuid)) {
567 DEBUG(0,("Can't become connected user!\n"));
568 yield_connection(conn, lp_servicename(SNUM(conn)));
569 conn_free(conn);
570 *ecode = ERRbadpw;
571 return NULL;
574 /* execute any "preexec = " line */
575 if (*lp_preexec(SNUM(conn))) {
576 pstring cmd;
577 pstrcpy(cmd,lp_preexec(SNUM(conn)));
578 standard_sub_conn(conn,cmd,sizeof(cmd));
579 ret = smbrun(cmd,NULL);
580 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
581 DEBUG(1,("preexec gave %d - failing connection\n", ret));
582 yield_connection(conn, lp_servicename(SNUM(conn)));
583 conn_free(conn);
584 *ecode = ERRsrverror;
585 return NULL;
590 * FIXME!!!! Reenabled this code since it current;y breaks
591 * move_driver_to_download_area() by keeping the root path
592 * of the connection at /tmp. I'll work on a real fix, but this
593 * will keep people happy for a temporary meaure. --jerry
595 #if CHECK_PATH_ON_TCONX
596 /* win2000 does not check the permissions on the directory
597 during the tree connect, instead relying on permission
598 check during individual operations. To match this behaviour
599 I have disabled this chdir check (tridge) */
600 if (vfs_ChDir(conn,conn->connectpath) != 0) {
601 DEBUG(0,("%s (%s) Can't change directory to %s (%s)\n",
602 remote_machine, conn->client_address,
603 conn->connectpath,strerror(errno)));
604 change_to_root_user();
605 yield_connection(conn, lp_servicename(SNUM(conn)));
606 conn_free(conn);
607 *ecode = ERRnosuchshare;
608 return NULL;
610 #else
611 /* the alternative is just to check the directory exists */
612 if (stat(conn->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
613 DEBUG(0,("%s is not a directory\n", conn->connectpath));
614 change_to_root_user();
615 yield_connection(conn, lp_servicename(SNUM(conn)));
616 conn_free(conn);
617 *ecode = ERRnosuchshare;
618 return NULL;
620 #endif
622 string_set(&conn->origpath,conn->connectpath);
624 #if SOFTLINK_OPTIMISATION
625 /* resolve any soft links early if possible */
626 if (vfs_ChDir(conn,conn->connectpath) == 0) {
627 pstring s;
628 pstrcpy(s,conn->connectpath);
629 vfs_GetWd(conn,s);
630 string_set(&conn->connectpath,s);
631 vfs_ChDir(conn,conn->connectpath);
633 #endif
636 * Print out the 'connected as' stuff here as we need
637 * to know the effective uid and gid we will be using.
640 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
641 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
642 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
643 dbgtext( "as user %s ", user );
644 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
645 dbgtext( "(pid %d)\n", (int)sys_getpid() );
648 /* we've finished with the sensitive stuff */
649 change_to_root_user();
651 /* Add veto/hide lists */
652 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
653 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
654 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
655 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
658 /* Invoke VFS make connection hook */
660 if (conn->vfs_ops.connect) {
661 DEBUG(10,("calling vfs_ops.connect for service %s (options = %s)\n", service, lp_vfs_options(SNUM(conn)) ));
662 if (conn->vfs_ops.connect(conn, service, user) < 0)
663 return NULL;
666 return(conn);
669 /****************************************************************************
670 Close a cnum
671 ****************************************************************************/
673 void close_cnum(connection_struct *conn, uint16 vuid)
675 DirCacheFlush(SNUM(conn));
677 change_to_root_user();
679 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
680 remote_machine,conn->client_address,
681 lp_servicename(SNUM(conn))));
683 if (conn->vfs_ops.disconnect != NULL) {
685 /* Call VFS disconnect hook */
687 conn->vfs_ops.disconnect(conn);
691 yield_connection(conn, lp_servicename(SNUM(conn)));
693 file_close_conn(conn);
694 dptr_closecnum(conn);
696 /* execute any "postexec = " line */
697 if (*lp_postexec(SNUM(conn)) &&
698 change_to_user(conn, vuid)) {
699 pstring cmd;
700 pstrcpy(cmd,lp_postexec(SNUM(conn)));
701 standard_sub_conn(conn,cmd,sizeof(cmd));
702 smbrun(cmd,NULL);
705 change_to_root_user();
706 /* execute any "root postexec = " line */
707 if (*lp_rootpostexec(SNUM(conn))) {
708 pstring cmd;
709 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
710 standard_sub_conn(conn,cmd,sizeof(cmd));
711 smbrun(cmd,NULL);
714 /* make sure we leave the directory available for unmount */
715 vfs_ChDir(conn, "/");
717 conn_free(conn);