Imported Upstream version 2.5.0
[debian/amanda] / recover-src / amrecover.c
index 4e2776a60356bf01fff2416d71ad02363e7adab7..a4fe069f59a5a5eddca41749ce97f164481dd65b 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: amrecover.c,v 1.29.4.7.4.6.2.7.2.1 2005/09/20 21:31:52 jrjackson Exp $
+ * $Id: amrecover.c,v 1.52 2006/01/14 04:37:19 paddy_s Exp $
  *
  * an interactive program for recovering backed-up files
  */
 #include "amrecover.h"
 #include "getfsent.h"
 #include "dgram.h"
-
-#if defined(KRB4_SECURITY)
-#include "krb4-security.h"
-#endif
 #include "util.h"
 
 #ifdef HAVE_LIBREADLINE
@@ -87,7 +83,8 @@ char *tape_server_name = NULL;
 int tape_server_socket;
 char *tape_device_name = NULL;
 am_feature_t *our_features = NULL;
-am_feature_t *their_features = NULL;
+am_feature_t *indexsrv_features = NULL;
+am_feature_t *tapesrv_features = NULL;
 
 
 #ifndef HAVE_LIBREADLINE
@@ -212,28 +209,25 @@ int server_happy ()
 int send_command(cmd)
 char *cmd;
 {
-    size_t l, n;
-    ssize_t s;
-    char *end;
-
     /*
      * NOTE: this routine is called from sigint_handler, so we must be
      * **very** careful about what we do since there is no way to know
      * our state at the time the interrupt happened.  For instance,
-     * do not use any stdio routines here.
+     * do not use any stdio or malloc routines here.
      */
-    for (l = 0, n = strlen(cmd); l < n; l += s)
-       if ((s = write(server_socket, cmd + l, n - l)) < 0) {
-           perror("amrecover: Error writing to server");
-           return -1;
-       }
-    end = "\r\n";
-    for (l = 0, n = strlen(end); l < n; l += s)
-       if ((s = write(server_socket, end + l, n - l)) < 0) {
-           perror("amrecover: Error writing to server");
-           return -1;
-       }
-    return 0;
+    struct iovec msg[2];
+    ssize_t bytes;
+
+    msg[0].iov_base = cmd;
+    msg[0].iov_len = strlen(msg[0].iov_base);
+    msg[1].iov_base = "\r\n";
+    msg[1].iov_len = strlen(msg[1].iov_base);
+    bytes = msg[0].iov_len + msg[1].iov_len;
+
+    if (writev(server_socket, msg, 2) < bytes) {
+       return -1;
+    }
+    return (0);
 }
 
 
@@ -428,6 +422,10 @@ char **argv;
     safe_fd(-1, 0);
 
     set_pname("amrecover");
+
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
+
     dbopen();
 
 #ifndef IGNORE_UID_CHECK
@@ -507,6 +505,9 @@ char **argv;
     amfree(disk_path);
     dump_date[0] = '\0';
 
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
+
     /* set up signal handler */
     act.sa_handler = sigint_handler;
     sigemptyset(&act.sa_mask);
@@ -529,7 +530,8 @@ char **argv;
                                             ntohs(sp->s_port),
                                             -1,
                                             -1,
-                                            &my_port);
+                                            &my_port,
+                                            0);
     if (server_socket < 0)
     {
        error("cannot connect to %s: %s", server_name, strerror(errno));
@@ -560,17 +562,7 @@ char **argv;
     }
 
     /* do the security thing */
-#if defined(KRB4_SECURITY)
-#if 0 /* not yet implemented */
-    if(krb4_auth)
-    {
-       line = get_krb_security();
-    } else
-#endif /* 0 */
-#endif
-    {
-       line = get_bsd_security();
-    }
+    line = get_security();
     if (converse(line) == -1)
        exit(1);
     if (!server_happy())
@@ -578,7 +570,7 @@ char **argv;
     memset(line, '\0', strlen(line));
     amfree(line);
 
-    /* try to get the features form the server */
+    /* try to get the features from the server */
     {
        char *our_feature_string = NULL;
        char *their_feature_string = NULL;
@@ -588,10 +580,10 @@ char **argv;
        line = stralloc2("FEATURES ", our_feature_string);
        if(exchange(line) == 0) {
            their_feature_string = stralloc(server_line+13);
-           their_features = am_string_to_feature(their_feature_string);
+           indexsrv_features = am_string_to_feature(their_feature_string);
        }
        else {
-           their_features = am_set_default_feature_set();
+           indexsrv_features = am_set_default_feature_set();
         }
        amfree(our_feature_string);
        amfree(their_feature_string);
@@ -675,3 +667,13 @@ char **argv;
     aclose(server_socket);
     return 0;
 }
+
+char *
+get_security()
+{
+    struct passwd *pwptr;
+
+    if((pwptr = getpwuid(getuid())) == NULL)
+       error("can't get login name for my uid %ld", (long)getuid());
+    return stralloc2("SECURITY USER ", pwptr->pw_name);
+}