- removed smb.conf.5.html as it now comes as part of htmldocs
[Samba/gbeck.git] / source / smbd / service.c
blobb4a1115867d76ef6216c14d16bc5067b8fe1dd14
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 extern int DEBUGLEVEL;
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;
38 /****************************************************************************
39 load parameters specific to a connection/service
40 ****************************************************************************/
41 BOOL become_service(connection_struct *conn,BOOL do_chdir)
43 extern char magic_char;
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;
54 snum = SNUM(conn);
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);
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 magic_char = lp_magicchar(snum);
75 use_mangled_map = (*lp_mangled_map(snum) ? True:False);
76 return(True);
80 /****************************************************************************
81 find a service entry
82 ****************************************************************************/
83 int find_service(char *service)
85 int iService;
87 string_sub(service,"\\","/");
89 iService = lp_servicenumber(service);
91 /* now handle the special case of a home directory */
92 if (iService < 0)
94 char *phome_dir = get_home_dir(service);
96 if(!phome_dir)
99 * Try mapping the servicename, it may
100 * be a Windows to unix mapped user name.
102 if(map_username(service))
103 phome_dir = get_home_dir(service);
106 DEBUG(3,("checking for home directory %s gave %s\n",service,
107 phome_dir?phome_dir:"(NULL)"));
109 if (phome_dir)
111 int iHomeService;
112 if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
114 lp_add_home(service,iHomeService,phome_dir);
115 iService = lp_servicenumber(service);
120 /* If we still don't have a service, attempt to add it as a printer. */
121 if (iService < 0)
123 int iPrinterService;
125 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
127 char *pszTemp;
129 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
130 pszTemp = PRINTCAP;
131 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
133 DEBUG(3,("%s is a valid printer name\n", service));
134 DEBUG(3,("adding %s as a printer service\n", service));
135 lp_add_printer(service,iPrinterService);
136 iService = lp_servicenumber(service);
137 if (iService < 0)
138 DEBUG(0,("failed to add %s as a printer service!\n", service));
140 else
141 DEBUG(3,("%s is not a valid printer name\n", service));
145 /* just possibly it's a default service? */
146 if (iService < 0)
148 char *pdefservice = lp_defaultservice();
149 if (pdefservice && *pdefservice && !strequal(pdefservice,service))
152 * We need to do a local copy here as lp_defaultservice()
153 * returns one of the rotating lp_string buffers that
154 * could get overwritten by the recursive find_service() call
155 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
157 pstring defservice;
158 pstrcpy(defservice, pdefservice);
159 iService = find_service(defservice);
160 if (iService >= 0)
162 string_sub(service,"_","/");
163 iService = lp_add_service(service,iService);
168 if (iService >= 0)
169 if (!VALID_SNUM(iService))
171 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
172 iService = -1;
175 if (iService < 0)
176 DEBUG(3,("find_service() failed to find service %s\n", service));
178 return (iService);
182 /****************************************************************************
183 make a connection to a service
184 ****************************************************************************/
185 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
187 int snum;
188 struct passwd *pass = NULL;
189 BOOL guest = False;
190 BOOL force = False;
191 extern int Client;
192 connection_struct *conn;
194 strlower(service);
196 snum = find_service(service);
197 if (snum < 0) {
198 extern int Client;
199 if (strequal(service,"IPC$")) {
200 DEBUG(3,("refusing IPC connection\n"));
201 *ecode = ERRnoipc;
202 return NULL;
205 DEBUG(0,("%s (%s) couldn't find service %s\n",
206 remote_machine, client_addr(Client), service));
207 *ecode = ERRinvnetname;
208 return NULL;
211 if (strequal(service,HOMES_NAME)) {
212 if (*user && Get_Pwnam(user,True))
213 return(make_connection(user,user,password,
214 pwlen,dev,vuid,ecode));
216 if(lp_security() != SEC_SHARE) {
217 if (validated_username(vuid)) {
218 pstrcpy(user,validated_username(vuid));
219 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
221 } else {
222 /* Security = share. Try with sesssetup_user
223 * as the username. */
224 if(*sesssetup_user) {
225 pstrcpy(user,sesssetup_user);
226 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
231 if (!lp_snum_ok(snum) ||
232 !check_access(Client,
233 lp_hostsallow(snum), lp_hostsdeny(snum))) {
234 *ecode = ERRaccess;
235 return NULL;
238 /* you can only connect to the IPC$ service as an ipc device */
239 if (strequal(service,"IPC$"))
240 pstrcpy(dev,"IPC");
242 if (*dev == '?' || !*dev) {
243 if (lp_print_ok(snum)) {
244 pstrcpy(dev,"LPT1:");
245 } else {
246 pstrcpy(dev,"A:");
250 /* if the request is as a printer and you can't print then refuse */
251 strupper(dev);
252 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
253 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
254 *ecode = ERRinvdevice;
255 return NULL;
258 /* lowercase the user name */
259 strlower(user);
261 /* add it as a possible user name */
262 add_session_user(service);
264 /* shall we let them in? */
265 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
266 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
267 *ecode = ERRbadpw;
268 return NULL;
271 conn = conn_new();
272 if (!conn) {
273 DEBUG(0,("Couldn't find free connection.\n"));
274 *ecode = ERRnoresource;
275 conn_free(conn);
276 return NULL;
279 /* find out some info about the user */
280 pass = Get_Pwnam(user,True);
282 if (pass == NULL) {
283 DEBUG(0,( "Couldn't find account %s\n",user));
284 *ecode = ERRbaduid;
285 conn_free(conn);
286 return NULL;
289 conn->read_only = lp_readonly(snum);
292 pstring list;
293 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
294 string_sub(list,"%S",service);
296 if (user_in_list(user,list))
297 conn->read_only = True;
299 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
300 string_sub(list,"%S",service);
302 if (user_in_list(user,list))
303 conn->read_only = False;
306 /* admin user check */
308 /* JRA - original code denied admin user if the share was
309 marked read_only. Changed as I don't think this is needed,
310 but old code left in case there is a problem here.
312 if (user_in_list(user,lp_admin_users(snum))
313 #if 0
314 && !conn->read_only
315 #endif
317 conn->admin_user = True;
318 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
319 } else {
320 conn->admin_user = False;
323 conn->force_user = force;
324 conn->vuid = vuid;
325 conn->uid = pass->pw_uid;
326 conn->gid = pass->pw_gid;
327 conn->num_files_open = 0;
328 conn->lastused = time(NULL);
329 conn->service = snum;
330 conn->used = True;
331 conn->printer = (strncmp(dev,"LPT",3) == 0);
332 conn->ipc = (strncmp(dev,"IPC",3) == 0);
333 conn->dirptr = NULL;
334 conn->veto_list = NULL;
335 conn->hide_list = NULL;
336 conn->veto_oplock_list = NULL;
337 string_set(&conn->dirpath,"");
338 string_set(&conn->user,user);
340 #ifdef HAVE_GETGRNAM
341 if (*lp_force_group(snum)) {
342 struct group *gptr;
343 pstring gname;
345 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
346 /* default service may be a group name */
347 string_sub(gname,"%S",service);
348 gptr = (struct group *)getgrnam(gname);
350 if (gptr) {
351 conn->gid = gptr->gr_gid;
352 DEBUG(3,("Forced group %s\n",gname));
353 } else {
354 DEBUG(1,("Couldn't find group %s\n",gname));
357 #endif
359 if (*lp_force_user(snum)) {
360 struct passwd *pass2;
361 fstring fuser;
362 fstrcpy(fuser,lp_force_user(snum));
363 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
364 if (pass2) {
365 conn->uid = pass2->pw_uid;
366 string_set(&conn->user,fuser);
367 fstrcpy(user,fuser);
368 conn->force_user = True;
369 DEBUG(3,("Forced user %s\n",fuser));
370 } else {
371 DEBUG(1,("Couldn't find user %s\n",fuser));
376 pstring s;
377 pstrcpy(s,lp_pathname(snum));
378 standard_sub(conn,s);
379 string_set(&conn->connectpath,s);
380 DEBUG(3,("Connect path is %s\n",s));
383 /* groups stuff added by ih */
384 conn->ngroups = 0;
385 conn->groups = NULL;
387 if (!IS_IPC(conn)) {
388 /* Find all the groups this uid is in and
389 store them. Used by become_user() */
390 get_unixgroups(conn->user,conn->uid,conn->gid,
391 &conn->ngroups,&conn->groups);
393 /* check number of connections */
394 if (!claim_connection(conn,
395 lp_servicename(SNUM(conn)),
396 lp_max_connections(SNUM(conn)),
397 False)) {
398 DEBUG(1,("too many connections - rejected\n"));
399 *ecode = ERRnoresource;
400 conn_free(conn);
401 return NULL;
404 if (lp_status(SNUM(conn)))
405 claim_connection(conn,"STATUS.",
406 MAXSTATUS,False);
407 } /* IS_IPC */
409 /* execute any "root preexec = " line */
410 if (*lp_rootpreexec(SNUM(conn))) {
411 pstring cmd;
412 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
413 standard_sub(conn,cmd);
414 DEBUG(5,("cmd=%s\n",cmd));
415 smbrun(cmd,NULL,False);
418 if (!become_user(conn, conn->vuid)) {
419 DEBUG(0,("Can't become connected user!\n"));
420 if (!IS_IPC(conn)) {
421 yield_connection(conn,
422 lp_servicename(SNUM(conn)),
423 lp_max_connections(SNUM(conn)));
424 if (lp_status(SNUM(conn))) {
425 yield_connection(conn,"STATUS.",MAXSTATUS);
428 conn_free(conn);
429 *ecode = ERRbadpw;
430 return NULL;
433 if (dos_ChDir(conn->connectpath) != 0) {
434 DEBUG(0,("Can't change directory to %s (%s)\n",
435 conn->connectpath,strerror(errno)));
436 unbecome_user();
437 if (!IS_IPC(conn)) {
438 yield_connection(conn,
439 lp_servicename(SNUM(conn)),
440 lp_max_connections(SNUM(conn)));
441 if (lp_status(SNUM(conn)))
442 yield_connection(conn,"STATUS.",MAXSTATUS);
444 conn_free(conn);
445 *ecode = ERRinvnetname;
446 return NULL;
449 string_set(&conn->origpath,conn->connectpath);
451 #if SOFTLINK_OPTIMISATION
452 /* resolve any soft links early */
454 pstring s;
455 pstrcpy(s,conn->connectpath);
456 dos_GetWd(s);
457 string_set(&conn->connectpath,s);
458 dos_ChDir(conn->connectpath);
460 #endif
462 add_session_user(user);
464 /* execute any "preexec = " line */
465 if (*lp_preexec(SNUM(conn))) {
466 pstring cmd;
467 pstrcpy(cmd,lp_preexec(SNUM(conn)));
468 standard_sub(conn,cmd);
469 smbrun(cmd,NULL,False);
472 /* we've finished with the sensitive stuff */
473 unbecome_user();
475 /* Add veto/hide lists */
476 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
477 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
478 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
479 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
482 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
483 extern int Client;
485 dbgtext( "%s (%s) ", remote_machine, client_addr(Client) );
486 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
487 dbgtext( "as user %s ", user );
488 dbgtext( "(uid=%d, gid=%d) ", (int)conn->uid, (int)conn->gid );
489 dbgtext( "(pid %d)\n", (int)getpid() );
492 return(conn);
496 /****************************************************************************
497 close a cnum
498 ****************************************************************************/
499 void close_cnum(connection_struct *conn, uint16 vuid)
501 extern int Client;
502 DirCacheFlush(SNUM(conn));
504 unbecome_user();
506 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
507 remote_machine,client_addr(Client),
508 lp_servicename(SNUM(conn))));
510 yield_connection(conn,
511 lp_servicename(SNUM(conn)),
512 lp_max_connections(SNUM(conn)));
514 if (lp_status(SNUM(conn)))
515 yield_connection(conn,"STATUS.",MAXSTATUS);
517 file_close_conn(conn);
518 dptr_closecnum(conn);
520 /* execute any "postexec = " line */
521 if (*lp_postexec(SNUM(conn)) &&
522 become_user(conn, vuid)) {
523 pstring cmd;
524 pstrcpy(cmd,lp_postexec(SNUM(conn)));
525 standard_sub(conn,cmd);
526 smbrun(cmd,NULL,False);
527 unbecome_user();
530 unbecome_user();
531 /* execute any "root postexec = " line */
532 if (*lp_rootpostexec(SNUM(conn))) {
533 pstring cmd;
534 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
535 standard_sub(conn,cmd);
536 smbrun(cmd,NULL,False);
539 conn_free(conn);