b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ID-mapping library - conversion utilities
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose Sumit Bose <sbose@redhat.com>
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose Copyright (C) 2012 Red Hat
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose This program is free software; you can redistribute it and/or modify
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose it under the terms of the GNU General Public License as published by
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose the Free Software Foundation; either version 3 of the License, or
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose (at your option) any later version.
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose This program is distributed in the hope that it will be useful,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose GNU General Public License for more details.
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose You should have received a copy of the GNU General Public License
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
9fd2775fe1ced6ff6a9a3ff7db124fcb52dade5dSumit Bose uint8_t id_auth[SID_ID_AUTHS]; /* highest order byte has index 0 */
9fd2775fe1ced6ff6a9a3ff7db124fcb52dade5dSumit Bose uint32_t sub_auths[SID_SUB_AUTHS]; /* host byte-order */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_bin_sid_to_dom_sid(struct sss_idmap_ctx *ctx,
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose if (length > sizeof(struct sss_dom_sid)) return IDMAP_SID_INVALID;
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose dom_sid = ctx->alloc_func(sizeof(struct sss_dom_sid), ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Safely copy in the SID revision number */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Safely copy in the number of sub auth values */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Make sure we aren't being told to read more bin_sid
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose * than can fit in the structure
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Safely copy in the id_auth values */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose for (i = 0; i < SID_ID_AUTHS; i++) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Safely copy in the sub_auths values */
9fd2775fe1ced6ff6a9a3ff7db124fcb52dade5dSumit Bose /* SID sub auth values in Active Directory are stored little-endian,
9fd2775fe1ced6ff6a9a3ff7db124fcb52dade5dSumit Bose * we store them in host order */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_dom_sid_to_bin_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose length = 2 + SID_ID_AUTHS + dom_sid->num_auths * 4;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose bin_sid = ctx->alloc_func(length, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose for (i = 0; i < SID_ID_AUTHS; i++) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_dom_sid_to_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose sid_buf = ctx->alloc_func(sid_buf_len, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Only 32bits are used for the string representation */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose nc = snprintf(sid_buf, sid_buf_len, "S-%u-%lu", dom_sid->sid_rev_num,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose (unsigned long) id_auth_val);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* Loop through the sub-auths, if any, prepending a hyphen
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose * for each one.
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_sid_to_dom_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose const char *sid,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (sid == NULL || (sid[0] != 'S' && sid[0] != 's') || sid[1] != '-') {
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose dom_sid = ctx->alloc_func(sizeof(struct sss_dom_sid), ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (errno != 0 || r == NULL || *r != '-' || ul > UINT8_MAX) {
2d07aa724c93bbaec2cd29470941c0754c74f715Sumit Bose if (errno != 0 || r == NULL || ul > UINT32_MAX) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* id_auth in the string should always be <2^32 in decimal */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* store values in the same order as the binary representation */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (*r == '\0') {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose /* no sub auths given */
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (*r != '-') {
2d07aa724c93bbaec2cd29470941c0754c74f715Sumit Bose if (errno != 0 || ul > UINT32_MAX || end == NULL ||
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose } while (*r != '\0');
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_sid_to_bin_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose const char *sid,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_sid_to_dom_sid(ctx, sid, &dom_sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_dom_sid_to_bin_sid(ctx, dom_sid, &bin_sid, &length);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_bin_sid_to_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_bin_sid_to_dom_sid(ctx, bin_sid, length, &dom_sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_dom_sid_to_sid(ctx, dom_sid, &sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_sid_to_smb_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose const char *sid,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_sid_to_dom_sid(ctx, sid, &dom_sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_dom_sid_to_smb_sid(ctx, dom_sid, &smb_sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_smb_sid_to_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_smb_sid_to_dom_sid(ctx, smb_sid, &dom_sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_dom_sid_to_sid(ctx, dom_sid, &sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_dom_sid_to_smb_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose smb_sid = ctx->alloc_func(sizeof(struct dom_sid), ctx->alloc_pvt);
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose for (c = 0; c < SID_ID_AUTHS; c++) {
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose for (c = 0; c < SID_SUB_AUTHS; c++) {
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_smb_sid_to_dom_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose dom_sid = ctx->alloc_func(sizeof(struct sss_dom_sid), ctx->alloc_pvt);
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose for (c = 0; c < SID_ID_AUTHS; c++) {
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose for (c = 0; c < SID_SUB_AUTHS; c++) {
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_bin_sid_to_smb_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_bin_sid_to_dom_sid(ctx, bin_sid, length, &dom_sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_dom_sid_to_smb_sid(ctx, dom_sid, &smb_sid);
2998435fcc95857b73049b3955af9889ab595f24Sumit Boseenum idmap_error_code sss_idmap_smb_sid_to_bin_sid(struct sss_idmap_ctx *ctx,
2998435fcc95857b73049b3955af9889ab595f24Sumit Bose err = sss_idmap_smb_sid_to_dom_sid(ctx, smb_sid, &dom_sid);