]> andersk Git - moira.git/blobdiff - server/qrtn.qc
catch unexpected ingres errors in a more sensible manner
[moira.git] / server / qrtn.qc
index 3d432483dab3d235cbd3264d222a1ac594f2d410..013903f6d87cf63333f6c4b51a69d66eaea0faee 100644 (file)
@@ -19,13 +19,16 @@ static char *rcsid_qrtn_qc = "$Header$";
 
 char *Argv[16];
 
-static int ingres_errno = 0;
+int ingres_errno = 0;
+int sms_errcode = 0;
+## int query_timeout = 30;
 extern char *whoami;
 extern FILE *journal;
 
 #define INGRES_BAD_INT 4111
 #define INGRES_BAD_DATE 4302
 #define INGRES_DEADLOCK 4700
+#define INGRES_TIMEOUT 4702
 
 /*
  * ingerr: (supposedly) called when Ingres indicates an error.
@@ -36,21 +39,25 @@ extern FILE *journal;
 static int ingerr(num)
     int *num;
 {
-    char buf[256];
+    ingres_errno = *num;
 
     switch (*num) {
     case INGRES_BAD_INT:
-       ingres_errno = SMS_INTEGER;
+       sms_errcode = SMS_INTEGER;
        break;
     case INGRES_BAD_DATE:
-       ingres_errno = SMS_DATE;
+       sms_errcode = SMS_DATE;
        break;
     case INGRES_DEADLOCK:
+       sms_errcode = SMS_DEADLOCK;
        com_err(whoami, 0, "INGRES deadlock detected");
-       ingres_errno = SMS_DEADLOCK;
+       break;
+    case INGRES_TIMEOUT:
+       sms_errcode = SMS_BUSY;
+       com_err(whoami, 0, "timed out getting lock");
        break;
     default:
-       ingres_errno = SMS_INGRES_ERR;
+       sms_errcode = SMS_INGRES_ERR;
        com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
        critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num);
        return (*num);
@@ -66,6 +73,7 @@ int sms_open_database()
     /* initialize local argv */
     for (i = 0; i < 16; i++)
        Argv[i] = malloc(ARGLEN);
+    incremental_init();
 
     IIseterr(ingerr);
        
@@ -73,7 +81,9 @@ int sms_open_database()
        
     /* open the database */
 ##  ingres sms
-##  set lockmode session where readlock = exclusive
+##  set lockmode session where level = table, timeout = query_timeout
+##  set lockmode on capacls where readlock = shared
+##  set lockmode on alias where readlock = shared
     return ingres_errno;
 }
 
@@ -91,6 +101,8 @@ sms_check_access(cl, name, argc, argv_ro)
     struct query *q;
     struct query *get_query_by_name();
 
+    ingres_errno = 0;
+
     q = get_query_by_name(name, cl->args->sms_version_no);
     if (q == (struct query *)0)
        return(SMS_NO_HANDLE);
@@ -120,6 +132,8 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
     struct save_queue *sq_create();
     char *build_sort();
 
+    ingres_errno = 0;
+
     /* list queries command */
     if (!strcmp(name, "_list_queries")) {
        list_queries(cl->args->sms_version_no, action, actarg);
@@ -142,7 +156,6 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
     v = q->validate;
 
     if (q->type != RETRIEVE) {
-##     set lockmode session where readlock = exclusive
 ##      begin transaction
     }
 
@@ -215,7 +228,9 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        /* if q->rvar = NULL, perform post_rtn only */
        if (q->rvar) {
            build_qual(q->qual, q->argc, Argv, qual);
+           incremental_before(q->rtable, qual, argv_ro);
            status = do_update(q, &Argv[q->argc], qual, action, actarg);
+           incremental_after(q->rtable, qual, argv_ro);
            if (status != SMS_SUCCESS) break;
            table = q->rtable;
            if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
@@ -254,8 +269,16 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        /* perform the append */
        /* if q->rvar = NULL, perform post_rtn only */
        if (q->rvar) {
+           incremental_clear_before();
            status = do_append(q, &Argv[q->argc], pqual, action, actarg);
            if (status != SMS_SUCCESS) break;
+           if (v && v->object_id) {
+               sprintf(qual, "%s.%s = values.value and values.name = \"%s\"",
+                       q->rvar, v->object_id, v->object_id);
+               incremental_after(q->rtable, qual, argv_ro);
+           } else
+             incremental_after(q->rtable, pqual, argv_ro);
+
            table = q->rtable;
 ##         repeat replace tblstats (appends = tblstats.appends + 1,
 ##                                  modtime = "now")
@@ -277,7 +300,9 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        /* if q->rvar = NULL, perform post_rtn only */
        if (q->rvar) {
            build_qual(q->qual, q->argc, Argv, qual);
+           incremental_before(q->rtable, qual, argv_ro);
            status = do_delete(q, qual, action, actarg);
+           incremental_clear_after();
            if (status != SMS_SUCCESS) break;
            table = q->rtable;
 ##         repeat replace tblstats (deletes = tblstats.deletes + 1,
@@ -292,10 +317,15 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
     }
 
 out:
+    if (status == SMS_SUCCESS && ingres_errno != 0) {
+       com_err(whoami, SMS_INTERNAL, "Server didn't notice INGRES ERROR %d",
+                      ingres_errno);
+       status = sms_errcode;
+    }
+
     if (q->type != RETRIEVE) {
         if (status == SMS_SUCCESS) {
 ##          end transaction    /* commit to this */
-##         set lockmode session where readlock = system
            if (journal) {
                char buf[1024], *bp;
                int i;
@@ -314,11 +344,14 @@ out:
                putc('\n', journal);
                fflush(journal);
            }
+           incremental_update();
        } else {
-           if (status != SMS_DEADLOCK) {
+           if (ingres_errno != INGRES_DEADLOCK) {
 ##             abort           /* it never happened */
            }
+           incremental_flush();
        }
+##      set lockmode session where readlock = system
     }
 
     if (status != SMS_SUCCESS && log_flags & LOG_RES)
@@ -412,15 +445,18 @@ sms_verify_query(cl, q, argc, argv_ro)
     register struct validate *v = q->validate;
     register int i;
     register int privileged = 0;
+    int len;
 
     /* copy the arguments into a local argv that we can modify */
     if (argc >= QMAXARGS)
       return(SMS_ARGS);
     for (i = 0; i < argc; i++) {
-       if (strlen(argv_ro[i]) < ARGLEN)
+       if ((len = strlen(argv_ro[i])) < ARGLEN)
            strcpy(Argv[i], argv_ro[i]);
        else
            return(SMS_ARG_TOO_LONG);
+       if (Argv[i][len-1] == '\\')
+         return(SMS_BAD_CHAR);
     }
 
     /* check initial query access */
@@ -453,6 +489,11 @@ sms_verify_query(cl, q, argc, argv_ro)
     return(privileged ? SMS_SUCCESS : SMS_PERM);
 }
 
+
+/* This routine caches info from the database.  Each query acl is stored
+ * in the query structure, and whether that acl contains everybody.
+ */
+
 check_query_access(q, argv, cl)
     struct query *q;
     char *argv[];
@@ -468,128 +509,84 @@ check_query_access(q, argv, cl)
     int client_id;
     char *client_type;
 
-    /* get query access control list */
-    name = q->shortname;
-##  repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
-##  inquire_equel (rowcount = "rowcount", errorno = "errorno")
-    if (errorno != 0) return(SMS_INGRES_ERR);
-    if (rowcount == 0) return(SMS_PERM);
-
     /* initialize default uid */
     if (def_uid == 0) {
 ##     retrieve (def_uid = users.users_id) where users.login = "default"
     }
 
-    /* check for default access */
-##  range of m is members
-##  repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
-##                   m.member_type = "USER" and m.#member_id = def_uid))
-    if (exists) return(SMS_SUCCESS);
+    /* get query access control list */
+    if (q->acl != 0)
+      acl_id = q->acl;
+    else {
+       name = q->shortname;
+##     repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
+##     inquire_equel (rowcount = "rowcount", errorno = "errorno")
+       if (errorno != 0) return(SMS_INGRES_ERR);
+       if (rowcount == 0) return(SMS_PERM);
+       q->acl = acl_id;
+
+       /* check for default access */
+##     repeat retrieve (exists = any(imembers.#member_id where
+##                                   imembers.list_id = @acl_id and
+##                                   imembers.member_type = "USER" and
+##                                   imembers.#member_id = def_uid))
+       q->everybody = exists;
+    }
 
-    /* parse client name */
-    status = get_client(cl, &client_type, &client_id);
-    if (status != SMS_SUCCESS) return(status);
+    if (q->everybody)
+      return(SMS_SUCCESS);
 
-    /* see if client is in the list (or any of its sub-lists) */
-    exists = find_member("LIST", acl_id, client_type, client_id, 0);
-    return ((exists) ? SMS_SUCCESS : SMS_PERM);
+    if (get_client(cl, &client_type, &client_id) != SMS_SUCCESS)
+      return(SMS_PERM);
+    if (find_member("LIST", acl_id, client_type, client_id, 0))
+      return(SMS_SUCCESS);
+    else
+      return(SMS_PERM);
 ##}
 
+
 get_client(cl, client_type, client_id)
     client *cl;
     char **client_type;
     int *client_id;
-##{
-    struct krbname *krb;
-##  int member_id;
-##  char *name;
-##  int rowcount;
-
-    if (cl->clname == NULL)
-       return SMS_PERM;
-    
-    /* for now ignore instances */
-    krb = &cl->kname;
-
-    /* if client is from local realm, get users_id */
-    if (!strcmp(krb->realm, krb_realm)) {
+{
+    if (cl->users_id > 0) {
        *client_id = cl->users_id;
        *client_type = "USER";
        return(SMS_SUCCESS);
     }
 
-    /* otherwise use string_id */
-    name = cl->clname;
-##  repeat retrieve (member_id = strings.string_id) 
-##      where strings.string = @name
-       
-    /* make sure we found a users or string id */
-##  inquire_equel (rowcount = "rowcount")
-    if (rowcount == 0) return(SMS_PERM);
+    if (cl->client_id < 0) {
+       *client_id = -cl->users_id;
+       *client_type = "KERBEROS";
+       return(SMS_SUCCESS);
+    }
 
-    *client_type = "STRING";
-    *client_id = member_id;
-    return(SMS_SUCCESS);
-##}
+    return(SMS_PERM);
+}
 
-##find_member(list_type, list_id, member_type, member_id, sq)
+##find_member(list_type, list_id, member_type, member_id)
     char *list_type;
 ##  int list_id;
 ##  char *member_type;
 ##  int member_id;
-    struct save_queue *sq;
 ##{
-##  int exists;
-##  int sublist;
-    int child;
-    struct save_queue *sq_create();
+##  int exists, errorno;
 
     if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
        list_id == member_id)
        return(1);
 
     /* see if client is a direct member of list */
-##  repeat retrieve (exists = any(m.#member_id where 
-##                               m.#list_id = @list_id and
-##                               m.#member_type = @member_type and 
-##                               m.#member_id = @member_id))
-    if (exists) return(1);
-
-    /* are there any sub-lists? */
-##  repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
-##                   m.#member_type = "LIST"))
-    if (!exists) return(0);
-
-    /* yes; now recurse through sublists */
-
-    /* create a save queue */
-    if (sq == (struct save_queue *)0) {
-       sq = sq_create();
-       child = 0;
-    } else {
-       child = 1;
-    }
-
-    /* save all sublist ids */
-##  range of m is members
-##  retrieve (sublist = m.#member_id) 
-##      where m.#list_id = list_id and m.#member_type = "LIST"
-##  {
-        sq_save_unique_data(sq, sublist);
-##  }
-
-    if (child) return(0);
-
-    /* at top-level, check sub-lists for client (breadth-first search) */
-    while (sq_get_data(sq, &sublist)) {
-       exists = find_member(list_type, sublist, member_type, member_id, sq);
-       if (exists) {
-           sq_destroy(sq);
-           return(1);
-       }
-    }
-    sq_destroy(sq);
-    return(0);
+##  repeat retrieve (exists = any(imembers.#member_id where 
+##                               imembers.#list_id = @list_id and
+##                               imembers.#member_type = @member_type and 
+##                               imembers.#member_id = @member_id))
+##  inquire_equel(errorno = "errorno")
+    if (errorno == 0)
+      return(exists);
+    else
+      return(0);
 ##}
 
 
@@ -793,6 +790,77 @@ set_next_object_id(object, table)
 ##}
 
 
+/* This looks up a login name and returns the SMS internal ID.  It is used
+ * by authenticate to put the users_id in the client structure.
+ */
+
+int get_users_id(name)
+char *name;
+##{
+##  int id, rowcount;
+##  char *login;
+
+    login = name;
+
+##  range of u is users
+##  repeat retrieve (id = u.#users_id) where u.#login = @login
+##  inquire_equel (rowcount = "rowcount")
+    
+    if (rowcount == 1)
+       return(id);
+    else
+       return(0);
+##}
+
+
+/* Turn a kerberos name into the user's ID of the account that principal
+ * owns.  Sets the kerberos ID and user ID.
+ */
+
+set_krb_mapping(name, login, ok, kid, uid)
+char *name;
+char *login;
+int ok;
+int *kid;
+int *uid;
+##{
+##  int u_id, k_id, rowcount;
+##  char *krbname;
+
+    krbname = name;
+    *kid = 0;
+    *uid = 0;
+
+##  range of k is krbmap
+##  range of s is strings
+##  repeat retrieve (u_id = k.#users_id, k_id = k.#string_id)
+##     where k.string_id = s.string_id and s.string = @krbname
+##  inquire_equel (rowcount = "rowcount")
+    
+    if (rowcount == 1) {
+       *kid = -k_id;
+       *uid = u_id;
+       return;
+    }
+
+##  repeat retrieve (k_id = s.#string_id) where s.string = @krbname
+##  inquire_equel (rowcount = "rowcount")
+
+    if (rowcount == 1) {
+       *kid = -k_id;
+    }
+
+    if (!ok) {
+       *uid = *kid;
+       return;
+    }
+
+    *uid = get_users_id(login);
+    if (*kid == 0)
+      *kid = *uid;
+##}
+
+
 /* For now this just checks the argc's.  It should also see that there
  * are no duplicate names.
  */
@@ -801,14 +869,22 @@ sanity_check_queries()
 {
     register int i;
     int maxv = 0, maxa = 0;
+#ifdef MULTIPROTOCOLS
     extern int QueryCount1, QueryCount2;
     extern struct query Queries1[], Queries2[];
+#else
+    extern int QueryCount2;
+    extern struct query Queries2[];
+#endif MULTIPROTOCOLS
+
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
 
+#ifdef MULTIPROTOCOLS
     for (i = 0; i < QueryCount1; i++) {
        maxv = MAX(maxv, Queries1[i].vcnt);
        maxa = MAX(maxa, Queries1[i].argc);
     }
+#endif MULTIPROTOCOLS
     for (i = 0; i < QueryCount2; i++) {
        maxv = MAX(maxv, Queries2[i].vcnt);
        maxa = MAX(maxa, Queries2[i].argc);
This page took 0.062452 seconds and 4 git commands to generate.