be1fbdfefa43b5cd520ab6e46ac123ee44302906
[framework/base/rpm-installer.git] / inc / rpm-installer-util.h
1 /*
2  * rpm-installer
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #ifndef __RPM_INSTALLER_UTIL_H_
24 #define __RPM_INSTALLER_UTIL_H_
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif                          /* __cplusplus */
29
30 #include <string.h>
31 #include <stdarg.h>
32 #include <sys/wait.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <wait.h>
37 #include <stdio.h>
38 #include <dirent.h>
39 #include <rpm/header.h>
40 #include <rpm/rpmts.h>
41 #include <rpm/rpmlib.h>
42 #include <glib.h>
43
44 #include <libxml/parser.h>
45 #include <libxml/xmlreader.h>
46 #include <libxml/xmlschemas.h>
47 #include <pkgmgr-info.h>
48
49 #include <dlog.h>
50
51 #ifdef  LOG_TAG
52 #undef  LOG_TAG
53 #define LOG_TAG         "rpm-installer"
54 #endif
55
56 #define _LOGE(fmt, arg...) do { \
57                         fprintf(stderr, "  ## "fmt"\n", ##arg); \
58                         LOGE(fmt, ##arg); \
59                 } while (0)
60
61 #define _LOGD(fmt, arg...) do { \
62                         fprintf(stderr, "  ## "fmt"\n", ##arg); \
63                         LOGD(fmt, ##arg); \
64                 } while (0)
65
66 #define _LOGP(fmt, arg...)      fprintf(stderr, "[coretpk-installer] "fmt"\n", ##arg)
67
68 #define RPM_BACKEND_EXEC        "rpm-backend"
69
70 #define WGT_CONFIG      "config.xml"
71 #define SIGNATURE1_XML                                          "signature1.xml"
72 #define SIGNATURE2_XML                                          "signature2.xml"
73 #define AUTHOR_SIGNATURE_XML                            "author-signature.xml"
74
75 #define ASCII(s) (const char *)s
76 #define XMLCHAR(s) (const xmlChar *)s
77
78 #define PKG_MAX_LEN             128
79 #define VERSION_MAX_LEN 11
80
81 #define DIRECTORY_PERMISSION_755                        0755
82 #define DIRECTORY_PERMISSION_644                        0644
83 #define FILE_PERMISSION_755                                     0755
84 #define FILE_PERMISSION_644                                     0644
85
86 struct pkginfo_t {
87         char package_name[PKG_MAX_LEN];
88         char version[VERSION_MAX_LEN];
89         char client_id[PKG_MAX_LEN];
90         char api_version[VERSION_MAX_LEN];
91         bool is_custom_clock;
92         bool is_widget;
93         bool is_preload;
94         GList *privileges;
95         pkgmgrinfo_install_location install_location;
96 };
97 typedef struct pkginfo_t pkginfo;
98
99 struct privilegeinfo_t {
100         char package_id[PKG_MAX_LEN];
101         int visibility;
102 };
103 typedef struct privilegeinfo_t privilegeinfo;
104
105 #ifndef PATH_MAX
106 # define PATH_MAX 4096
107 #endif
108
109 #define FREE_AND_STRDUP(from, to) do { \
110                 if (to) free((void *)to); \
111                 if (from) to = strdup(from); \
112         } while (0)
113
114
115 #define FREE_AND_NULL(ptr) do { \
116                 if (ptr) { \
117                         free((void *)ptr); \
118                         ptr = NULL; \
119                 } \
120         } while (0)
121
122
123 /*Error number according to Tizen Native Package Manager Command Specification v1.0*/
124 #define RPM_INSTALLER_SUCCESS                                   0
125 #define RPM_INSTALLER_ERR_WRONG_PARAM                           64
126 #define RPM_INSTALLER_ERR_DBUS_PROBLEM                          102
127 #define RPM_INSTALLER_ERR_PACKAGE_EXIST                         121
128 #define RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED                 104
129 #define RPM_INSTALLER_ERR_RESOURCE_BUSY                         105
130 #define RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY                     63
131 #define RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION                  107
132 #define RPM_INSTALLER_ERR_NO_RPM_FILE                           2
133 #define RPM_INSTALLER_ERR_DB_ACCESS_FAILED                      109
134 #define RPM_INSTALLER_ERR_RPM_OPERATION_FAILED                  110
135 #define RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED                  111
136 #define RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS                 112
137 #define RPM_INSTALLER_ERR_NEED_USER_CONFIRMATION                113
138 #define RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED         114
139 #define RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED       115
140 #define RPM_INSTALLER_ERR_CLEAR_DATA_FAILED                     116
141 #define RPM_INSTALLER_ERR_INTERNAL                              117
142 #define RPM_INSTALLER_ERR_PKG_NOT_FOUND                         1
143 #define RPM_INSTALLER_ERR_UNKNOWN                               119
144 #define RPM_INSTALLER_ERR_NO_MANIFEST                           11
145 #define RPM_INSTALLER_ERR_INVALID_MANIFEST                      12
146 #define RPM_INSTALLER_ERR_SIG_NOT_FOUND                         21
147 #define RPM_INSTALLER_ERR_SIG_INVALID                                   22
148 #define RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED                               23
149 #define RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND                           31
150 #define RPM_INSTALLER_ERR_CERT_INVALID                                  32
151 #define RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED         33
152 #define RPM_INSTALLER_ERR_NO_CONFIG                                         34
153 #define RPM_INSTALLER_ERR_INVALID_CONFIG        35
154 #define RPM_INSTALLER_ERR_CMD_NOT_SUPPORTED     36
155 #define RPM_INSTALLER_ERR_PRIVILEGE_UNAUTHORIZED        43
156 #define RPM_INSTALLER_ERR_PRIVILEGE_UNKNOWN     44
157 #define RPM_INSTALLER_ERR_PRIVILEGE_USING_LEGACY_FAILED 45
158
159 #define RPM_INSTALLER_SUCCESS_STR                       "Success"
160 #define RPM_INSTALLER_ERR_WRONG_PARAM_STR               "Wrong Input Param"
161 #define RPM_INSTALLER_ERR_DBUS_PROBLEM_STR                      "DBUS Error"
162 #define RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY_STR "Not Enough Memory"
163 #define RPM_INSTALLER_ERR_PACKAGE_EXIST_STR     "Package Already Installed"
164 #define RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED_STR     "Package Not Installed"
165 #define RPM_INSTALLER_ERR_RESOURCE_BUSY_STR                     "Resource Busy"
166 #define RPM_INSTALLER_ERR_UNKNOWN_STR                   "Unknown Error"
167 #define RPM_INSTALLER_ERR_PKG_NOT_FOUND_STR             "Package file not found"
168 #define RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION_STR      "Version Not supported"
169 #define RPM_INSTALLER_ERR_NO_RPM_FILE_STR       "No RPM Package"
170 #define RPM_INSTALLER_ERR_DB_ACCESS_FAILED_STR  "DB Access Failed"
171 #define RPM_INSTALLER_ERR_RPM_OPERATION_FAILED_STR      "RPM operation failed"
172 #define RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED_STR      "Package Not Upgraded"
173 #define RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS_STR     "Wrong Args to Script"
174 #define RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED_STR     "Installation Disabled"
175 #define RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED_STR   "Uninstallation Disabled"
176 #define RPM_INSTALLER_ERR_CLEAR_DATA_FAILED_STR         "Clear Data Failed"
177 #define RPM_INSTALLER_ERR_INTERNAL_STR  "Internal Error"
178 #define RPM_INSTALLER_ERR_NO_MANIFEST_STR       "Manifest File Not Found"
179 #define RPM_INSTALLER_ERR_INVALID_MANIFEST_STR  "Manifest Validation Failed"
180 #define RPM_INSTALLER_ERR_SIG_NOT_FOUND_STR             "Signature Not Found"
181 #define RPM_INSTALLER_ERR_SIG_INVALID_STR       "Invalid Signature"
182 #define RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED_STR   "Signature Verification Failed"
183 #define RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND_STR               "Root Cert Not Found"
184 #define RPM_INSTALLER_ERR_CERT_INVALID_STR      "Invalid Certificate"
185 #define RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED_STR     "Certificate Chain Verification Failed"
186 #define RPM_INSTALLER_ERR_NO_CONFIG_STR         "Config file is not present"
187 #define RPM_INSTALLER_ERR_INVALID_CONFIG_STR    "Config file is not valid"
188 #define RPM_INSTALLER_ERR_CMD_NOT_SUPPORTED_STR "Unsupported Command"
189 #define RPM_INSTALLER_ERR_PRIVILEGE_UNAUTHORIZED_STR "Unauthorized privilege"
190 #define RPM_INSTALLER_ERR_PRIVILEGE_UNKNOWN_ERR_STR "Unknown privilege"
191 #define RPM_INSTALLER_ERR_PRIVILEGE_USING_LEGACY_FAILED_STR "Deprecated privilege"
192
193 #define DEBUG_ERR               0x0001
194 #define DEBUG_INFO              0x0002
195 #define DEBUG_RESULT    0x0004
196
197 #define RPM_LOG 1
198 #define SIZE_KB 1024
199 #define BUFF_SZE    1024
200 #define RPM_INSTALLER_RW_INSTALL_PATH "/opt/usr"
201 #define DIR_RPM_INSTALLER_APPLICATIONS_TEMP "/tmp/wgt_unzip"
202 #define RPM_UNZIP "/usr/bin/unzip"
203 #define DIR_PERMS (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
204 #define DIR_RPM_WGT_SMACK_RULE_OPT "/opt/usr/.wgt/"
205
206         void _ri_error_no_to_string(int errnumber, char **errstr);
207         int _ri_recursive_delete_dir(char *dirname);
208         int _ri_string_to_error_no(char *errstr);
209         int _ri_get_available_free_memory(const char *opt_path, unsigned long *free_mem);
210         int  _ri_process_wgt_package(char** pkgid);
211         unsigned long  _ri_calculate_file_size(const char *filename);
212         int  _ri_wgt_package_extract(char *pkgid);
213         int  _ri_stream_config_file(const char* filename, pkginfo *info);
214         void _ri_process_config_node(xmlTextReaderPtr reader, pkginfo * info);
215         int _verify_wgt_package_signature_files();
216         void _ri_remove_wgt_unzip_dir();
217         int _ri_xsystem(const char *argv[]);
218
219         int  _get_package_name_from_xml(char* manifest,char** pkgname);
220         int  _get_pkgname_from_rpm_name(char* pkgfile,char** pkgname);
221         int _child_element(xmlTextReaderPtr reader, int depth);
222         char *_ri_basename(char *name);
223         int _ri_verify_sig_and_cert(const char *sigfile, int *visibility);
224         char* _manifest_to_package(const char* manifest);
225         int _rpm_delete_dir(char *dirname);
226         unsigned long  _ri_calculate_rpm_size( char* rpm_file);
227         int _ri_get_attribute(xmlTextReaderPtr reader,char *attribute, const char **xml_attribute);
228 #ifdef __cplusplus
229 }
230 #endif                          /* __cplusplus */
231 #endif                          /* __RPM_INSTALLER_UTIL_H_ */