CarServiceUtils.java revision 4aeb4bf0c56588be65264c324bbaaa545ad6714c
1ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung/*
2ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * Copyright (C) 2015 The Android Open Source Project
3ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung *
4ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * Licensed under the Apache License, Version 2.0 (the "License");
5ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * you may not use this file except in compliance with the License.
6ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * You may obtain a copy of the License at
7ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung *
8ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung *      http://www.apache.org/licenses/LICENSE-2.0
9ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung *
10ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * Unless required by applicable law or agreed to in writing, software
11ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * distributed under the License is distributed on an "AS IS" BASIS,
12ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * See the License for the specific language governing permissions and
14ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung * limitations under the License.
15ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung */
16ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung
17ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoungpackage com.android.car;
18ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung
194aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.content.Context;
204aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.content.pm.ApplicationInfo;
214aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.content.pm.PackageManager;
224aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.content.pm.PackageManager.NameNotFoundException;
234aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.os.Binder;
244aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Parkimport android.util.Log;
254aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park
26ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung/** Utility class */
27ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoungpublic class CarServiceUtils {
28ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung
294aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park    private static final String PACKAGE_NOT_FOUND = "Package not found:";
304aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park
314aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park    /**
324aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park     * Check if package name passed belongs to UID for the current binder call.
334aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park     * @param context
344aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park     * @param packageName
354aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park     */
364aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park    public static void assertPakcageName(Context context, String packageName)
374aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            throws IllegalArgumentException, SecurityException {
384aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        if (packageName == null) {
394aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            throw new IllegalArgumentException("Package name null");
404aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        }
414aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        ApplicationInfo appInfo = null;
424aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        try {
434aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            appInfo = context.getPackageManager().getApplicationInfo(packageName,
444aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park                    0);
454aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        } catch (NameNotFoundException e) {
464aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            String msg = PACKAGE_NOT_FOUND + packageName;
474aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            Log.w(CarLog.TAG_SERVICE, msg, e);
484aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            throw new SecurityException(msg, e);
494aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        }
504aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        if (appInfo == null) {
514aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            throw new SecurityException(PACKAGE_NOT_FOUND + packageName);
524aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        }
534aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        int uid = Binder.getCallingUid();
544aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        if (uid != appInfo.uid) {
554aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park            throw new SecurityException("Wrong package name:" + packageName +
564aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park                    ", The package does not belong to caller's uid:" + uid);
574aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park        }
584aeb4bf0c56588be65264c324bbaaa545ad6714cKeun-young Park    }
59ca515079e9fc0c35b1498830f67378e9ccf949e5keunyoung}
60