home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / smb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-13  |  4.5 KB  |  191 lines

  1. /*
  2.  *  smbmount.c
  3.  *
  4.  *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
  5.  *
  6.  *  Hacked by msf@redhat.com
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <pwd.h>
  14. #include <grp.h>
  15. #include <sys/socket.h>
  16. #include <sys/param.h>
  17. #include <netinet/in.h>
  18. #include <netdb.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. /* #include <sys/wait.h> */  /* generates a warning here */
  22. extern pid_t waitpid(pid_t, int *, int);
  23. #include <sys/errno.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <sys/mount.h>
  30. #include <mntent.h>
  31.  
  32. #include "dns.h"
  33. #include "smb_mount.h"
  34.  
  35. #ifndef MS_MGC_VAL
  36. #define MS_MGC_VAL 0xC0ED0000
  37. #endif
  38.  
  39. #define SMB_PORT 139
  40.  
  41. #include "log.h"
  42.  
  43.  
  44. /* probably have your own version of this? */
  45.  
  46. static void
  47. str_upper(char *name)
  48. {
  49.     while (*name)  {
  50.         *name = toupper(*name);
  51.         name = name + 1;
  52.     }
  53. }
  54.  
  55. /****************************************
  56.  Mount smb filesystem
  57.  
  58.  
  59.    server is name of SMB server - should NOT have domain name, etc 
  60.                                   as this is a NetBIOS name. For
  61.                                   machines running samba, is normally
  62.                                   the same as the hostname (sans domain)
  63.  
  64.    share is the name of the SMB share we are trying to mount
  65.  
  66.    user is the user to login as on SMB server.
  67.  
  68.    password is corresponding password.
  69.  
  70.    client is the NetBIOS name of machine attempting the mount. Just
  71.                  strip the hostname of domain to get this.
  72.  
  73.    mount_point - self explanatory
  74.  
  75. Way I'd do this - look at printtool (new one of course) and add a SMB printer.
  76.                   Ask for stuff just like I do.
  77.  
  78. End of docs 
  79. *****************************************/
  80.  
  81. int 
  82. smbmount(char *server, char *share, char *user, char *password, char *client, char *mount_point)
  83. {
  84.         struct smb_mount_data data;
  85.     char hostname[MAXHOSTNAMELEN + 1];
  86.  
  87.         int um;
  88.     unsigned int flags;
  89.  
  90.     /*        
  91.     struct stat st;
  92.     unsigned int flags;
  93.     */
  94.  
  95.     memset(&data, 0, sizeof(struct smb_mount_data));
  96.  
  97.     memset(hostname, '\0', MAXHOSTNAMELEN+1);
  98.     gethostname(hostname, MAXHOSTNAMELEN);
  99.  
  100.     data.version = SMB_MOUNT_VERSION;
  101.  
  102.         /* getuid() gives us the real uid, who may umount the fs */
  103.         data.mounted_uid = getuid();
  104.  
  105.     sprintf(data.service, "\\\\%s\\%s", server, share);
  106.         str_upper(data.service);
  107.  
  108.         strcpy(data.root_path, "/");
  109.     strcpy(data.username, user);
  110.  
  111.     /* msf - this doesn't make sense during the install */
  112. #if 0
  113.         if (getenv("USER")) {
  114.                 strcpy(data.username, getenv("USER"));
  115.                 str_upper(data.username);
  116.         }
  117.  
  118.         if (data.username[0] == 0 && getenv("LOGNAME"))
  119.         {
  120.                 strcpy(data.username,getenv("LOGNAME"));
  121.                 str_upper(data.username);
  122.         }
  123. #endif
  124.  
  125.         data.max_xmit = 4070;   /* allocate at most one page in kernel */
  126.         data.uid = getuid();
  127.         data.gid = getgid();
  128.         um = umask(0);
  129.         umask(um);
  130.         data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & ~um;
  131.         data.dir_mode  = 0;
  132.     data.addr.sin_family = AF_INET;
  133.     data.addr.sin_port = htons(SMB_PORT);
  134.  
  135.     /*    strcpy(data.domain, "?"); */
  136.  
  137.     if (mygethostbyname(server, &data.addr.sin_addr)) {
  138.       logMessage("%s: unknown host", server);
  139.       return -1;
  140.     }
  141.     
  142.     data.fd = socket(AF_INET, SOCK_STREAM, 0);
  143.     if (data.fd == -1) {
  144.         logMessage("smb socket: %s", strerror(errno));
  145.                 return -1;
  146.     }
  147.     
  148.  
  149.         if (data.dir_mode == 0) {
  150.                 data.dir_mode = data.file_mode;
  151.                 if ((data.dir_mode & S_IRUSR) != 0)
  152.                         data.dir_mode |= S_IXUSR;
  153.                 if ((data.dir_mode & S_IRGRP) != 0)
  154.                         data.dir_mode |= S_IXGRP;
  155.                 if ((data.dir_mode & S_IROTH) != 0)
  156.                         data.dir_mode |= S_IXOTH;
  157.         }
  158.  
  159.     strcpy(data.password, password);
  160.  
  161.     str_upper(data.password);
  162.  
  163.         if (data.server_name[0] == '\0')
  164.     {
  165.                 if (strlen(server) > sizeof(data.server_name)-1)
  166.         {
  167.                         logMessage("server name too long as a netbios name");
  168.                         return -1;
  169.                 }
  170.                 strcpy(data.server_name, server);
  171.                 str_upper(data.server_name);
  172.         }
  173.  
  174.     
  175.     strcpy(data.client_name, client);
  176.   
  177.     flags = MS_MGC_VAL;
  178.  
  179.     if (mount(NULL, mount_point, "smbfs",
  180.                   flags, (char *)&data) < 0) {
  181.         logMessage("smb mount error: %s", strerror(errno));
  182.             close(data.fd);
  183.         return -1;
  184.     }
  185.  
  186.         close(data.fd);
  187.  
  188.  
  189.     return 0;
  190. }    
  191.