]> granicus.if.org Git - curl/commitdiff
Add two new options for the SFTP/SCP/FILE protocols: CURLOPT_NEW_FILE_PERMS
authorJames Housley <jim@thehousleys.net>
Wed, 27 Jun 2007 20:15:48 +0000 (20:15 +0000)
committerJames Housley <jim@thehousleys.net>
Wed, 27 Jun 2007 20:15:48 +0000 (20:15 +0000)
and CURLOPT_NEW_DIRECTORY_PERMS.  These control the premissions for files
and directories created on the remote server.  CURLOPT_NEW_FILE_PERMS
defaults to 0644 and CURLOPT_NEW_DIRECTORY_PERMS defaults to 0755

docs/libcurl/curl_easy_setopt.3
include/curl/curl.h
lib/easy.c
lib/file.c
lib/ssh.c
lib/url.c
lib/urldata.h

index 72b4cdbdd8a0cd94c072c070756bbf0e679475b9..01e4dc3c055c6cf3d0bed12828134586f210b86c 100644 (file)
@@ -1401,6 +1401,17 @@ this curl handle use the data from the shared handle instead of keeping the
 data to itself. This enables several curl handles to share data. If the curl
 handles are used simultaneously, you \fBMUST\fP use the locking methods in the
 share handle. See \fIcurl_share_setopt(3)\fP for details.
+.IP CURLOPT_NEW_FILE_PERMS
+Pass a long as a parameter, containing the value of the permissions that will
+be assigned to newly created files on the remote server.  The default value is
+\fI0644\fP, but any valid value can be used.  The only protocols that can use
+this are \fIsftp://\fP, \fIscp://\fP and \fIfile://\fP. (Added in 7.16.4)
+.IP CURLOPT_NEW_DIRECTORY_PERMS
+Pass a long as a parameter, containing the value of the permissions that will
+be assigned to newly created directories on the remote server.  The default
+value is \fI0755\fP, but any valid value can be used.  The only protocols that
+can use this are \fIsftp://\fP, \fIscp://\fP and \fIfile://\fP.
+(Added in 7.16.4)
 .SH TELNET OPTIONS
 .IP CURLOPT_TELNETOPTIONS
 Provide a pointer to a curl_slist with variables to pass to the telnet
index 000335b74241dac45d30f1cc1ef3541368cee3c5..2dbcfd7375e9b26b54f1e1df27e66ccf1eac5cfa 100644 (file)
@@ -1076,6 +1076,11 @@ typedef enum {
   CINIT(HTTP_TRANSFER_DECODING, LONG, 157),
   CINIT(HTTP_CONTENT_DECODING, LONG, 158),
 
+  /* Permission used when creating new files and directories on the remote
+     server for protocols that support it, SFTP/SCP/FILE */
+  CINIT(NEW_FILE_PERMS, LONG, 159),
+  CINIT(NEW_DIRECTORY_PERMS, LONG, 160),
+
   CURLOPT_LASTENTRY /* the last unused */
 } CURLoption;
 
index d5816bc4897a9a0080b40b5ec68b98afea1bf263..5c8d6bb3e25deeace6311bb9b6e4b96526915923 100644 (file)
@@ -731,6 +731,8 @@ void curl_easy_reset(CURL *curl)
 
   data->set.ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
                                                       type */
+  data->set.new_file_perms = 0644;    /* Default permissions */
+  data->set.new_directory_perms = 0755; /* Default permissions */
 }
 
 #ifdef CURL_DOES_CONVERSIONS
index 96f53f77999cdd35352a47ee47a780127bfe0eff..cec98986b1239e624bd8798057cb216f835c11f8 100644 (file)
@@ -217,8 +217,23 @@ static CURLcode file_upload(struct connectdata *conn)
 
   if(data->reqdata.resume_from)
     fp = fopen( file->path, "ab" );
-  else
+  else {
+    int fd;
+
+#if defined(WIN32) || defined(MSDOS) || defined(__EMX__)
+    fd = open(file->path, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
+              conn->data->set.new_file_perms);
+#else /* !(WIN32 || MSDOS || __EMX__) */
+    fd = open(file->path, O_WRONLY|O_CREAT|O_TRUNC,
+              conn->data->set.new_file_perms);
+#endif /* !(WIN32 || MSDOS || __EMX__) */
+    if (fd < 0) {
+      failf(data, "Can't open %s for writing", file->path);
+      return CURLE_WRITE_ERROR;
+    }
+    close(fd);
     fp = fopen(file->path, "wb");
+  }
 
   if(!fp) {
     failf(data, "Can't open %s for writing", file->path);
index b45134e73e3273eef4b469d0eadbff423a5758af..6a4b51ae3334aa87411b8fa263ee9c7b87d3807d 100644 (file)
--- a/lib/ssh.c
+++ b/lib/ssh.c
@@ -1137,10 +1137,7 @@ CURLcode Curl_scp_do(struct connectdata *conn, bool *done)
 #if (LIBSSH2_APINO >= 200706012030)
     do {
       scp->ssh_channel = libssh2_scp_send_ex(scp->ssh_session, scp->path,
-                                             LIBSSH2_SFTP_S_IRUSR|
-                                             LIBSSH2_SFTP_S_IWUSR|
-                                             LIBSSH2_SFTP_S_IRGRP|
-                                             LIBSSH2_SFTP_S_IROTH,
+                                             conn->data->set.new_file_perms,
                                              conn->data->set.infilesize, 0, 0);
       if (!scp->ssh_channel &&
           (libssh2_session_last_errno(scp->ssh_session) !=
@@ -1150,10 +1147,7 @@ CURLcode Curl_scp_do(struct connectdata *conn, bool *done)
     } while (!scp->ssh_channel);
 #else /* !(LIBSSH2_APINO >= 200706012030) */
     scp->ssh_channel = libssh2_scp_send_ex(scp->ssh_session, scp->path,
-                                           LIBSSH2_SFTP_S_IRUSR|
-                                           LIBSSH2_SFTP_S_IWUSR|
-                                           LIBSSH2_SFTP_S_IRGRP|
-                                           LIBSSH2_SFTP_S_IROTH,
+                                           conn->data->set.new_file_perms,
                                            conn->data->set.infilesize, 0, 0);
     if (!scp->ssh_channel)
       return CURLE_FAILED_INIT;
@@ -1363,8 +1357,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
       sftp->sftp_handle =
         libssh2_sftp_open(sftp->sftp_session, sftp->path,
                         LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
-                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                        data->set.new_file_perms);
       if (!sftp->sftp_handle &&
           (libssh2_session_last_errno(sftp->ssh_session) !=
            LIBSSH2_ERROR_EAGAIN)) {
@@ -1381,8 +1374,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
               sftp->sftp_handle = libssh2_sftp_open(sftp->sftp_session,
                           sftp->path,
                           LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
-                          LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                          LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                          data->set.new_file_perms);
               if (!sftp->sftp_handle &&
                   (libssh2_session_last_errno(sftp->ssh_session) !=
                    LIBSSH2_ERROR_EAGAIN)) {
@@ -1406,8 +1398,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
     sftp->sftp_handle =
       libssh2_sftp_open(sftp->sftp_session, sftp->path,
                         LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
-                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                        data->set.new_file_perms);
     if (!sftp->sftp_handle) {
       err = libssh2_sftp_last_error(sftp->sftp_session);
       if (((err == LIBSSH2_FX_NO_SUCH_FILE) ||
@@ -1420,8 +1411,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
         if (res == 0) {
           sftp->sftp_handle = libssh2_sftp_open(sftp->sftp_session, sftp->path,
                     LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
-                    LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                    LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                    data->set.new_file_perms);
         }
       }
       if (!sftp->sftp_handle) {
@@ -1636,8 +1626,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
       do {
         sftp->sftp_handle =
           libssh2_sftp_open(sftp->sftp_session, sftp->path, LIBSSH2_FXF_READ,
-                          LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                          LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                          data->set.new_file_perms);
         if (!sftp->sftp_handle &&
             (libssh2_session_last_errno(sftp->ssh_session) !=
                                    LIBSSH2_ERROR_EAGAIN)) {
@@ -1650,8 +1639,7 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
 #else /* !(LIBSSH2_APINO >= 200706012030) */
       sftp->sftp_handle =
         libssh2_sftp_open(sftp->sftp_session, sftp->path, LIBSSH2_FXF_READ,
-                          LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
-                          LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
+                          data->set.new_file_perms);
       if (!sftp->sftp_handle) {
         err = libssh2_sftp_last_error(sftp->sftp_session);
         failf(conn->data, "Could not open remote file for reading: %s",
@@ -2304,15 +2292,11 @@ static CURLcode sftp_create_dirs(struct connectdata *conn) {
       /* 'mode' - parameter is preliminary - default to 0644 */
 #if (LIBSSH2_APINO >= 200706012030)
       while ((rc = libssh2_sftp_mkdir(sftp->sftp_session, sftp->path,
-                               LIBSSH2_SFTP_S_IRWXU |
-                               LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IXGRP |
-                               LIBSSH2_SFTP_S_IROTH | LIBSSH2_SFTP_S_IXOTH)) ==
+                                      conn->data->set.new_directory_perms)) ==
              LIBSSH2_ERROR_EAGAIN);
 #else /* !(LIBSSH2_APINO >= 200706012030) */
       rc = libssh2_sftp_mkdir(sftp->sftp_session, sftp->path,
-                               LIBSSH2_SFTP_S_IRWXU |
-                               LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IXGRP |
-                               LIBSSH2_SFTP_S_IROTH | LIBSSH2_SFTP_S_IXOTH);
+                               conn->data->set.new_directory_perms);
 #endif /* !(LIBSSH2_APINO >= 200706012030) */
       *slash_pos = '/';
       ++slash_pos;
index 789959762a193d360add76aeb98b841e259f87ff..b5b83effbeade3cf39349371fea2c56b1470a60f 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -595,6 +595,8 @@ CURLcode Curl_open(struct SessionHandle **curl)
 
     data->set.ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
                                                         type */
+    data->set.new_file_perms = 0644;    /* Default permissions */
+    data->set.new_directory_perms = 0755; /* Default permissions */
 
     /* most recent connection is not yet defined */
     data->state.lastconnect = -1;
@@ -1759,6 +1761,21 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
      */
     data->set.http_ce_skip = (bool)(0 == va_arg(param, long));
     break;
+
+  case CURLOPT_NEW_FILE_PERMS:
+    /*
+     * Uses these permissions instead of 0644
+     */
+    data->set.new_file_perms = va_arg(param, long);
+    break;
+
+  case CURLOPT_NEW_DIRECTORY_PERMS:
+    /*
+     * Uses these permissions instead of 0755
+     */
+    data->set.new_directory_perms = va_arg(param, long);
+    break;
+
   default:
     /* unknown tag and its companion, just ignore: */
     result = CURLE_FAILED_INIT; /* correct this */
index f52f96fbdf89095814d4bdd05837cf94aaf38b57..bd012b81790bb906a237161c3deac39b3276e99f 100644 (file)
@@ -1356,6 +1356,8 @@ struct UserDefined {
                             transfer-encoded (chunked, compressed) */
   bool http_ce_skip;     /* pass the raw body data to the user, even when
                             content-encoded (chunked, compressed) */
+  long new_file_perms;    /* Permissions to use when creating remote files */
+  long new_directory_perms; /* Permissions to use when creating remote dirs */
 };
 
 struct Names {