@@ -40,8 +40,12 @@
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
+#include <pwd.h>
+#include <grp.h>
#include <wbclient.h>
+#define INVAV 22 /* This is an invalid authority value */
+
static const char *prog = "cifs.idmap";
static void usage(void)
@@ -79,8 +83,10 @@ cifs_idmap(const key_serial_t key, const char *key_descr)
uid_t uid = 0;
gid_t gid = 0;;
wbcErr rc = 1;
- char *sidstr = NULL;
+ char *idstr, *sidstr;
struct wbcDomainSid sid;
+ struct passwd *pswdptr, *winpswdptr;
+ struct group *grpptr, *wingrpptr;
/*
* Use winbind to convert received string to a SID and lookup
@@ -134,6 +140,75 @@ cifs_idmap(const key_serial_t key, const char *key_descr)
goto cifs_idmap_ret;
}
+ idstr = strget(key_descr, "oi:");
+ if (idstr) {
+ uid = atoi(idstr);
+ pswdptr = getpwuid(uid);
+ if (pswdptr) {
+ rc = wbcGetpwnam(pswdptr->pw_name, &winpswdptr);
+ if (rc)
+ syslog(LOG_ERR, "%s: Invalid user: %s",
+ __func__, pswdptr->pw_name);
+ else {
+ rc = wbcUidToSid(winpswdptr->pw_uid, &sid);
+ if (rc)
+ syslog(LOG_ERR, "uid %d to SID err: %d", winpswdptr->pw_uid, rc);
+ }
+ } else
+ syslog(LOG_ERR, "%s: Invalid uid: %d with error: %s",
+ __func__, uid, strerror(errno));
+ if (!rc) { /* uid has been mapped to a SID */
+ if (sid.id_auth[5] == INVAV) {
+ syslog(LOG_ERR, "%s: Invalid uid %d",
+ __func__, uid);
+ rc = 1;
+ goto cifs_idmap_ret;
+ }
+ rc = keyctl_instantiate(key, &sid,
+ sizeof(struct wbcDomainSid), 0);
+ if (rc)
+ syslog(LOG_ERR, "%s: key inst: %s",
+ __func__, strerror(errno));
+ }
+
+ goto cifs_idmap_ret;
+ }
+
+ idstr = strget(key_descr, "gi:");
+ if (idstr) {
+ gid = atoi(idstr);
+ grpptr = getgrgid(gid);
+ if (grpptr) {
+ rc = wbcGetgrnam(grpptr->gr_name, &wingrpptr);
+ if (rc)
+ syslog(LOG_ERR, "%s: Invalid user: %s",
+ __func__, grpptr->gr_name);
+ else {
+ rc = wbcGidToSid(wingrpptr->gr_gid, &sid);
+ if (rc)
+ syslog(LOG_ERR, "gid %d to SID err: %d", wingrpptr->gr_gid, rc);
+ }
+ } else
+ syslog(LOG_ERR, "%s: Invalid gid: %d with error: %s",
+ __func__, gid, strerror(errno));
+ if (!rc) { /* gid has been mapped to a SID */
+ if (sid.id_auth[5] == INVAV) {
+ syslog(LOG_ERR, "%s: Invalid gid %d",
+ __func__, gid);
+ rc = 1;
+ goto cifs_idmap_ret;
+ }
+ rc = keyctl_instantiate(key, &sid,
+ sizeof(struct wbcDomainSid), 0);
+ if (rc)
+ syslog(LOG_ERR, "%s: key inst: %s",
+ __func__, strerror(errno));
+ }
+
+ goto cifs_idmap_ret;
+ }
+
+
syslog(LOG_DEBUG, "Invalid key: %s", key_descr);
cifs_idmap_ret: