mount.cifs patch: cleaner compile, plus -A cred_file option

Ronan Waide waider at waider.ie
Fri Dec 12 13:03:23 GMT 2003


This cleans up some warnings in the mount.cifs compile and adds a "-A
credentials_file" option. The credentials parsing code is ripped
straight from mount.smbfs, if I recall correctly. I wrote this some
months ago and forgot to post it, but it /is/ up to date with respect
to current CVS (3.0.1rc2).

Cheers,
Waider.

Index: source/client/mount.cifs.c
===================================================================
RCS file: /cvsroot/samba/source/client/mount.cifs.c,v
retrieving revision 1.3.2.5
diff -c -r1.3.2.5 mount.cifs.c
*** source/client/mount.cifs.c	7 Nov 2003 15:52:18 -0000	1.3.2.5
--- source/client/mount.cifs.c	12 Dec 2003 13:02:42 -0000
***************
*** 1,4 ****
--- 1,6 ----
+ #ifndef _GNU_SOURCE /* defined by compiler anyway? */
  #define _GNU_SOURCE
+ #endif
  
  #include <stdlib.h>
  #include <unistd.h>
***************
*** 14,19 ****
--- 16,22 ----
  #include <netdb.h>
  #include <string.h>
  #include <mntent.h>
+ #include <stdio.h>
  
  #define MOUNT_CIFS_VERSION "1"
  
***************
*** 40,45 ****
--- 43,52 ----
          call system(umount argv) etc.
                  
  BB end finish BB */
+ void mount_cifs_usage(void);
+ char *getusername(void);
+ char * parse_cifs_url(char *unc_name);
+ static void read_credentials_file(char *filename, char *username, char *password);
  
  void mount_cifs_usage()
  {
***************
*** 61,69 ****
  	return username;
  }
  
! char * parse_cifs_url(unc_name)
  {
  	printf("\ncifs url %s\n",unc_name);
  }
  
  int parse_options(char * options)
--- 68,77 ----
  	return username;
  }
  
! char * parse_cifs_url(char *unc_name)
  {
  	printf("\ncifs url %s\n",unc_name);
+ 	return unc_name; /* XXX */
  }
  
  int parse_options(char * options)
***************
*** 226,233 ****
  	char * ipaddress_string = NULL;
  	struct hostent * host_entry;
  	struct in_addr server_ipaddr;
! 	int rc,j;
! 	char temp[64];
  
  	if(length > 1023) {
  		printf("mount error: UNC name too long");
--- 234,240 ----
  	char * ipaddress_string = NULL;
  	struct hostent * host_entry;
  	struct in_addr server_ipaddr;
! 	int rc;
  
  	if(length > 1023) {
  		printf("mount error: UNC name too long");
***************
*** 330,336 ****
  	char * mountpoint;
  	char * options;
  	char * temp;
! 	int rc,i;
  	int rsize = 0;
  	int wsize = 0;
  	int nomtab = 0;
--- 337,343 ----
  	char * mountpoint;
  	char * options;
  	char * temp;
! 	int rc;
  	int rsize = 0;
  	int wsize = 0;
  	int nomtab = 0;
***************
*** 363,371 ****
  	mountpoint = argv[2];
  	/* add sharename in opts string as unc= parm */
  
! 	while ((c = getopt_long (argc, argv, "afFhilL:no:O:rsU:vVwt:",
  			 longopts, NULL)) != -1) {
  		switch (c) {
  /*	case 'a':	       
  		++mount_all;
  		break;
--- 370,383 ----
  	mountpoint = argv[2];
  	/* add sharename in opts string as unc= parm */
  
! 	while ((c = getopt_long (argc, argv, "A:afFhilL:no:O:rsU:vVwt:",
  			 longopts, NULL)) != -1) {
  		switch (c) {
+ 		case 'A':
+ 		  user_name = malloc( 128 );
+ 		  mountpassword = malloc( 128 );
+ 		  read_credentials_file(optarg, user_name, mountpassword);
+ 		  break;
  /*	case 'a':	       
  		++mount_all;
  		break;
***************
*** 490,496 ****
  	}
  
  	if((getuid() != 0) && (geteuid() == 0)) {
! 		if((statbuf.st_uid == getuid()) && (S_IRWXU == statbuf.st_mode & S_IRWXU)) {
  			printf("setuid mount allowed\n");
  		} else {
  			printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n"); 
--- 502,508 ----
  	}
  
  	if((getuid() != 0) && (geteuid() == 0)) {
! 		if((statbuf.st_uid == getuid()) && (S_IRWXU == ( statbuf.st_mode & S_IRWXU))) {
  			printf("setuid mount allowed\n");
  		} else {
  			printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n"); 
***************
*** 599,601 ****
--- 611,672 ----
  	return 0;
  }
  
+ /****************************************************************************
+ get username and password from a credentials file
+ exit on failure (from smbclient, move to libsmb or shared .c file?)
+ ****************************************************************************/
+ static void read_credentials_file(char *filename, char *username, char *password)
+ {
+ 	FILE *auth;
+ 	char buf[128];
+ 	unsigned short len = 0;
+ 	char *ptr, *val, *param;
+ 
+ 	if ((auth=fopen(filename, "r")) == NULL)
+ 	{
+ 		/* fail if we can't open the credentials file */
+ 	  fprintf(stderr, "ERROR: Unable to open credentials file!\n");
+ 		exit (-1);
+ 	}
+ 
+ 	while (!feof(auth))
+ 	{
+ 		/* get a line from the file */
+ 		if (!fgets (buf, sizeof(buf), auth))
+ 			continue;
+ 		len = strlen(buf);
+ 
+ 		if ((len) && (buf[len-1]=='\n'))
+ 		{
+ 			buf[len-1] = '\0';
+ 			len--;
+ 		}
+ 		if (len == 0)
+ 			continue;
+ 
+ 		/* break up the line into parameter & value.
+ 		   will need to eat a little whitespace possibly */
+ 		param = buf;
+ 		if (!(ptr = strchr (buf, '=')))
+ 			continue;
+ 		val = ptr+1;
+ 		*ptr = '\0';
+ 
+ 		/* eat leading white space */
+ 		while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
+ 			val++;
+ 
+ 		if (strcasecmp("password", param) == 0)
+ 		{
+ 			strncpy(password, val,128);
+ 			got_password = 1;
+ 		}
+ 		else if (strcasecmp("username", param) == 0) {
+ 			strncpy(username, val,128);
+ 			got_user = 1;
+ 		}
+ 
+ 		memset(buf, 0, sizeof(buf));
+ 	}
+ 	fclose(auth);
+ }

-- 
waider at waider.ie / Yes, it /is/ very personal of me.
"Well, from what google can find, xenon hexafluoride is useful for two
 things. Serving as something for chemists to talk about, and making quartz
 detonate." - Someone on /.


More information about the samba-technical mailing list