Baseline update from LYNQ_SDK_ASR_T108_V05.03.01.00(kernel build error.)

Change-Id: I56fc72cd096e82c589920026553170e5cb9692eb
diff --git a/package/utils/adbd/src/adb/adb.c b/package/utils/adbd/src/adb/adb.c
old mode 100644
new mode 100755
index 8368ad4..c254959
--- a/package/utils/adbd/src/adb/adb.c
+++ b/package/utils/adbd/src/adb/adb.c
@@ -56,6 +56,10 @@
 
 static int auth_enabled = 0;
 
+#ifdef MBTK_ADB_SEC_SUPPORT
+FILE *log_file = NULL;
+#endif
+
 #if !ADB_HOST
 static const char *adb_device_banner = "device";
 // changed by feilv
@@ -1691,6 +1695,11 @@
 
 int main(int argc, char **argv)
 {
+
+#ifdef MBTK_ADB_SEC_SUPPORT
+    // log_file = fopen("/tmp/adb.log", "w");
+#endif
+
 #if ADB_HOST
     adb_sysdeps_init();
     adb_trace_init();
diff --git a/package/utils/adbd/src/adb/adb.h b/package/utils/adbd/src/adb/adb.h
old mode 100644
new mode 100755
index 4704abb..a3d348b
--- a/package/utils/adbd/src/adb/adb.h
+++ b/package/utils/adbd/src/adb/adb.h
@@ -18,6 +18,9 @@
 #define __ADB_H
 
 #include <limits.h>
+#ifdef MBTK_ADB_SEC_SUPPORT
+#include <stdio.h>
+#endif
 
 #include "transport.h"  /* readx(), writex() */
 
@@ -379,7 +382,39 @@
   extern int     adb_trace_mask;
   extern unsigned char    adb_trace_output_count;
   void    adb_trace_init(void);
+#ifdef MBTK_ADB_SEC_SUPPORT
+#  define ADB_TRACING 1
 
+extern FILE *log_file;
+
+  /* you must define TRACE_TAG before using this macro */
+#  define  D(...)                                      \
+        do {                                           \
+            if (ADB_TRACING && log_file) {                         \
+                int save_errno = errno;                \
+                adb_mutex_lock(&D_lock);               \
+                fprintf(log_file, "%s::%s():",           \
+                        __FILE__, __FUNCTION__);       \
+                errno = save_errno;                    \
+                fprintf(log_file, __VA_ARGS__ );         \
+                fflush(log_file);                        \
+                adb_mutex_unlock(&D_lock);             \
+                errno = save_errno;                    \
+           }                                           \
+        } while (0)
+#  define  DR(...)                                     \
+        do {                                           \
+            if (ADB_TRACING && log_file) {                         \
+                int save_errno = errno;                \
+                adb_mutex_lock(&D_lock);               \
+                errno = save_errno;                    \
+                fprintf(log_file, __VA_ARGS__ );         \
+                fflush(log_file);                        \
+                adb_mutex_unlock(&D_lock);             \
+                errno = save_errno;                    \
+           }                                           \
+        } while (0)
+#else
 #  define ADB_TRACING  ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
 
   /* you must define TRACE_TAG before using this macro */
@@ -409,6 +444,7 @@
                 errno = save_errno;                    \
            }                                           \
         } while (0)
+#endif		
 #else
 #  define  D(...)          ((void)0)
 #  define  DR(...)         ((void)0)
diff --git a/package/utils/adbd/src/adb/file_sync_service.c b/package/utils/adbd/src/adb/file_sync_service.c
old mode 100644
new mode 100755
index 436ec02..3d83eb7
--- a/package/utils/adbd/src/adb/file_sync_service.c
+++ b/package/utils/adbd/src/adb/file_sync_service.c
@@ -33,6 +33,66 @@
 #include "adb.h"
 #include "file_sync_service.h"
 
+#ifdef MBTK_ADB_SEC_SUPPORT
+static char* mbtk_sec_files[] = {"/etc/passwd", "/etc/shadow", "/bin/adb_shell", "/bin/busybox", "/usr/bin/adbd", NULL};
+
+static bool file_name_cmp(const char *name1, const char *name2) {
+    char tmp_name1[1025] = {0};
+    char tmp_name2[1025] = {0};
+    int i = 0;
+    int j = 0;
+    while(i < strlen(name1)) {
+        if(name1[i] != '/')
+            tmp_name1[j++] = name1[i];
+        i++;
+    }
+
+    i = 0;
+    j = 0;
+    while(i < strlen(name2)) {
+        if(name2[i] != '/')
+            tmp_name2[j++] = name2[i];
+        i++;
+    }
+
+    return strcmp(tmp_name1, tmp_name2);
+}
+
+static bool mbtk_sec_file_check(bool is_pull, const char *name) {
+    struct stat file_stat;
+    memset(&file_stat, 0, sizeof(struct stat));
+    if(lstat(name, &file_stat)) { // Fail.
+        D("MBTK : lstat fail : %d\n", errno);
+        if(errno == ENOENT) { // File not exist.
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    if(S_ISREG(file_stat.st_mode)) {
+        D("MBTK : FILE REG.\n");
+        char **ptr = mbtk_sec_files;
+        while(*ptr) {
+            if(file_name_cmp(*ptr, name) == 0)
+                return false;
+            ptr++;
+        }
+        return true;
+    } else if(S_ISCHR(file_stat.st_mode)
+        || S_ISBLK(file_stat.st_mode)) {
+        D("MBTK : FILE CHR/BLK.\n");
+        if(is_pull) {
+            return true;
+        } else {
+            return false;
+        }
+    } else {
+        return false;
+    }
+}
+#endif
+
 /* TODO: use fs_config to configure permissions on /data */
 static bool is_on_system(const char *name) {
     const char *SYSTEM = "/system/";
@@ -423,6 +483,10 @@
     syncmsg msg;
     char name[1025];
     unsigned namelen;
+#ifdef MBTK_ADB_SEC_SUPPORT
+    mode_t mode = 0;
+    char file_name[1025] = {0};
+#endif
 
     char *buffer = malloc(SYNC_DATA_MAX);
     if(buffer == 0) goto fail;
@@ -447,6 +511,61 @@
 
         msg.req.namelen = 0;
         D("sync: '%s' '%s'\n", (char*) &msg.req, name);
+#ifdef MBTK_ADB_SEC_SUPPORT
+        if(msg.req.id == ID_SEND) { // push
+            // '/usr/bin/adbd,33206'
+            char *ptr = strstr(name, ",");
+            if(ptr && ptr > name) {
+                struct stat file_stat;
+                mode = 0;
+                memset(file_name, 0, sizeof(file_name));
+                memcpy(file_name, name, ptr - name);
+
+                if(!mbtk_sec_file_check(false, file_name)) {
+                    break;
+                }
+
+                memset(&file_stat, 0, sizeof(struct stat));
+                if(stat(file_name, &file_stat)) { // Fail.
+                    mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH/* | S_IXOTH*/;
+                } else {
+                    if(file_stat.st_mode & S_IRUSR) {
+                        mode |= S_IRUSR;
+                    }
+                    if(file_stat.st_mode & S_IWUSR) {
+                        mode |= S_IWUSR;
+                    }
+                    if(file_stat.st_mode & S_IXUSR) {
+                        mode |= S_IXUSR;
+                    }
+                    if(file_stat.st_mode & S_IRGRP) {
+                        mode |= S_IRGRP;
+                    }
+                    if(file_stat.st_mode & S_IWGRP) {
+                        mode |= S_IWGRP;
+                    }
+                    if(file_stat.st_mode & S_IXGRP) {
+                        mode |= S_IXGRP;
+                    }
+                    if(file_stat.st_mode & S_IROTH) {
+                        mode |= S_IROTH;
+                    }
+                    if(file_stat.st_mode & S_IWOTH) {
+                        mode |= S_IWOTH;
+                    }
+                    if(file_stat.st_mode & S_IXOTH) {
+                        mode |= S_IXOTH;
+                    }
+                }
+
+                D("MBTK : %s mode is %x\n", file_name, mode);
+            }
+        } else if(msg.req.id == ID_RECV) { // pull
+            if(!mbtk_sec_file_check(true, name)) {
+                break;
+            }
+        }
+#endif
 
         switch(msg.req.id) {
         case ID_STAT:
@@ -457,6 +576,16 @@
             break;
         case ID_SEND:
             if(do_send(fd, name, buffer)) goto fail;
+#ifdef MBTK_ADB_SEC_SUPPORT
+            if(strlen(file_name) > 0) {
+                if(chmod(file_name, mode)) {
+                    D("MBTK : chmod(%s, %x) fail.\n", file_name, mode);
+                } else {
+                    D("MBTK : chmod(%s, %x) success.\n", file_name, mode);
+                    system("sync");
+                }
+            }
+#endif
             break;
         case ID_RECV:
             if(do_recv(fd, name, buffer)) goto fail;
diff --git a/package/utils/adbd/src/adb/services.c b/package/utils/adbd/src/adb/services.c
old mode 100644
new mode 100755
index 0dbe4db..b57e891
--- a/package/utils/adbd/src/adb/services.c
+++ b/package/utils/adbd/src/adb/services.c
@@ -21,6 +21,12 @@
 #include <string.h>
 #include <errno.h>
 
+#ifdef MBTK_ADB_SEC_SUPPORT
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#endif
+
 #include "sysdeps.h"
 
 #define  TRACE_TAG  TRACE_SERVICES
@@ -300,15 +306,70 @@
 }
 #endif  /* !ABD_HOST */
 
-//#define ADB_PASSWD
 #if ADB_HOST
 #define SHELL_COMMAND "/bin/sh"
 #else
-#ifdef ADB_PASSWD
-#define SHELL_COMMAND "/bin/login"
+	
+#ifdef MBTK_ADB_SEC_SUPPORT
+#define SHELL_COMMAND "/bin/adb_shell"
 #else
 #define SHELL_COMMAND "/bin/sh"
-#endif //ADB_PASSWD
+#endif
+
+#endif
+
+#ifdef MBTK_ADB_SEC_SUPPORT
+#define MBTK_LOGIN_STATE_FILE "/tmp/mbtk_login_count"
+
+static int login_conf_read(const char *path, long *value)
+{
+    char buff[64];
+    int fd = adb_open(path, O_RDONLY);
+    if(fd <= 0)
+        return -1;
+    memset(buff, 0, sizeof(buff));
+    if(adb_read(fd, buff, sizeof(buff)) > 0) {
+        *value = atol(buff);
+    }
+    adb_close(fd);
+    return 0;
+}
+
+static int login_conf_write(const char *path, long value)
+{
+    char buff[64];
+    int fd = adb_open(path, O_WRONLY);
+    if(fd <= 0)
+        return -1;
+    memset(buff, 0, sizeof(buff));
+    snprintf(buff, sizeof(buff), "%ld\n", value);
+    if(adb_write(fd, buff, strlen(buff)) > 0) {
+        adb_close(fd);
+        return 0;
+    }
+    adb_close(fd);
+    return -1;
+}
+
+static int login_count_change(int add)
+{
+    long count = 0L;
+    if(login_conf_read(MBTK_LOGIN_STATE_FILE, &count)) {
+        count = 0L;
+    }
+    // printf("old count is %d\n", count);
+    if(add) {
+        count++;
+    } else {
+        count--;
+    }
+
+    if(count < 0) {
+        count = 0L;
+    }
+    return login_conf_write(MBTK_LOGIN_STATE_FILE, count);
+}
+
 #endif
 
 #if !ADB_HOST
@@ -334,6 +395,10 @@
             }
          }
     }
+
+#ifdef MBTK_ADB_SEC_SUPPORT
+    login_count_change(0);
+#endif
     D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
     if (SHELL_EXIT_NOTIFY_FD >=0) {
       int res;
@@ -352,11 +417,7 @@
 
     const char *arg0, *arg1;
     if (name == 0 || *name == 0) {
-#ifdef ADB_PASSWD
-        arg0 = "-p"; arg1 = 0;
-#else
         arg0 = "-"; arg1 = 0;
-#endif //ADB_PASSWD
     } else {
         arg0 = "-c"; arg1 = name;
     }
@@ -436,7 +497,14 @@
     } else if(!HOST && !strncmp(name, "exec:", 5)) {
         ret = create_subproc_thread(name + 5, SUBPROC_RAW);
     } else if(!strncmp(name, "sync:", 5)) {
-        ret = create_service_thread(file_sync_service, NULL);
+#ifdef MBTK_ADB_SEC_SUPPORT
+        long login_success = 0L;
+        if(login_conf_read(MBTK_LOGIN_STATE_FILE, &login_success) == 0 && login_success > 0) {
+            ret = create_service_thread(file_sync_service, NULL);
+        }
+#else
+		ret = create_service_thread(file_sync_service, NULL);
+#endif
     } else if(!strncmp(name, "remount:", 8)) {
         ret = create_service_thread(remount_service, NULL);
     } else if(!strncmp(name, "reboot:", 7)) {